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

Stablize Rust-Native Completion Engine Tracking Issue #3166

Open
6 of 17 tasks
epage opened this issue Dec 13, 2021 · 14 comments
Open
6 of 17 tasks

Stablize Rust-Native Completion Engine Tracking Issue #3166

epage opened this issue Dec 13, 2021 · 14 comments
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations C-tracking-issue Category: A tracking issue for an unstable feature E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@epage
Copy link
Member

epage commented Dec 13, 2021

Maintainer's notes:

Remaining work for feature parity

Non-blocking work

Design considerations

  • Make it easy to use for the natively supported shells but allow other shells to be supported

Prior art


#3022 was a tipping point for me in realizing that maybe our current approach to completions doesn't work. We effectively have to implement a mostly-untested parser within each shell. Examples of other problems that seem to stem from this:

If we take the approach of argcomplete where we do the parsing in our core code, rather than in each completion script, this will help us share parsing logic between shell, share some or all parsing logic with clap itself, and make a subset of the logic more testable.

We also need to decide whether to morph the existing parser into supporting this or create a custom parser (since the needs are pretty special). If we do a custom parser, we should probably do #2915 first so we can reuse lexing logic between clap and the completion generation. I've also been considering #2912 which would allow reusing the completion logic with any CLI parser.

@epage epage added C-enhancement Category: Raise on the bar on expectations A-completion Area: completion generator S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Dec 13, 2021
@epage
Copy link
Member Author

epage commented Apr 15, 2022

Bash's expectations

