Use enum for completions

This commit is contained in:
Florian Bruhin 2014-07-29 00:37:32 +02:00
parent 54246bacbe
commit 265019650b
3 changed files with 31 additions and 21 deletions

View File

@ -40,6 +40,7 @@ import qutebrowser.utils.log as log
from qutebrowser.config.iniparsers import ReadConfigParser from qutebrowser.config.iniparsers import ReadConfigParser
from qutebrowser.config.conftypes import ValidationError from qutebrowser.config.conftypes import ValidationError
from qutebrowser.commands.exceptions import CommandError from qutebrowser.commands.exceptions import CommandError
from qutebrowser.utils.usertypes import Completion
def instance(): def instance():
@ -283,7 +284,7 @@ class ConfigManager(QObject):
return existed return existed
@cmdutils.register(name='get', instance='config', @cmdutils.register(name='get', instance='config',
completion=['section', 'option']) completion=[Completion.section, Completion.option])
def get_wrapper(self, sectname, optname): def get_wrapper(self, sectname, optname):
"""Get the value from a section/option. """Get the value from a section/option.
@ -329,7 +330,8 @@ class ConfigManager(QObject):
return newval return newval
@cmdutils.register(name='set', instance='config', @cmdutils.register(name='set', instance='config',
completion=['section', 'option', 'value']) completion=[Completion.section, Completion.option,
Completion.value])
def set_wrapper(self, sectname, optname, value): def set_wrapper(self, sectname, optname, value):
"""Set an option. """Set an option.
@ -344,7 +346,8 @@ class ConfigManager(QObject):
raise CommandError("set: {} - {}".format(e.__class__.__name__, e)) raise CommandError("set: {} - {}".format(e.__class__.__name__, e))
@cmdutils.register(name='set-temp', instance='config', @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): def set_temp_wrapper(self, sectname, optname, value):
"""Set a temporary option. """Set a temporary option.

View File

@ -29,6 +29,7 @@ from qutebrowser.models.completionfilter import CompletionFilterModel as CFM
from qutebrowser.models.completion import ( from qutebrowser.models.completion import (
CommandCompletionModel, SettingSectionCompletionModel, CommandCompletionModel, SettingSectionCompletionModel,
SettingOptionCompletionModel, SettingValueCompletionModel) SettingOptionCompletionModel, SettingValueCompletionModel)
from qutebrowser.utils.usertypes import Completion
class Completer(QObject): class Completer(QObject):
@ -57,30 +58,32 @@ class Completer(QObject):
self.ignore_change = False self.ignore_change = False
self._models = { self._models = {
'option': {}, Completion.option: {},
'value': {}, Completion.value: {},
} }
self._init_command_completion() self._init_command_completion()
self._init_setting_completions() self._init_setting_completions()
def _init_command_completion(self): def _init_command_completion(self):
"""Initialize the command completion model.""" """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): def _init_setting_completions(self):
"""Initialize setting completion models.""" """Initialize setting completion models."""
self._models['section'] = CFM(SettingSectionCompletionModel(self), self._models[Completion.section] = CFM(
self) SettingSectionCompletionModel(self), self)
self._models['option'] = {} self._models[Completion.option] = {}
self._models['value'] = {} self._models[Completion.value] = {}
for sectname in configdata.DATA: for sectname in configdata.DATA:
model = SettingOptionCompletionModel(sectname, self) 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) config.instance().changed.connect(model.on_config_changed)
self._models['value'][sectname] = {} self._models[Completion.value][sectname] = {}
for opt in configdata.DATA[sectname].keys(): for opt in configdata.DATA[sectname].keys():
model = SettingValueCompletionModel(sectname, opt, self) 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) config.instance().changed.connect(model.on_config_changed)
def _get_new_completion(self, parts, cursor_part): def _get_new_completion(self, parts, cursor_part):
@ -92,7 +95,7 @@ class Completer(QObject):
""" """
if cursor_part == 0: if cursor_part == 0:
# '|' or 'set|' # '|' or 'set|'
return self._models['command'] return self._models[Completion.command]
# delegate completion to command # delegate completion to command
try: try:
completions = cmdutils.cmd_dict[parts[0]].completion completions = cmdutils.cmd_dict[parts[0]].completion
@ -102,29 +105,29 @@ class Completer(QObject):
if completions is None: if completions is None:
# command without any available completions # command without any available completions
return None return None
dbg_completions = completions[:] dbg_completions = [c.name for c in completions]
try: try:
idx = cursor_part - 1 idx = cursor_part - 1
completion_name = completions[idx] completion = completions[idx]
except IndexError: except IndexError:
# More arguments than completions # More arguments than completions
logger.debug("completions: {}".format(', '.join(dbg_completions))) logger.debug("completions: {}".format(', '.join(dbg_completions)))
return None return None
dbg_completions[idx] = '*' + dbg_completions[idx] + '*' dbg_completions[idx] = '*' + dbg_completions[idx] + '*'
logger.debug("completions: {}".format(', '.join(dbg_completions))) logger.debug("completions: {}".format(', '.join(dbg_completions)))
if completion_name == 'option': if completion == Completion.option:
section = parts[cursor_part - 1] section = parts[cursor_part - 1]
model = self._models['option'].get(section) model = self._models[completion].get(section)
elif completion_name == 'value': elif completion == Completion.value:
section = parts[cursor_part - 2] section = parts[cursor_part - 2]
option = parts[cursor_part - 1] option = parts[cursor_part - 1]
try: try:
model = self._models['value'][section][option] model = self._models[completion][section][option]
except KeyError: except KeyError:
# No completion model for this section/option. # No completion model for this section/option.
model = None model = None
else: else:
model = self._models.get(completion_name) model = self._models.get(completion)
return model return model
def selection_changed(self, selected, _deselected): def selection_changed(self, selected, _deselected):

View File

@ -247,6 +247,10 @@ KeyMode = enum('KeyMode', 'normal', 'hint', 'command', 'yesno', 'prompt',
'insert', 'passthrough') 'insert', 'passthrough')
# Available command completions
Completion = enum('Completion', 'command', 'section', 'option', 'value')
class Question(QObject): class Question(QObject):
"""A question asked to the user, e.g. via the status bar. """A question asked to the user, e.g. via the status bar.