Skip to content

Commit b672b30

Browse files
authored
feat(cli): Allow completions for third-party subcommand names (#15961)
### What does this PR try to resolve? This will complete the name for third-party subcommands, like `cargo-release`. This does not complete their args This is part of #14520. ### How to test and review this PR?
2 parents 9733318 + e64e8aa commit b672b30

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/bin/cargo/cli.rs

Lines changed: 29 additions & 28 deletions
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::third_party_subcommands;
1415
use super::user_defined_aliases;
1516
use crate::command_prelude::*;
1617
use crate::util::is_rustup;
@@ -703,7 +704,9 @@ See '<bright-cyan,bold>cargo help</> <cyan><<command>></>' for more information
703704
.into_iter()
704705
.map(|t| clap_complete::CompletionCandidate::new(t))
705706
.collect::<Vec<_>>();
706-
candidates.extend(get_alias_candidates());
707+
if let Ok(gctx) = new_gctx_for_completions() {
708+
candidates.extend(get_command_candidates(&gctx));
709+
}
707710
candidates
708711
}))
709712
.subcommands(commands::builtin())
@@ -726,33 +729,31 @@ fn get_toolchains_from_rustup() -> Vec<String> {
726729
stdout.lines().map(|line| format!("+{}", line)).collect()
727730
}
728731

729-
fn get_alias_candidates() -> Vec<clap_complete::CompletionCandidate> {
730-
if let Ok(gctx) = new_gctx_for_completions() {
731-
let alias_map = user_defined_aliases(&gctx);
732-
return alias_map
733-
.iter()
734-
.map(|(alias, cmd_info)| {
735-
let help_text = match cmd_info {
736-
CommandInfo::Alias { target } => {
737-
let cmd_str = target
738-
.iter()
739-
.map(String::as_str)
740-
.collect::<Vec<_>>()
741-
.join(" ");
742-
format!("alias for {}", cmd_str)
743-
}
744-
CommandInfo::BuiltIn { .. } => {
745-
unreachable!("BuiltIn command shouldn't appear in alias map")
746-
}
747-
CommandInfo::External { .. } => {
748-
unreachable!("External command shouldn't appear in alias map")
749-
}
750-
};
751-
clap_complete::CompletionCandidate::new(alias.clone()).help(Some(help_text.into()))
752-
})
753-
.collect();
754-
}
755-
Vec::new()
732+
fn get_command_candidates(gctx: &GlobalContext) -> Vec<clap_complete::CompletionCandidate> {
733+
let mut commands = user_defined_aliases(gctx);
734+
commands.extend(third_party_subcommands(gctx));
735+
commands
736+
.iter()
737+
.map(|(name, cmd_info)| {
738+
let help_text = match cmd_info {
739+
CommandInfo::Alias { target } => {
740+
let cmd_str = target
741+
.iter()
742+
.map(String::as_str)
743+
.collect::<Vec<_>>()
744+
.join(" ");
745+
format!("alias for {}", cmd_str)
746+
}
747+
CommandInfo::BuiltIn { .. } => {
748+
unreachable!("BuiltIn command shouldn't appear in alias map")
749+
}
750+
CommandInfo::External { path } => {
751+
format!("from {}", path.display())
752+
}
753+
};
754+
clap_complete::CompletionCandidate::new(name.clone()).help(Some(help_text.into()))
755+
})
756+
.collect()
756757
}
757758

758759
#[test]

0 commit comments

Comments
 (0)