Skip to content

Commit 6d215f4

Browse files
committed
feat: Add custom completer for cargo <TAB> to complete aliases defined in config.toml
1 parent 437ff04 commit 6d215f4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/bin/cargo/cli.rs

Lines changed: 16 additions & 0 deletions
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.
@@ -691,6 +693,20 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
691693
}))
692694
}).collect()
693695
})))
696+
.add(clap_complete::engine::SubcommandCandidates::new(move || {
697+
alias_map.iter()
698+
.map(|(alias, cmd_info)| {
699+
let help_text = if let super::CommandInfo::Alias { target } = cmd_info {
700+
let cmd_str = target.iter().map(String::as_str).collect::<Vec<_>>().join(" ");
701+
format!("alias for {}", cmd_str)
702+
} else {
703+
"alias (from config)".to_string()
704+
};
705+
clap_complete::CompletionCandidate::new(alias.clone())
706+
.help(Some(help_text.into()))
707+
})
708+
.collect()
709+
}))
694710
.subcommands(commands::builtin())
695711
}
696712

0 commit comments

Comments
 (0)