-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Proposal: Better shell completions #6645
Comments
I don't know how this works at all, but why do some tool's shell completions just work (e.g., Git) whereas the Rust tools seem to need some kind of opt-in step? Of course, I think it would be nice if we didn't need this opt-in step. |
It depends on how rust/cargo is installed. Some installers (like ubuntu) will automatically add the completion file to the global location (such as |
FWIW, I just started learning Rust (cargo), using rustup, and this was pretty much the first thing I hit. 😄 If we want to keep stuff in the user's home directory (as opposed to e.g.
I use said approach with kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion |
@ehuss, had you considered looking at how to include something like this in clap (or as an add-on to clap) rather than directly in cargo? |
@scooter-dangle In a sense, it will be a clap addon, because it will probably be looking at the (hidden) clap internals to infer the options. My impression is that clap development has stalled and v3 has a long ways to go, so I don't think it is something we can use in the foreseeable future, and there's no indication how new features like this would make progress. There is an issue for this (clap-rs/clap#810), but there's no sense from the issue if it will support custom extensions or how it will work. |
I used Related issue: rust-lang/rustup#1821 |
See more: |
Is it possible to reduce cargo start time or optionally use generated completions as it is faster only a one-time cost? |
I'm not sure what you mean here. Cargo should launch in about 25ms, depending on your system. I think that should be plenty fast for completion. |
As a workaround, for now I just added a symlink to the installed completions for my current toolchain, in my local completions. On Linux, stable toolchain:
This will work for a specific toolchain, but the file seems to be a copy of cargo.bashcomp.sh, so I guess it doesn't matter too much. |
I would like to add an enhancement to the original feature request. Interactive fuzzy completionsI got this idea after writing custom fzf wrappers over cmake like this one: Once I wrote this bash function, i could drop into any new cmake project, run my since cargo already provides a uniform way to build any rust projects, I think it would be great to enable developers to interactively select the build target. user storycargo build --example <TAB><TAB>
# a fuzzy finder window appears with all buildable examples as defined in Cargo.toml or examples/
cargo build --bin i_already_know_the_name --features <TAB><TAB>
# a fuzzy finder window appears with all available features to complete
cargo bench <TAB><TAB>
# a fuzzy finder window with all benchmarks appears Required building blocks2 components are required - quickly producing a list of relevant targets (filtered by type) and exposing a fuzzy interface to the user. Query cargo for all targets to complete withBazel and buck build systems have Add a fuzzy completion interfaceThe fzf-like rust crate is called skim and it provides us with the primitives to expose a fuzzy finder to the user. https://github.com/lotabout/skim Once this exists, the bash completion functions can call the corresponding cargo fuzzy-picker depending on the tokens already typed on the command line. Prior Art/Other build systemsAs i was writing this, I found this open feature request on the bazel repo. Great minds think alike! Under-exploredSince cargo enables people to write their own plugins, like cargo fuzz, the target types defined by those need to be made available to original cargo, so they can be listed as completion options. I am not sure how that would work. |
If cargo support this, it would be better. ps: as a gopher, I have used https://github.com/spf13/cobra to build command line tools for a long time, which provides a basic support for generating completion script for bash, zsh, powershell, etc. It works like |
FYI: Clap generates completions like that ( |
Relevant clap issues:
|
It seems weirdly inconsistent that For those arriving in the future, the work around to enable these manually (Tested Ubuntu 22.04/20.04) is: |
How will this handle security with respect to path specifications in rust-toolchain.toml? It would be pretty bad if doing a cargo command completion inside a malicious repo would cause arbitrary code execution. And even for a non-malicious repo have to wait for rustup to download the toolchain if it isn't already when trying to get completions is annoying. Will the shell completions ensure that they invoke the cargo from which they were generated without going through the rustup wrapper? |
I don't think that will be a change from the status quo. The existing completions may call cargo or rustup, depending on the argument. |
#14084 proposes an idea that the completion script could learn available learn options from |
Does that include aliases? It would be convenient if completions were alias-aware |
I'd like to be alias-aware but not going to block the work on it as it adds a lot of extra complexity to clap_complete as we now need cargo to interject itself directly into the parsing process. |
feat: Add native comlpetion with CompleteEnv under the nightly ### What does this PR try to resolve? Related issue #6645 Tracking issue #14520 This PR is the first step to move cargo shell completions to native completions by using `clap_complete` crate. It makes users could complete cargo subcommand and flags. By using `clap_complete` crate, we could extend the supported shells to Bash, Zsh, Elvish, Fish, and PowerShell. However, at the current stage, the support for PowerShell in `clap_complete` is not fully developed. See clap-rs/clap#3166 to get more context about what features `clap_complete` has supported. ### How to test and review this PR? 1. Build a test environment, including the necessary short completion scripts, and the `complete` function to start an interactive shell with the help of a pty device and obtain completion results. 2. Simply test the completion results of subcommands in bash, zsh, fish, elvish.
This is a proposal to add better shell completion support to Cargo. The outline of the strategy is:
cargo completions
subcommand to assist with completion support.cargo completions <shell>
will output a completion script for the given shell (bash, zsh, etc.).cargo completions
to implement the work of actually emitting completions. This can significantly simplify the work of supporting completions for different shells. The completion script is very small (seenpm completion
for an example). This also means all the logic can be written in one language (Rust) and can be more easily tested.Benefits:
--bin
or--example
completing the target name).Drawbacks:
Please let me know if you have any thoughts or objections.
The text was updated successfully, but these errors were encountered: