Skip to content

Commit 307cbfd

Browse files
authored
feat: Add custom completer for cargo <TAB> to complete aliases defined in config.toml (#15319)
<!-- 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 aliases defined in `config.toml` ### 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 aliases suggestions https://github.com/user-attachments/assets/d8a265e8-bbd9-4f58-8121-80caf142d8a6
2 parents 7440762 + 7e98310 commit 307cbfd

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/bin/cargo/cli.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt::Write;
1111

1212
use super::commands;
1313
use super::list_commands;
14+
use super::user_defined_aliases;
1415
use crate::command_prelude::*;
1516
use crate::util::is_rustup;
1617
use cargo::core::shell::ColorChoice;
@@ -693,11 +694,13 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
693694
}))
694695
}).collect()
695696
})))
696-
.add(clap_complete::engine::SubcommandCandidates::new(|| {
697-
get_toolchains_from_rustup()
697+
.add(clap_complete::engine::SubcommandCandidates::new(move || {
698+
let mut candidates = get_toolchains_from_rustup()
698699
.into_iter()
699700
.map(|t| clap_complete::CompletionCandidate::new(t))
700-
.collect()
701+
.collect::<Vec<_>>();
702+
candidates.extend(get_alias_candidates());
703+
candidates
701704
}))
702705
.subcommands(commands::builtin())
703706
}
@@ -719,6 +722,35 @@ fn get_toolchains_from_rustup() -> Vec<String> {
719722
stdout.lines().map(|line| format!("+{}", line)).collect()
720723
}
721724

725+
fn get_alias_candidates() -> Vec<clap_complete::CompletionCandidate> {
726+
if let Ok(gctx) = new_gctx_for_completions() {
727+
let alias_map = user_defined_aliases(&gctx);
728+
return alias_map
729+
.iter()
730+
.map(|(alias, cmd_info)| {
731+
let help_text = match cmd_info {
732+
CommandInfo::Alias { target } => {
733+
let cmd_str = target
734+
.iter()
735+
.map(String::as_str)
736+
.collect::<Vec<_>>()
737+
.join(" ");
738+
format!("alias for {}", cmd_str)
739+
}
740+
CommandInfo::BuiltIn { .. } => {
741+
unreachable!("BuiltIn command shouldn't appear in alias map")
742+
}
743+
CommandInfo::External { .. } => {
744+
unreachable!("External command shouldn't appear in alias map")
745+
}
746+
};
747+
clap_complete::CompletionCandidate::new(alias.clone()).help(Some(help_text.into()))
748+
})
749+
.collect();
750+
}
751+
Vec::new()
752+
}
753+
722754
#[test]
723755
fn verify_cli() {
724756
let gctx = GlobalContext::default().unwrap();

src/cargo/util/command_prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ fn get_packages() -> CargoResult<Vec<Package>> {
13531353
Ok(packages)
13541354
}
13551355

1356-
fn new_gctx_for_completions() -> CargoResult<GlobalContext> {
1356+
pub fn new_gctx_for_completions() -> CargoResult<GlobalContext> {
13571357
let cwd = std::env::current_dir()?;
13581358
let mut gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?);
13591359

0 commit comments

Comments
 (0)