Skip to content

Commit

Permalink
Auto merge of rust-lang#14534 - shannmu:_cargo_installed_crates, r=epage
Browse files Browse the repository at this point in the history
feat: Add custom completer for completing installed binaries

### What does this PR try to resolve?
Tracking issue rust-lang#14520

Add custom completer for `cargo uninstall <TAB>`
  • Loading branch information
bors committed Sep 16, 2024
2 parents 7984efa + 954581b commit 654a433
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/bin/cargo/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use cargo::ops;
pub fn cli() -> Command {
subcommand("uninstall")
.about("Remove a Rust binary")
.arg(Arg::new("spec").value_name("SPEC").num_args(0..))
.arg(
Arg::new("spec")
.value_name("SPEC")
.num_args(0..)
.add::<clap_complete::ArgValueCandidates>(clap_complete::ArgValueCandidates::new(
|| get_installed_crates(),
)),
)
.arg(opt("root", "Directory to uninstall packages from").value_name("DIR"))
.arg_silent_suggestion()
.arg_package_spec_simple("Package to uninstall")
Expand Down Expand Up @@ -37,3 +44,25 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
ops::uninstall(root, specs, &values(args, "bin"), gctx)?;
Ok(())
}

fn get_installed_crates() -> Vec<clap_complete::CompletionCandidate> {
get_installed_crates_().unwrap_or_default()
}

fn get_installed_crates_() -> Option<Vec<clap_complete::CompletionCandidate>> {
let mut candidates = Vec::new();

let gctx = GlobalContext::default().ok()?;

let root = ops::resolve_root(None, &gctx).ok()?;

let tracker = ops::InstallTracker::load(&gctx, &root).ok()?;

for (_, v) in tracker.all_installed_bins() {
for bin in v {
candidates.push(clap_complete::CompletionCandidate::new(bin));
}
}

Some(candidates)
}
1 change: 1 addition & 0 deletions src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use self::cargo_update::update_lockfile;
pub use self::cargo_update::upgrade_manifests;
pub use self::cargo_update::write_manifest_upgrades;
pub use self::cargo_update::UpdateOptions;
pub use self::common_for_install_and_uninstall::{resolve_root, InstallTracker};
pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions};
pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile};
pub use self::registry::info;
Expand Down

0 comments on commit 654a433

Please sign in to comment.