Inputs for -F and -C:

  • COMP_LINE: The current command line. This variable is available only in shell functions and external commands invoked by the programmable completion facilities
  • COMP_POINT: The index of the current cursor position relative to the beginning of the current command. If the current cursor position is at the end of the current command, the value of this variable is equal to ${#COMP_LINE}. This variable is available only in shell functions and external commands invoked by the programmable completion facilities
  • COMP_KEY: The key (or final key of a key sequence) used to invoke the current completion function.
  • COMP_TYPE: Set to an integer value corresponding to the type of completion attempted that caused a completion function to be called: TAB, for normal completion, ‘?’, for listing completions after successive tabs, ‘!’, for listing alternatives on partial word completion, ‘@’, to list completions if the word is not unmodified, or ‘%’, for menu completion. This variable is available only in shell functions and external commands invoked by the programmable completion facilities
  • 1: name of the command whose arguments are being completed
  • 2: word being completed
  • 3: word preceding the word being completed on the current command line

Inputs for -F:

  • COMP_WORDS, COMP_CWORD when used with -F

Output for -C:

  • print a list of completions, one per line, to the standard output. Backslash may be used to escape a newline, if necessary.

Output for -F:

  • It must put the possible completions in the COMPREPLY array variable, one per array element.

See

@epage
Copy link
Member Author

epage commented Apr 16, 2022

argcomplete emulates bash's interface in fish by

    set -x COMP_LINE (commandline -p)
    set -x COMP_POINT (string length (commandline -cp))
    set -x COMP_TYPE

and then just registers that function

https://github.com/kislyuk/argcomplete/blob/develop/argcomplete/shell_integration.py#L60

@epage
Copy link
Member Author

epage commented Apr 16, 2022

Value hints are supported on

  • zsh
  • fish

Tooltips are supported on

  • zsh
  • powershell
  • elvish
  • fish

We should make sure we don't lose these features as part of this transition, ie we shouldn't drop to the lowest common denominator.

@epage
Copy link
Member Author

epage commented Apr 22, 2022

For Powershell

Register-ArgumentCompleter
        -CommandName <String[]>
        -ScriptBlock <ScriptBlock>
        [-Native]
        [<CommonParameters>]

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-7.2

The block receives

When you specify the Native parameter, the script block must take the following parameters in the specified order. The names of the parameters aren't important because PowerShell passes in the values by position.

  • $wordToComplete (Position 0) - This parameter is set to value the user has provided before they pressed Tab. Your script block should use this value to determine tab completion values.
  • $commandAst (Position 1) - This parameter is set to the Abstract Syntax Tree (AST) for the current input line. For more information, see Ast Class.
  • $cursorPosition (Position 2) - This parameter is set to the position of the cursor when the user pressed Tab.

The block provides CompletionResult

The CompletionResult object allows you to provide additional details to each returned value:

  • completionText (String) - The text to be used as the auto completion result. This is the value sent to the command.
  • listItemText (String) - The text to be displayed in a list, such as when the user presses Ctrl+Space. This is used for display only and is not passed to the command when selected.
  • resultType (CompletionResultType) - The type of completion result.
  • toolTip (String) - The text for the tooltip with details to be displayed about the object. This is visible when the user selects an item after pressing Ctrl+Space.

So it seems like Powershell can fit within rust-driven completions and provide the full feature set.

@epage
Copy link
Member Author

epage commented Apr 22, 2022

I'm starting small and prototyping for just bash support. Looking at argcomplete, it seems they had to use their own shlex implementation. Hoping we can avoid that. The first step is comex/rust-shlex#12.

@epage epage added E-medium Call for participation: Experience needed to fix: Medium / intermediate and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Apr 27, 2022
@epage epage added the C-tracking-issue Category: A tracking issue for an unstable feature label Jun 16, 2022
@epage epage changed the title Leverage clap's parser for completions Stablize Rust-Native Completion Engine Trakcing Issue Jun 16, 2022
@epage epage changed the title Stablize Rust-Native Completion Engine Trakcing Issue Stablize Rust-Native Completion Engine Tracking Issue Jun 16, 2022
@epage epage added E-help-wanted Call for participation: Help is requested to fix this issue. 💸 $20 and removed 💸 $20 labels Jun 16, 2022
@happenslol
Copy link

Hi, I'm interested in helping with this this I'm developing a few personal tools using clap that would hugely benefit from dynamic completions. Is there any good issues that nobody is working on you could point me towards?

@epage
Copy link
Member Author

epage commented Jun 28, 2022

@happenslol Thanks!

Anything unchecked in the "Remaining work" section is up for grabs; just post here that you are getting started on it. The highest priority is the support for each shell as that will help gauge the feasibility and provide feedback on the API. The rest is flushing out parsing implementation.

I should split those out into individual issues to make it easier for people to coordinate on those (and to add bounties) but I probably won't have time for another week or so.

@happenslol
Copy link

Alright, had a busy week, so I'm only getting back around to this now.

I've looked at the work done in #3656, and it looks like the next steps to add zsh and fish support would be to pull some of the functionality out of clap_complete::dynamic::bash, and adapt it to the other shells to provide the same functionality. Am I correct in assuming that?

I'd look at argcomplete for the other implementations here, since the current one also seems to be coming from there.

@epage
Copy link
Member Author

epage commented Jul 3, 2022

@happenslol Yes, that is correct. I'd like to focus on shell support initially as that is where the most unknowns exist

@happenslol
Copy link

Started work here. I'm looking at cobra for zsh completions since argcomplete basically takes the easy way out and tells people to enable bash completion support for zsh 😅

@epage
Copy link
Member Author

epage commented Jul 11, 2022

Oh if cobra is doing the same type of dynamic completions as argcomplete, that is great! That'll serve as a much better example!

weihanglo added a commit to weihanglo/fish-shell that referenced this issue Aug 29, 2022
The old way of generating cargo completions no longer work, so we need
to manually maintain the completions until clap completions support[1].

[1]: clap-rs/clap#3166
mqudsi pushed a commit to fish-shell/fish-shell that referenced this issue Aug 31, 2022
The old way of generating cargo completions no longer work, so we need
to manually maintain the completions until clap completions support[1].

[1]: clap-rs/clap#3166
@blaggacao
Copy link

I want to apport a data point:

Implements cobra completion with advanced things like a fs cache for dynamic completion querys (e.g. with provenience from remote APIs) for an x amount of seconds.

Imagine a aws CLI wrapper that interacts with your custom infrastructure and things like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations C-tracking-issue Category: A tracking issue for an unstable feature E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

3 participants