From b74a97580f0ca811be708c01c540af0b8cacbb0b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 2 May 2014 14:18:40 +0200 Subject: [PATCH] Handle completions for ValueList sections correctly --- TODO | 1 - qutebrowser/models/completion.py | 16 ++++++++++--- qutebrowser/utils/usertypes.py | 15 +++++++++++++ qutebrowser/widgets/_completion.py | 36 +++++++++++++++++++++--------- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 1e5b6d825..d8e147c66 100644 --- a/TODO +++ b/TODO @@ -37,7 +37,6 @@ handle completion for aliases keybind should have completion for commands/arguments Hiding scrollbars Ctrl+A/X to increase/decrease last number in URL -command completion gets hidden when doing a new ValueList value logging contexts catch import errors for PyQt and QtWebKit - Add more element-selection-detection code (with options?) based on: diff --git a/qutebrowser/models/completion.py b/qutebrowser/models/completion.py index d9d821465..60cd043eb 100644 --- a/qutebrowser/models/completion.py +++ b/qutebrowser/models/completion.py @@ -61,10 +61,20 @@ class SettingValueCompletionModel(BaseCompletionModel): # pylint: disable=abstract-method - def __init__(self, section, option, parent=None): + def __init__(self, section, option=None, parent=None): super().__init__(parent) - cat = self.new_category("Setting values for {}".format(option)) - vals = configdata.DATA[section][option].typ.complete() + if hasattr(configdata.DATA[section], 'valtype'): + # Same type for all values (ValueList) + cat = self.new_category("Setting values for {} options".format( + section)) + vals = configdata.DATA[section].valtype.complete() + else: + if option is None: + raise ValueError("option may only be None for ValueList " + "sections, but {} is not!".format(section)) + # Different type for each value (KeyValue) + cat = self.new_category("Setting values for {}".format(option)) + vals = configdata.DATA[section][option].typ.complete() if vals is None: raise NoCompletionsError for (val, desc) in vals: diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 6e541ad69..e8dfd1ef6 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -140,3 +140,18 @@ class NeighborList: else: self.idx = self._items.index(self._default) return self.curitem() + + +class FakeDict: + + """A fake dictionary which always returns the same value. + + Attributes: + _val: The value to return. + """ + + def __init__(self, val): + self._val = val + + def __getitem__(self, _key): + return self._val diff --git a/qutebrowser/widgets/_completion.py b/qutebrowser/widgets/_completion.py index ac8d71110..cd0178372 100644 --- a/qutebrowser/widgets/_completion.py +++ b/qutebrowser/widgets/_completion.py @@ -41,6 +41,7 @@ from qutebrowser.models.completionfilter import CompletionFilterModel from qutebrowser.models.completion import ( CommandCompletionModel, SettingSectionCompletionModel, SettingOptionCompletionModel, SettingValueCompletionModel) +from qutebrowser.utils.usertypes import FakeDict class CompletionView(QTreeView): @@ -114,17 +115,7 @@ class CompletionView(QTreeView): 'option': {}, 'value': {}, } - for sect in configdata.DATA.keys(): - self._models['option'][sect] = CompletionFilterModel( - SettingOptionCompletionModel(sect, self)) - self._models['value'][sect] = {} - for opt in configdata.DATA[sect].keys(): - try: - self._models['value'][sect][opt] = ( - CompletionFilterModel(SettingValueCompletionModel( - sect, opt, self))) - except NoCompletionsError: - pass + self._init_completions() self._completing = False self._delegate = _CompletionItemDelegate(self) @@ -144,6 +135,29 @@ class CompletionView(QTreeView): self.hide() # FIXME set elidemode + def _init_completions(self): + """Initialize completion models.""" + for sectname, sect in configdata.DATA.items(): + self._models['option'][sectname] = CompletionFilterModel( + SettingOptionCompletionModel(sectname, self)) + if hasattr(sect, 'valtype'): + # Same type for all values (ValueList) + try: + model = CompletionFilterModel( + SettingValueCompletionModel(sectname, parent=self)) + self._models['value'][sectname] = FakeDict(model) + except NoCompletionsError: + pass + else: + self._models['value'][sectname] = {} + for opt in configdata.DATA[sectname].keys(): + try: + self._models['value'][sectname][opt] = ( + CompletionFilterModel(SettingValueCompletionModel( + sectname, opt, self))) + except NoCompletionsError: + pass + def _next_idx(self, upwards): """Get the previous/next QModelIndex displayed in the view.