Complete multi-part values correctly

This commit is contained in:
Florian Bruhin 2014-04-09 17:54:41 +02:00
parent 3c343ecff7
commit 3f7635619a
5 changed files with 72 additions and 37 deletions

View File

@ -164,7 +164,20 @@ class Config:
"""Returns True if option is in section.""" """Returns True if option is in section."""
return option in self.config[section] return option in self.config[section]
@cmdutils.register(instance='config', completion=['setting']) @cmdutils.register(name='get', instance='config', completion=['setting'],
split_args=False)
def get_wrapper(self, sectopt):
"""Wrapper for the get-command to have section/option in one arg.
Arguments:
secopt: Section and option, delimited by a space.
Return:
The value of the section/option.
"""
return self.get(*sectopt.split())
def get(self, section, option, fallback=_UNSET, raw=False): def get(self, section, option, fallback=_UNSET, raw=False):
"""Get the value from a section/option. """Get the value from a section/option.

View File

@ -17,8 +17,6 @@
"""A CompletionModel filled with all commands and descriptions.""" """A CompletionModel filled with all commands and descriptions."""
from collections import OrderedDict
from qutebrowser.commands.utils import cmd_dict from qutebrowser.commands.utils import cmd_dict
from qutebrowser.models.completion import CompletionModel from qutebrowser.models.completion import CompletionModel
@ -35,7 +33,7 @@ class CommandCompletionModel(CompletionModel):
cmdlist = [] cmdlist = []
for obj in set(cmd_dict.values()): for obj in set(cmd_dict.values()):
if not obj.hide: if not obj.hide:
cmdlist.append([obj.name, obj.desc]) cmdlist.append((obj.name, obj.desc))
data = OrderedDict() cat = self.new_category("Commands")
data['Commands'] = sorted(cmdlist) for (name, desc) in sorted(cmdlist):
self.init_data(data) self.new_item(cat, name, desc)

View File

@ -20,6 +20,8 @@
import logging import logging
from PyQt5.QtCore import Qt, QVariant, QAbstractItemModel, QModelIndex from PyQt5.QtCore import Qt, QVariant, QAbstractItemModel, QModelIndex
ROLE_MARKS = Qt.UserRole
ROLE_FULLTEXT = Qt.UserRole + 1
class CompletionModel(QAbstractItemModel): class CompletionModel(QAbstractItemModel):
@ -92,23 +94,41 @@ class CompletionModel(QAbstractItemModel):
idx = self.index(k, 0, cat) idx = self.index(k, 0, cat)
old = self.data(idx).value() old = self.data(idx).value()
marks = self._get_marks(needle, old) marks = self._get_marks(needle, old)
self.setData(idx, marks, Qt.UserRole) self.setData(idx, marks, ROLE_MARKS)
def init_data(self, data): def new_category(self, name):
"""Initialize the Qt model based on the data given. """Add a new category to the model.
Args: Args:
data: dict of data to process. name: The name of the category to add.
Return:
The created CompletionItem.
""" """
for (cat, items) in data.items(): cat = CompletionItem([name], self._root)
newcat = CompletionItem([cat], self._root) self._id_map[id(cat)] = cat
self._id_map[id(newcat)] = newcat self._root.children.append(cat)
self._root.children.append(newcat) return cat
for item in items:
newitem = CompletionItem(item, newcat) def new_item(self, cat, name, desc, completion=None):
self._id_map[id(newitem)] = newitem """Add a new item to a category.
newcat.children.append(newitem)
Args:
cat: The parent category.
name: The name of the item.
desc: The description of the item.
completion: The long text to insert for a completion.
None if it's the same as name.
Return:
The created CompletionItem.
"""
item = CompletionItem((name, desc), parent=cat, fulltext=completion)
self._id_map[id(item)] = item
cat.children.append(item)
return item
def removeRows(self, position=0, count=1, parent=QModelIndex()): def removeRows(self, position=0, count=1, parent=QModelIndex()):
"""Remove rows from the model. """Remove rows from the model.
@ -171,7 +191,7 @@ class CompletionModel(QAbstractItemModel):
Args: Args:
index: The QModelIndex for which to get data for. index: The QModelIndex for which to get data for.
roel: The role to use for the data. role: The role to use for the data.
Return: Return:
The data as QVariant or an invalid QVariant on error. The data as QVariant or an invalid QVariant on error.
@ -334,21 +354,25 @@ class CompletionItem():
children: The children of this item. children: The children of this item.
_data: The data of this item. _data: The data of this item.
_marks: The marks of this item. _marks: The marks of this item.
_fulltext: The full text to insert when completing.
None if _data[0] already is the full text.
""" """
def __init__(self, data, parent=None): def __init__(self, data, parent=None, fulltext=None):
"""Constructor for CompletionItem. """Constructor for CompletionItem.
Args: Args:
data: The data for the model, as tuple (columns). data: The data for the model, as tuple (columns).
parent: An optional parent item. parent: An optional parent item.
fulltext: The full text to insert when completing.
""" """
self.parent = parent self.parent = parent
self.children = [] self.children = []
self._data = data self._data = data
self._marks = [] self._marks = []
self._fulltext = fulltext
def data(self, column, role=Qt.DisplayRole): def data(self, column, role=Qt.DisplayRole):
"""Get the data for role/column. """Get the data for role/column.
@ -366,8 +390,10 @@ class CompletionItem():
""" """
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
return self._data[column] return self._data[column]
elif role == Qt.UserRole: elif role == ROLE_MARKS:
return self._marks return self._marks
elif role == ROLE_FULLTEXT:
return self._fulltext
else: else:
raise ValueError("Invalid role {}".format(role)) raise ValueError("Invalid role {}".format(role))
@ -385,8 +411,10 @@ class CompletionItem():
""" """
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
self._data[column] = value self._data[column] = value
elif role == Qt.UserRole: elif role == ROLE_MARKS:
self._marks = value self._marks = value
elif role == ROLE_FULLTEXT:
self._fulltext = value
else: else:
raise ValueError("Invalid role {}".format(role)) raise ValueError("Invalid role {}".format(role))

