diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 253de9a9692..dd166d14778 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -15,7 +15,9 @@ use super::list_commands; use super::third_party_subcommands; use super::user_defined_aliases; use crate::command_prelude::*; +use crate::util::important_paths::find_root_manifest_for_wd; use crate::util::is_rustup; +use cargo::core::Workspace; use cargo::core::shell::ColorChoice; use cargo::util::style; @@ -729,6 +731,7 @@ See 'cargo help <>' for more information .collect::>(); if let Ok(gctx) = new_gctx_for_completions() { candidates.extend(get_command_candidates(&gctx)); + candidates.extend(get_script_candidates(&gctx)); } candidates })) @@ -778,7 +781,35 @@ fn get_command_candidates(gctx: &GlobalContext) -> Vec Vec { + get_script_candidates_(gctx).unwrap_or_default() +} +fn get_script_candidates_( + gctx: &GlobalContext, +) -> CargoResult> { + let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, gctx)?; + + let Some(pkg) = ws.current_opt() else { + return Ok(vec![]); + }; + let mut candidates = Vec::new(); + + if let Some(metadata) = pkg.manifest().custom_metadata() { + if let Some(scripts) = metadata.get("scripts") { + if let Some(table) = scripts.as_table() { + for name in table.keys() { + candidates.push( + clap_complete::CompletionCandidate::new(name.clone()) + .help(Some("script".into())), + ); + } + } + } + } + + Ok(candidates) +} #[test] fn verify_cli() { let gctx = GlobalContext::default().unwrap();