Skip to content

Commit

Permalink
fix slow completion on projects that provide a cached get_command_sho…
Browse files Browse the repository at this point in the history
…rt_help

the method was not used anymore. Also add get_command_hidden that may also be cached
  • Loading branch information
glehmann committed Oct 15, 2019
1 parent 4d7e9c1 commit af2d549
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 2 additions & 3 deletions click_completion/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ def get_choices(cli, prog_name, args, incomplete):
choices.append((opt, None))
if isinstance(ctx.command, MultiCommand):
for name in ctx.command.list_commands(ctx):
command = ctx.command.get_command(ctx, name)
if match(name, incomplete) and not command.hidden:
choices.append((name, command.get_short_help_str()))
if match(name, incomplete):
choices.append((name, ctx.command.get_command_short_help(ctx, name)))

for item, help in choices:
yield (item, help)
Expand Down
25 changes: 24 additions & 1 deletion click_completion/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,29 @@ def multicommand_get_command_short_help(self, ctx, cmd_name):
str
The sub command short help
"""
return self.get_command(ctx, cmd_name).short_help
return self.get_command(ctx, cmd_name).get_short_help_str()


def multicommand_get_command_hidden(self, ctx, cmd_name):
"""Returns the short help of a subcommand
It allows MultiCommand subclasses to implement more efficient ways to provide the subcommand hidden attribute, for
example by leveraging some caching.
Parameters
----------
ctx : click.core.Context
The current context
cmd_name :
The sub command name
Returns
-------
bool
The sub command hidden status
"""
cmd = self.get_command(ctx, cmd_name)
return cmd.hidden if cmd else False


def _shellcomplete(cli, prog_name, complete_var=None):
Expand Down Expand Up @@ -139,4 +161,5 @@ def patch():
click.types.ParamType.complete = param_type_complete
click.types.Choice.complete = choice_complete
click.core.MultiCommand.get_command_short_help = multicommand_get_command_short_help
click.core.MultiCommand.get_command_hidden = multicommand_get_command_hidden
click.core._bashcomplete = _shellcomplete

0 comments on commit af2d549

Please sign in to comment.