Skip to content

Commit

Permalink
Issue click-contrib#68, added support in click's autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ori Markovitch committed Dec 4, 2019
1 parent 2d78dc5 commit 529ce5f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion click_repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,42 @@ def get_completions(self, document, complete_event=None):
choices = []
for param in ctx.command.params:
if isinstance(param, click.Option):
for options in (param.opts, param.secondary_opts):
possible_options = (params.opts, params.secondary_opts)
called = False
for options in possible_options:
for o in options:
choices.append(
Completion(
text_type(o), -len(incomplete), display_meta=param.help
)
)
# We want to make sure if this parameter was called
if o in args[param.nargs * -1:]:
called = True
if called and hasattr(param, autocompletion) and param.autocompletion is not None:
for autocomplete in param.autocompletion(ctx, args, incomplete):
if isinstance(autocomplete, tuple):
choices.append(Completion(
text_type(auto_complete[0]), -len(incomplete),
display_meta=auto_complete[1]
))
else:
choices.append(Completion(text_type(auto_complete), -len(incomplete)))

elif isinstance(param, click.Argument):
if isinstance(param.type, click.Choice):
for choice in param.type.choices:
choices.append(Completion(text_type(choice), -len(incomplete)))
if hasattr(param, autocompletion) and param.autocompletion is not None:
for autocomplete in param.autocompletion(ctx, args, incomplete):
if isinstance(autocomplete, tuple):
choices.append(Completion(
text_type(auto_complete[0]), -len(incomplete),
display_meta=auto_complete[1]
))
else:
choices.append(Completion(text_type(auto_complete), -len(incomplete)))


if isinstance(ctx.command, click.MultiCommand):
for name in ctx.command.list_commands(ctx):
Expand Down

0 comments on commit 529ce5f

Please sign in to comment.