Skip to content

Commit 2a0af44

Browse files
committed
feat: Add custom completer for cargo <TAB> to complete aliases defined in config.toml
1 parent 68c26f6 commit 2a0af44

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/bin/cargo/cli.rs

+16
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ pub fn cli(gctx: &GlobalContext) -> Command {
571571
.invalid(style::INVALID)
572572
};
573573

574+
let alias_map = super::user_defined_aliases(gctx);
575+
574576
Command::new("cargo")
575577
// Subcommands all count their args' display order independently (from 0),
576578
// which makes their args interspersed with global args. This puts global args last.
@@ -697,6 +699,20 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
697699
.map(|t| clap_complete::CompletionCandidate::new(t))
698700
.collect()
699701
}))
702+
.add(clap_complete::engine::SubcommandCandidates::new(move || {
703+
alias_map.iter()
704+
.map(|(alias, cmd_info)| {
705+
let help_text = if let super::CommandInfo::Alias { target } = cmd_info {
706+
let cmd_str = target.iter().map(String::as_str).collect::<Vec<_>>().join(" ");
707+
format!("alias for {}", cmd_str)
708+
} else {
709+
"alias (from config)".to_string()
710+
};
711+
clap_complete::CompletionCandidate::new(alias.clone())
712+
.help(Some(help_text.into()))
713+
})
714+
.collect()
715+
}))
700716
.subcommands(commands::builtin())
701717
}
702718

0 commit comments

Comments
 (0)