Refactor duplicate code for command completion.

Consolidate the logic used to generate the command completion category
into one place. This is shared by CommandCompletionModel,
HelpCompletionModel, and BindCompletionModel.
This commit is contained in:
Ryan Roden-Corrent 2016-08-01 17:06:49 -04:00
parent 0ea61d5f15
commit 8dbc368d08
2 changed files with 42 additions and 44 deletions

View File

@ -39,23 +39,11 @@ class CommandCompletionModel(base.BaseCompletionModel):
def __init__(self, parent=None):
super().__init__(parent)
assert cmdutils.cmd_dict
cmdlist = []
for obj in set(cmdutils.cmd_dict.values()):
if (obj.hide or (obj.debug and not objreg.get('args').debug) or
obj.deprecated):
pass
else:
cmdlist.append((obj.name, obj.desc))
for name, cmd in config.section('aliases').items():
cmdlist.append((name, "Alias for '{}'".format(cmd)))
cmdlist = _get_cmd_completions(include_aliases=True,
include_hidden=False)
cat = self.new_category("Commands")
# map each command to its bound keys and show these in the misc column
key_config = objreg.get('key-config')
cmd_to_keys = key_config.get_reverse_bindings_for('normal')
for (name, desc) in sorted(cmdlist):
self.new_item(cat, name, desc, ', '.join(cmd_to_keys[name]))
for (name, desc, misc) in cmdlist:
self.new_item(cat, name, desc, misc, )
class HelpCompletionModel(base.BaseCompletionModel):
@ -72,17 +60,11 @@ class HelpCompletionModel(base.BaseCompletionModel):
def _init_commands(self):
"""Fill completion with :command entries."""
assert cmdutils.cmd_dict
cmdlist = []
for obj in set(cmdutils.cmd_dict.values()):
if ((obj.debug and not objreg.get('args').debug) or
obj.deprecated):
pass
else:
cmdlist.append((':' + obj.name, obj.desc))
cmdlist = _get_cmd_completions(include_aliases=False,
include_hidden=True, prefix=':')
cat = self.new_category("Commands")
for (name, desc) in sorted(cmdlist):
self.new_item(cat, name, desc)
for (name, desc, misc) in cmdlist:
self.new_item(cat, name, desc, misc)
def _init_settings(self):
"""Fill completion with section->option entries."""
@ -272,20 +254,35 @@ class BindCompletionModel(base.BaseCompletionModel):
def __init__(self, parent=None):
super().__init__(parent)
assert cmdutils.cmd_dict
cmdlist = []
for obj in set(cmdutils.cmd_dict.values()):
if ((obj.debug and not objreg.get('args').debug) or
obj.deprecated):
pass
else:
cmdlist.append((obj.name, obj.desc))
for name, cmd in config.section('aliases').items():
cmdlist.append((name, "Alias for '{}'".format(cmd)))
cmdlist = _get_cmd_completions(include_hidden=True,
include_aliases=True)
cat = self.new_category("Commands")
for (name, desc, misc) in cmdlist:
self.new_item(cat, name, desc, misc)
# map each command to its bound keys and show these in the misc column
key_config = objreg.get('key-config')
cmd_to_keys = key_config.get_reverse_bindings_for('normal')
for (name, desc) in sorted(cmdlist):
self.new_item(cat, name, desc, ', '.join(cmd_to_keys[name]))
def _get_cmd_completions(include_hidden, include_aliases, prefix=''):
"""Get a list of completions info for commands, sorted by name.
Args:
include_hidden: True to include commands annotated with hide=True.
include_aliases: True to include command aliases.
prefix: String to append to the command name.
Return: A list of tuples of form (name, description, bindings).
"""
assert cmdutils.cmd_dict
cmdlist = []
cmd_to_keys = objreg.get('key-config').get_reverse_bindings_for('normal')
for obj in set(cmdutils.cmd_dict.values()):
if ((not obj.debug or objreg.get('args').debug) and
not obj.deprecated and
(include_hidden or not obj.hide)):
bindings = ', '.join(cmd_to_keys[obj.name])
cmdlist.append((prefix + obj.name, obj.desc, bindings))
if include_aliases:
for name, cmd in config.section('aliases').items():
cmdlist.append((name, "Alias for '{}'".format(cmd), ''))
return sorted(cmdlist)

View File

@ -189,7 +189,7 @@ def test_command_completion(qtmodeltester, monkeypatch, stubs, config_stub,
]
def test_help_completion(qtmodeltester, monkeypatch, stubs):
def test_help_completion(qtmodeltester, monkeypatch, stubs, key_config_stub):
"""Test the results of command completion.
Validates that:
@ -201,6 +201,7 @@ def test_help_completion(qtmodeltester, monkeypatch, stubs):
- only the first line of a multiline description is shown
"""
module = 'qutebrowser.completion.models.miscmodels'
key_config_stub.set_bindings_for('normal', {'s': 'stop', 'rr': 'roll'})
_patch_cmdutils(monkeypatch, stubs, module + '.cmdutils')
_patch_configdata(monkeypatch, stubs, module + '.configdata.DATA')
model = miscmodels.HelpCompletionModel()
@ -212,8 +213,8 @@ def test_help_completion(qtmodeltester, monkeypatch, stubs):
("Commands", [
(':drop', 'drop all user data', ''),
(':hide', '', ''),
(':roll', 'never gonna give you up', ''),
(':stop', 'stop qutebrowser', ''),
(':roll', 'never gonna give you up', 'rr'),
(':stop', 'stop qutebrowser', 's'),
]),
("Settings", [
('general->time', 'Is an illusion.', ''),