From 09a472e32b651aab1b7d85649e7a4ced33456f02 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 12 Sep 2025 14:19:49 -0500 Subject: [PATCH] refactor(cli): Pull out completion context creation --- src/bin/cargo/cli.rs | 55 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index aba77bd96..27c5b76ae 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -703,7 +703,9 @@ See 'cargo help <>' for more information .into_iter() .map(|t| clap_complete::CompletionCandidate::new(t)) .collect::>(); - candidates.extend(get_alias_candidates()); + if let Ok(gctx) = new_gctx_for_completions() { + candidates.extend(get_alias_candidates(&gctx)); + } candidates })) .subcommands(commands::builtin()) @@ -726,33 +728,30 @@ fn get_toolchains_from_rustup() -> Vec { stdout.lines().map(|line| format!("+{}", line)).collect() } -fn get_alias_candidates() -> Vec { - 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::>() - .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() +fn get_alias_candidates(gctx: &GlobalContext) -> Vec { + let alias_map = user_defined_aliases(gctx); + 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::>() + .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() } #[test]