Skip to content

Commit

Permalink
Auto merge of #14656 - shannmu:_cargo_publish_registry_completer, r=e…
Browse files Browse the repository at this point in the history
…page

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>`.
  • Loading branch information
bors committed Oct 8, 2024
2 parents 42a774b + 245cba2 commit ac39e69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ This is the catch all, handling hashes to named references in remote repositorie
.long("registry")
.action(ArgAction::Set)
.value_name("NAME")
.help("Package registry for this dependency"),
.help("Package registry for this dependency")
.add(clap_complete::ArgValueCandidates::new(|| {
let candidates = get_registry_candidates();
candidates.unwrap_or_default()
})),
])
.next_help_heading("Section")
.args([
Expand Down
22 changes: 21 additions & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ pub trait CommandExt: Sized {
}

fn arg_registry(self, help: &'static str) -> Self {
self._arg(opt("registry", help).value_name("REGISTRY"))
self._arg(opt("registry", help).value_name("REGISTRY").add(
clap_complete::ArgValueCandidates::new(|| {
let candidates = get_registry_candidates();
candidates.unwrap_or_default()
}),
))
}

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

pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
let gctx = new_gctx_for_completions()?;

if let Ok(Some(registries)) =
gctx.get::<Option<HashMap<String, HashMap<String, String>>>>("registries")
{
Ok(registries
.keys()
.map(|name| clap_complete::CompletionCandidate::new(name.to_owned()))
.collect())
} else {
Ok(vec![])
}
}

fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
get_targets_from_metadata()
.unwrap_or_default()
Expand Down

0 comments on commit ac39e69

Please sign in to comment.