Skip to content

Commit ac39e69

Browse files
committed
Auto merge of #14656 - shannmu:_cargo_publish_registry_completer, r=epage
feat: Add custom completer for completing registry name ### What does this PR try to resolve? Tracking issue #14520 Add custom completer for completing `cargo publish --registry <TAB>` and `cargo add --registry <TAB>`.
2 parents 42a774b + 245cba2 commit ac39e69

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/bin/cargo/commands/add.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ This is the catch all, handling hashes to named references in remote repositorie
144144
.long("registry")
145145
.action(ArgAction::Set)
146146
.value_name("NAME")
147-
.help("Package registry for this dependency"),
147+
.help("Package registry for this dependency")
148+
.add(clap_complete::ArgValueCandidates::new(|| {
149+
let candidates = get_registry_candidates();
150+
candidates.unwrap_or_default()
151+
})),
148152
])
149153
.next_help_heading("Section")
150154
.args([

src/cargo/util/command_prelude.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,12 @@ pub trait CommandExt: Sized {
386386
}
387387

388388
fn arg_registry(self, help: &'static str) -> Self {
389-
self._arg(opt("registry", help).value_name("REGISTRY"))
389+
self._arg(opt("registry", help).value_name("REGISTRY").add(
390+
clap_complete::ArgValueCandidates::new(|| {
391+
let candidates = get_registry_candidates();
392+
candidates.unwrap_or_default()
393+
}),
394+
))
390395
}
391396

392397
fn arg_index(self, help: &'static str) -> Self {
@@ -1063,6 +1068,21 @@ pub fn lockfile_path(
10631068
return Ok(Some(path));
10641069
}
10651070

1071+
pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1072+
let gctx = new_gctx_for_completions()?;
1073+
1074+
if let Ok(Some(registries)) =
1075+
gctx.get::<Option<HashMap<String, HashMap<String, String>>>>("registries")
1076+
{
1077+
Ok(registries
1078+
.keys()
1079+
.map(|name| clap_complete::CompletionCandidate::new(name.to_owned()))
1080+
.collect())
1081+
} else {
1082+
Ok(vec![])
1083+
}
1084+
}
1085+
10661086
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
10671087
get_targets_from_metadata()
10681088
.unwrap_or_default()

0 commit comments

Comments
 (0)