View File

@ -17,10 +17,6 @@
"""A CompletionModel filled with settings and their descriptions.""" """A CompletionModel filled with settings and their descriptions."""
import logging
from collections import OrderedDict
from qutebrowser.models.completion import CompletionModel from qutebrowser.models.completion import CompletionModel
from qutebrowser.config.configdata import configdata from qutebrowser.config.configdata import configdata
@ -33,11 +29,8 @@ class SettingCompletionModel(CompletionModel):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
data = OrderedDict()
for secname, secdata in configdata().items(): for secname, secdata in configdata().items():
newdata = [] cat = self.new_category(secname)
for name in secdata.values.keys(): for name in secdata.values.keys():
newdata.append((name, secdata.descriptions[name])) self.new_item(cat, name, secdata.descriptions[name],
data[secname] = newdata '{} {}'.format(secname, name))
logging.debug("Setting data: {}".format(data))
self.init_data(data)

View File

@ -34,6 +34,7 @@ from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
import qutebrowser.config.config as config import qutebrowser.config.config as config
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
from qutebrowser.models.completion import ROLE_MARKS, ROLE_FULLTEXT
from qutebrowser.config.style import get_stylesheet from qutebrowser.config.style import get_stylesheet
from qutebrowser.commands.parsers import split_cmdline from qutebrowser.commands.parsers import split_cmdline
from qutebrowser.models.completionfilter import CompletionFilterModel from qutebrowser.models.completionfilter import CompletionFilterModel
@ -48,7 +49,7 @@ class CompletionView(QTreeView):
Based on QTreeView but heavily customized so root elements show as category Based on QTreeView but heavily customized so root elements show as category
headers, and children show as flat list. headers, and children show as flat list.
Highlights completions based on marks in the UserRole. Highlights completions based on marks in the ROLE_MARKS data.
Attributes: Attributes:
_model: The currently active filter model. _model: The currently active filter model.
@ -259,10 +260,12 @@ class CompletionView(QTreeView):
idx = self._next_idx(shift) idx = self._next_idx(shift)
self.selectionModel().setCurrentIndex( self.selectionModel().setCurrentIndex(
idx, QItemSelectionModel.ClearAndSelect) idx, QItemSelectionModel.ClearAndSelect)
data = self._model.data(idx, role=ROLE_FULLTEXT)
if data is None:
data = self._model.data(idx) data = self._model.data(idx)
if data is not None: if data is not None:
self._ignore_next = True self._ignore_next = True
self.change_completed_part.emit(self._model.data(idx)) self.change_completed_part.emit(data)
class _CompletionItemDelegate(QStyledItemDelegate): class _CompletionItemDelegate(QStyledItemDelegate):
@ -407,7 +410,7 @@ class _CompletionItemDelegate(QStyledItemDelegate):
self._doc.setDocumentMargin(2) self._doc.setDocumentMargin(2)
if index.column() == 0: if index.column() == 0:
marks = index.data(Qt.UserRole) marks = index.data(ROLE_MARKS)
for mark in marks: for mark in marks:
cur = QTextCursor(self._doc) cur = QTextCursor(self._doc)
cur.setPosition(mark[0]) cur.setPosition(mark[0])