From 265019650b360bad89b92ee8c8b3ed561939cdb4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 29 Jul 2014 00:37:32 +0200 Subject: [PATCH] Use enum for completions --- qutebrowser/config/config.py | 9 +++++--- qutebrowser/utils/completer.py | 39 ++++++++++++++++++---------------- qutebrowser/utils/usertypes.py | 4 ++++ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 0568e3791..dd0d06cf4 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -40,6 +40,7 @@ import qutebrowser.utils.log as log from qutebrowser.config.iniparsers import ReadConfigParser from qutebrowser.config.conftypes import ValidationError from qutebrowser.commands.exceptions import CommandError +from qutebrowser.utils.usertypes import Completion def instance(): @@ -283,7 +284,7 @@ class ConfigManager(QObject): return existed @cmdutils.register(name='get', instance='config', - completion=['section', 'option']) + completion=[Completion.section, Completion.option]) def get_wrapper(self, sectname, optname): """Get the value from a section/option. @@ -329,7 +330,8 @@ class ConfigManager(QObject): return newval @cmdutils.register(name='set', instance='config', - completion=['section', 'option', 'value']) + completion=[Completion.section, Completion.option, + Completion.value]) def set_wrapper(self, sectname, optname, value): """Set an option. @@ -344,7 +346,8 @@ class ConfigManager(QObject): raise CommandError("set: {} - {}".format(e.__class__.__name__, e)) @cmdutils.register(name='set-temp', instance='config', - completion=['section', 'option', 'value']) + completion=[Completion.section, Completion.option, + Completion.value]) def set_temp_wrapper(self, sectname, optname, value): """Set a temporary option. diff --git a/qutebrowser/utils/completer.py b/qutebrowser/utils/completer.py index 3604673f8..3d229a120 100644 --- a/qutebrowser/utils/completer.py +++ b/qutebrowser/utils/completer.py @@ -29,6 +29,7 @@ from qutebrowser.models.completionfilter import CompletionFilterModel as CFM from qutebrowser.models.completion import ( CommandCompletionModel, SettingSectionCompletionModel, SettingOptionCompletionModel, SettingValueCompletionModel) +from qutebrowser.utils.usertypes import Completion class Completer(QObject): @@ -57,30 +58,32 @@ class Completer(QObject): self.ignore_change = False self._models = { - 'option': {}, - 'value': {}, + Completion.option: {}, + Completion.value: {}, } self._init_command_completion() self._init_setting_completions() def _init_command_completion(self): """Initialize the command completion model.""" - self._models['command'] = CFM(CommandCompletionModel(self), self) + self._models[Completion.command] = CFM( + CommandCompletionModel(self), self) def _init_setting_completions(self): """Initialize setting completion models.""" - self._models['section'] = CFM(SettingSectionCompletionModel(self), - self) - self._models['option'] = {} - self._models['value'] = {} + self._models[Completion.section] = CFM( + SettingSectionCompletionModel(self), self) + self._models[Completion.option] = {} + self._models[Completion.value] = {} for sectname in configdata.DATA: model = SettingOptionCompletionModel(sectname, self) - self._models['option'][sectname] = CFM(model, self) + self._models[Completion.option][sectname] = CFM(model, self) config.instance().changed.connect(model.on_config_changed) - self._models['value'][sectname] = {} + self._models[Completion.value][sectname] = {} for opt in configdata.DATA[sectname].keys(): model = SettingValueCompletionModel(sectname, opt, self) - self._models['value'][sectname][opt] = CFM(model, self) + self._models[Completion.value][sectname][opt] = CFM( + model, self) config.instance().changed.connect(model.on_config_changed) def _get_new_completion(self, parts, cursor_part): @@ -92,7 +95,7 @@ class Completer(QObject): """ if cursor_part == 0: # '|' or 'set|' - return self._models['command'] + return self._models[Completion.command] # delegate completion to command try: completions = cmdutils.cmd_dict[parts[0]].completion @@ -102,29 +105,29 @@ class Completer(QObject): if completions is None: # command without any available completions return None - dbg_completions = completions[:] + dbg_completions = [c.name for c in completions] try: idx = cursor_part - 1 - completion_name = completions[idx] + completion = completions[idx] except IndexError: # More arguments than completions logger.debug("completions: {}".format(', '.join(dbg_completions))) return None dbg_completions[idx] = '*' + dbg_completions[idx] + '*' logger.debug("completions: {}".format(', '.join(dbg_completions))) - if completion_name == 'option': + if completion == Completion.option: section = parts[cursor_part - 1] - model = self._models['option'].get(section) - elif completion_name == 'value': + model = self._models[completion].get(section) + elif completion == Completion.value: section = parts[cursor_part - 2] option = parts[cursor_part - 1] try: - model = self._models['value'][section][option] + model = self._models[completion][section][option] except KeyError: # No completion model for this section/option. model = None else: - model = self._models.get(completion_name) + model = self._models.get(completion) return model def selection_changed(self, selected, _deselected): diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index e32ad7dc8..5bdd15329 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -247,6 +247,10 @@ KeyMode = enum('KeyMode', 'normal', 'hint', 'command', 'yesno', 'prompt', 'insert', 'passthrough') +# Available command completions +Completion = enum('Completion', 'command', 'section', 'option', 'value') + + class Question(QObject): """A question asked to the user, e.g. via the status bar.