Completion for varargs.

When a command has positional varargs, keep offering the configured
completion for each successive argument.

Right now this only influences `config-cycle`.

Previously, `config-cycle <option> ` would offer a value completion for
only the first argument after the option. Now it will keep offering
value completion for each successive argument.

This will be useful for passing multiple tags to the new bookmark
commands that will be added for #882.
This commit is contained in:
Ryan Roden-Corrent 2018-03-25 21:59:30 -04:00
parent d4ea1df232
commit f237a87ad0
2 changed files with 17 additions and 0 deletions

View File

@ -123,6 +123,7 @@ class Command:
self.pos_args = []
self.desc = None
self.flags_with_args = []
self._has_vararg = False
# This is checked by future @cmdutils.argument calls so they fail
# (as they'd be silently ignored otherwise)
@ -170,6 +171,8 @@ class Command:
def get_pos_arg_info(self, pos):
"""Get an ArgInfo tuple for the given positional parameter."""
if pos >= len(self.pos_args) and self._has_vararg:
pos = len(self.pos_args) - 1
name = self.pos_args[pos][0]
return self._qute_args.get(name, ArgInfo())
@ -233,6 +236,8 @@ class Command:
log.commands.vdebug('Adding arg {} of type {} -> {}'.format(
param.name, typ, callsig))
self.parser.add_argument(*args, **kwargs)
if param.kind == inspect.Parameter.VAR_POSITIONAL:
self._has_vararg = True
return signature.parameters.values()
def _param_to_argparse_kwargs(self, param, is_bool):

View File

@ -129,12 +129,20 @@ def cmdutils_patch(monkeypatch, stubs, miscmodels_patch):
"""docstring."""
pass
@cmdutils.argument('option', completion=miscmodels_patch.option)
@cmdutils.argument('values', completion=miscmodels_patch.value)
def config_cycle(option, *values):
"""For testing varargs."""
pass
cmd_utils = stubs.FakeCmdUtils({
'set': command.Command(name='set', handler=set_command),
'help': command.Command(name='help', handler=show_help),
'open': command.Command(name='open', handler=openurl, maxsplit=0),
'bind': command.Command(name='bind', handler=bind),
'tab-detach': command.Command(name='tab-detach', handler=tab_detach),
'config-cycle': command.Command(name='config-cycle',
handler=config_cycle),
})
monkeypatch.setattr(completer, 'cmdutils', cmd_utils)
@ -191,6 +199,10 @@ def _set_cmd_prompt(cmd, txt):
('/:help|', None, '', []),
('::bind|', 'command', ':bind', []),
(':-w open |', None, '', []),
# varargs
(':config-cycle option |', 'value', '', ['option']),
(':config-cycle option one |', 'value', '', ['option', 'one']),
(':config-cycle option one two |', 'value', '', ['option', 'one', 'two']),
])
def test_update_completion(txt, kind, pattern, pos_args, status_command_stub,
completer_obj, completion_widget_stub, config_stub,