Skip to content

feat: Add custom completer for cargo <TAB> to complete aliases defined in config.toml #15319

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Write;

use super::commands;
use super::list_commands;
use super::user_defined_aliases;
use crate::command_prelude::*;
use crate::util::is_rustup;
use cargo::core::shell::ColorChoice;
Expand Down Expand Up @@ -691,11 +692,13 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
}))
}).collect()
})))
.add(clap_complete::engine::SubcommandCandidates::new(|| {
get_toolchains_from_rustup()
.add(clap_complete::engine::SubcommandCandidates::new(move || {
let mut candidates = get_toolchains_from_rustup()
.into_iter()
.map(|t| clap_complete::CompletionCandidate::new(t))
.collect()
.collect::<Vec<_>>();
candidates.extend(get_alias_candidates());
candidates
}))
.subcommands(commands::builtin())
}
Expand All @@ -717,6 +720,35 @@ fn get_toolchains_from_rustup() -> Vec<String> {
stdout.lines().map(|line| format!("+{}", line)).collect()
}

fn get_alias_candidates() -> Vec<clap_complete::CompletionCandidate> {
if let Ok(gctx) = new_gctx_for_completions() {
let alias_map = user_defined_aliases(&gctx);
return alias_map
.iter()
.map(|(alias, cmd_info)| {
let help_text = match cmd_info {
CommandInfo::Alias { target } => {
let cmd_str = target
.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.join(" ");
format!("alias for {}", cmd_str)
}
CommandInfo::BuiltIn { .. } => {
unreachable!("BuiltIn command shouldn't appear in alias map")
}
CommandInfo::External { .. } => {
unreachable!("External command shouldn't appear in alias map")
}
};
clap_complete::CompletionCandidate::new(alias.clone()).help(Some(help_text.into()))
})
.collect();
}
Vec::new()
}

#[test]
fn verify_cli() {
let gctx = GlobalContext::default().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ fn get_packages() -> CargoResult<Vec<Package>> {
Ok(packages)
}

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

Expand Down