Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign Completion System #1622

Merged
merged 7 commits into from
Oct 3, 2020

Conversation

amy-lei
Copy link
Contributor

@amy-lei amy-lei commented Jul 7, 2020

Resolves #1484
Resolves #780 (Files and paths)

Redesign the Completion to be more extensible:

  • Created completion classes that extend ShellComplete
    • Users can write their own class that extends ShellComplete via add_completion_class
  • Added a shell_complete function to every Click object (types, params, etc.) that takes in ctx, all_args, and incomplete to determine the metadata in the form (type, value, help)
  • Shell scripts use metadata to determine the list of completions
    • dir indicates to shells to default to their own completion system for directories
    • file indicates to shells to default to their own completion system for files
    • plain indicates to the shell to use the value of metadata

See #1622 (comment) for more changes.

@kx-chen kx-chen force-pushed the 1484-redesign-completion branch from e811891 to 9c3645e Compare July 8, 2020 18:38
@amy-lei amy-lei force-pushed the 1484-redesign-completion branch 3 times, most recently from 86ed365 to 5e78c64 Compare July 10, 2020 23:01
@kx-chen kx-chen force-pushed the 1484-redesign-completion branch 2 times, most recently from 9abd538 to ef7850a Compare July 17, 2020 07:02
@amy-lei amy-lei force-pushed the 1484-redesign-completion branch from e282e78 to f64f0a4 Compare July 20, 2020 20:03
@kx-chen kx-chen force-pushed the 1484-redesign-completion branch 2 times, most recently from b1737ad to 6ed101d Compare July 22, 2020 02:30
@amy-lei amy-lei force-pushed the 1484-redesign-completion branch 2 times, most recently from 9f332b9 to 12e6b08 Compare July 22, 2020 18:06
@amy-lei amy-lei marked this pull request as ready for review July 22, 2020 19:09
@kx-chen kx-chen force-pushed the 1484-redesign-completion branch from 2071ff2 to 5cf7e04 Compare July 23, 2020 00:20
@kx-chen kx-chen force-pushed the 1484-redesign-completion branch 2 times, most recently from daf7b52 to 2438e73 Compare August 4, 2020 22:57
CHANGES.rst Outdated Show resolved Hide resolved
src/click/shell_completion.py Outdated Show resolved Hide resolved
src/click/shell_completion.py Outdated Show resolved Hide resolved
src/click/types.py Outdated Show resolved Hide resolved
@kx-chen kx-chen force-pushed the 1484-redesign-completion branch from 2438e73 to 5e5815b Compare August 6, 2020 07:57
@davidism davidism added the f:completion feature: shell completion label Aug 7, 2020
src/click/types.py Outdated Show resolved Hide resolved
@jkowalleck
Copy link
Contributor

Hello @amy-lei & @kx-chen

could you review MLH-Fellowship#28 and merge it into your branch, so #1622 gets updated according to #1529 ?

thanks in advance.

@davidism
Copy link
Member

davidism commented Sep 29, 2020

Pushed what I've been working on so far. It looks like it rewrites the entire rewrite, but most of the changes are cosmetic or reorganizing, not rewriting the logic for the most part. I'll rebase this into the original commit, to keep the rewrite history useful for later. Haven't looked at the docs or changelog yet, and the failing tests are Bash not being available in those envs.

  • Removed top-level exports.
  • Command subclasses call super().shell_complete() to extend the completion results.
  • If an option has an autocompletion function, it is preferred over the type completion, rather than the opposite. This allows custom completion to override type completion, which makes more sense.
  • Expanded ShellComplete class methods to make it easier to override/introspect specific parts of completion.
  • Don't use abc for now, didn't seem to be much use for it. Don't check type in add_completion_class.
  • Don't need deepcopy, args.copy() is sufficient.
  • Made param names and docstrings consistent.
  • Rewrote tests to use objects directly instead of decorators, which makes for more concise code. Added tests for the full source and complete env var invocations.

