Skip to content

Commit d2dc2f8

Browse files
committed
resolved conflict
1 parent 2a0af44 commit d2dc2f8

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

src/bin/cargo/cli.rs

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

574-
let alias_map = super::user_defined_aliases(gctx);
575-
576574
Command::new("cargo")
577575
// Subcommands all count their args' display order independently (from 0),
578576
// which makes their args interspersed with global args. This puts global args last.
@@ -693,25 +691,13 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
693691
}))
694692
}).collect()
695693
})))
696-
.add(clap_complete::engine::SubcommandCandidates::new(|| {
697-
get_toolchains_from_rustup()
694+
.add(clap_complete::engine::SubcommandCandidates::new(move || {
695+
let mut candidates = get_toolchains_from_rustup()
698696
.into_iter()
699697
.map(|t| clap_complete::CompletionCandidate::new(t))
700-
.collect()
701-
}))
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()
698+
.collect::<Vec<_>>();
699+
candidates.extend(crate::util::command_prelude::get_alias_candidates());
700+
candidates
715701
}))
716702
.subcommands(commands::builtin())
717703
}

src/bin/cargo/main.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn list_commands(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
205205
}
206206

207207
// Add the user-defined aliases
208-
let alias_commands = user_defined_aliases(gctx);
208+
let alias_commands = command_prelude::user_defined_aliases(gctx);
209209
commands.extend(alias_commands);
210210

211211
// `help` is special, so it needs to be inserted separately.
@@ -249,22 +249,6 @@ fn third_party_subcommands(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo
249249
}
250250
commands
251251
}
252-
253-
fn user_defined_aliases(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
254-
let mut commands = BTreeMap::new();
255-
if let Ok(aliases) = gctx.get::<BTreeMap<String, StringOrVec>>("alias") {
256-
for (name, target) in aliases.iter() {
257-
commands.insert(
258-
name.to_string(),
259-
CommandInfo::Alias {
260-
target: target.clone(),
261-
},
262-
);
263-
}
264-
}
265-
commands
266-
}
267-
268252
fn find_external_subcommand(gctx: &GlobalContext, cmd: &str) -> Option<PathBuf> {
269253
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
270254
search_directories(gctx)

src/cargo/util/command_prelude.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use cargo_util_schemas::manifest::StringOrVec;
2424
use clap::builder::UnknownArgumentValueParser;
2525
use home::cargo_home_with_cwd;
2626
use semver::Version;
27-
use std::collections::HashMap;
27+
use std::collections::{BTreeMap, HashMap};
2828
use std::ffi::{OsStr, OsString};
2929
use std::path::Path;
3030
use std::path::PathBuf;
@@ -1370,6 +1370,44 @@ fn new_gctx_for_completions() -> CargoResult<GlobalContext> {
13701370
Ok(gctx)
13711371
}
13721372

1373+
pub fn get_alias_candidates() -> Vec<clap_complete::CompletionCandidate> {
1374+
if let Ok(gctx) = new_gctx_for_completions() {
1375+
let alias_map = user_defined_aliases(&gctx);
1376+
return alias_map
1377+
.iter()
1378+
.map(|(alias, cmd_info)| {
1379+
let help_text = if let CommandInfo::Alias { target } = cmd_info {
1380+
let cmd_str = target
1381+
.iter()
1382+
.map(String::as_str)
1383+
.collect::<Vec<_>>()
1384+
.join(" ");
1385+
format!("alias for {}", cmd_str)
1386+
} else {
1387+
"alias (from config)".to_string()
1388+
};
1389+
clap_complete::CompletionCandidate::new(alias.clone()).help(Some(help_text.into()))
1390+
})
1391+
.collect();
1392+
}
1393+
Vec::new()
1394+
}
1395+
1396+
pub fn user_defined_aliases(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
1397+
let mut commands = BTreeMap::new();
1398+
if let Ok(aliases) = gctx.get::<BTreeMap<String, StringOrVec>>("alias") {
1399+
for (name, target) in aliases.iter() {
1400+
commands.insert(
1401+
name.to_string(),
1402+
CommandInfo::Alias {
1403+
target: target.clone(),
1404+
},
1405+
);
1406+
}
1407+
}
1408+
commands
1409+
}
1410+
13731411
#[track_caller]
13741412
pub fn ignore_unknown<T: Default>(r: Result<T, clap::parser::MatchesError>) -> T {
13751413
match r {

0 commit comments

Comments
 (0)