Skip to content

Commit 083dc92

Browse files
authored
feat: Add custom completer for cargo +<TAB> to complete toolchain name (#15301)
<!-- Thanks for submitting a pull request 🎉! Here are some tips for you: * If this is your first contribution, read "Cargo Contribution Guide" first: https://doc.crates.io/contrib/ * Run `cargo fmt --all` to format your code changes. * Small commits and pull requests are always preferable and easy to review. * If your idea is large and needs feedback from the community, read how: https://doc.crates.io/contrib/process/#working-on-large-features * Cargo takes care of compatibility. Read our design principles: https://doc.crates.io/contrib/design.html * When changing help text of cargo commands, follow the steps to generate docs: https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages * If your PR is not finished, set it as "draft" PR or add "WIP" in its title. * It's ok to use the CI resources to test your PR, but please don't abuse them. ### What does this PR try to resolve? Explain the motivation behind this change. A clear overview along with an in-depth explanation are helpful. You can use `Fixes #<issue number>` to associate this PR to an existing issue. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. --> ### What does this PR try to resolve? Related to #14520 This PR introduces auto-completion for the `cargo +<TAB>` option. When a user types `cargo +<TAB>` and presses the TAB key, the system will automatically suggest toolchain in current device ### How should we test and review this PR? To verify this feature, follow these steps: 1. In the terminal, type `cargo +<TAB>` 2. Press the TAB key. 3. You should see toolchain suggestions https://github.com/user-attachments/assets/c8bd8a5e-e97d-4d0a-b284-1ed52734c4a2
2 parents 26a87d6 + 1cea458 commit 083dc92

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/bin/cargo/cli.rs

+23
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,32 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
691691
}))
692692
}).collect()
693693
})))
694+
.add(clap_complete::engine::SubcommandCandidates::new(|| {
695+
get_toolchains_from_rustup()
696+
.into_iter()
697+
.map(|t| clap_complete::CompletionCandidate::new(t))
698+
.collect()
699+
}))
694700
.subcommands(commands::builtin())
695701
}
696702

703+
fn get_toolchains_from_rustup() -> Vec<String> {
704+
let output = std::process::Command::new("rustup")
705+
.arg("toolchain")
706+
.arg("list")
707+
.arg("-q")
708+
.output()
709+
.unwrap();
710+
711+
if !output.status.success() {
712+
return vec![];
713+
}
714+
715+
let stdout = String::from_utf8(output.stdout).unwrap();
716+
717+
stdout.lines().map(|line| format!("+{}", line)).collect()
718+
}
719+
697720
#[test]
698721
fn verify_cli() {
699722
let gctx = GlobalContext::default().unwrap();

0 commit comments

Comments
 (0)