Things I noticed that I want to address:

  • Use a class instead of a tuple for completion items.
  • Should an autocompletion function be passed the param? I think we might have discussed this when we introduced it.
  • Should autocompletion be renamed to shell_complete? (And stored as self.custom_shell_complete.)
  • Extract completion code from BaseCommand.main. Then an extension could potentially replace the entire completion system, including how it's activated.
  • Exit if complete_var is present in the env at all, not just if the completion instruction was valid. Doesn't seem to be any reason for the current need to return True or False from completion.
  • Completion passes all the args to every function, not just the parsed args for the relevant object. This seems to be so Command.shell_complete can exclude options that have already been given. But it's not particularly accurate right now because it lets other names for the same option through, and can be tripped up if a group and its command both have an option with the same name. ctx.params can be used in most cases, but misses names that aren't exposed, but ctx.args is empty after parsing. Perhaps add a ctc._all_params to track all parsed options?

@davidism davidism force-pushed the 1484-redesign-completion branch from 2dc3348 to 0595b52 Compare September 29, 2020 01:57
@kortina
Copy link

kortina commented Sep 29, 2020

FYI, on @davidism hunch, I (in the pip installed click, version on my machine, 7.1.2) edited the file at:

$HOME/.pyenv/versions/3.7.0/lib/python3.7/site-packages/click/_bashcomplete.py

And removed the line in question:
https://github.com/pallets/click/blob/master/src/click/_bashcomplete.py#L69

Now it respects my zsh config!

Separately from this PR, do you think I should submit a PR that just removes that line?

@davidism
Copy link
Member

I already got it. It was originally introduced as part of #1059, but didn't seem relevant to that, so I removed it.

@kortina
Copy link

kortina commented Sep 29, 2020

ahhh, this is so awesome! 🙏🏿🙏🏿

@jkowalleck
Copy link
Contributor

@davidism the latest changes from 2dc3348 to 0595b52 removed some tests.
i am not sure if they also removed some features, but you should have a closer look at it.

@davidism
Copy link
Member

The file was renamed, and since it was also rewritten significantly git shows it as deleted (and a new file created). All the behaviors are still tested.

@jkowalleck
Copy link
Contributor

The file was renamed, and since it was also rewritten significantly git shows it as deleted (and a new file created). All the behaviors are still tested.

@davidism
could you point out where the features of test_command_names()
are tested as it was in https://github.com/pallets/click/blob/0daa2e046d4fae1f81f8f4bee194189a81878f1e/tests/test_shellcomplete.py

@davidism
Copy link
Member

davidism commented Sep 29, 2020

https://github.com/pallets/click/pull/1622/files#diff-ccad5580eac2cb11fe983477ec9559daR204-R208

The longer test wasn't necessary, as they were all different ways to write the same underlying Click behavior, which is now what is represented in the test.

@jkowalleck
Copy link
Contributor

@davidism thanks for making this clear.
appreciate it.

The longer test wasn't necessary, as they were all different ways to write the same underlying Click behavior, which is now what is represented in the test.
you are right. previously tests were functional. now they are more like unit-tests.

src/click/types.py Outdated Show resolved Hide resolved
@davidism davidism force-pushed the 1484-redesign-completion branch 3 times, most recently from 2e4c8a2 to 6e0af87 Compare October 3, 2020 03:16
amy-lei and others added 2 commits October 3, 2020 08:01
Co-authored-by: Kai Chen <kaichen120@gmail.com>
Co-authored-by: David Lord <davidism@gmail.com>
Co-authored-by: Amy <leiamy12@gmail.com>
Co-authored-by: David Lord <davidism@gmail.com>
@davidism davidism force-pushed the 1484-redesign-completion branch from 598f69c to 6313dd2 Compare October 3, 2020 19:06
amy-lei and others added 4 commits October 3, 2020 12:08
Co-authored-by: Kai Chen <kaichen120@gmail.com>
Co-authored-by: David Lord <davidism@gmail.com>
new function takes additional param arg, must return a homogeneous list
of strings or CompletionItem, and must perform matching on results
@davidism davidism force-pushed the 1484-redesign-completion branch from 6313dd2 to 3faede8 Compare October 3, 2020 19:28
@davidism davidism merged commit 6c8301e into pallets:master Oct 3, 2020
@davidism davidism deleted the 1484-redesign-completion branch October 3, 2020 20:52
@davidism davidism added this to the 8.0.0 milestone Oct 3, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f:completion feature: shell completion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redesign completion system Tab completion behavior for click.File and click.Path types
7 participants