Skip to content

Commit 7f266e9

Browse files
committed
feat: Add custom completer for completing installed binaries
1 parent e7ca9be commit 7f266e9

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/bin/cargo/commands/uninstall.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ use cargo::ops;
55
pub fn cli() -> Command {
66
subcommand("uninstall")
77
.about("Remove a Rust binary")
8-
.arg(Arg::new("spec").value_name("SPEC").num_args(0..))
8+
.arg(
9+
Arg::new("spec")
10+
.value_name("SPEC")
11+
.num_args(0..)
12+
.add::<clap_complete::ArgValueCandidates>(clap_complete::ArgValueCandidates::new(
13+
|| get_installed_crates(),
14+
)),
15+
)
916
.arg(opt("root", "Directory to uninstall packages from").value_name("DIR"))
1017
.arg_silent_suggestion()
1118
.arg_package_spec_simple("Package to uninstall")
@@ -37,3 +44,25 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
3744
ops::uninstall(root, specs, &values(args, "bin"), gctx)?;
3845
Ok(())
3946
}
47+
48+
fn get_installed_crates() -> Vec<clap_complete::CompletionCandidate> {
49+
get_installed_crates_().unwrap_or_default()
50+
}
51+
52+
fn get_installed_crates_() -> Option<Vec<clap_complete::CompletionCandidate>> {
53+
let mut candidates = Vec::new();
54+
55+
let gctx = GlobalContext::default().ok()?;
56+
57+
let root = ops::resolve_root(None, &gctx).ok()?;
58+
59+
let tracker = ops::InstallTracker::load(&gctx, &root).ok()?;
60+
61+
for (_, v) in tracker.all_installed_bins() {
62+
for bin in v {
63+
candidates.push(clap_complete::CompletionCandidate::new(bin));
64+
}
65+
}
66+
67+
Some(candidates)
68+
}

src/cargo/ops/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use self::cargo_update::update_lockfile;
2222
pub use self::cargo_update::upgrade_manifests;
2323
pub use self::cargo_update::write_manifest_upgrades;
2424
pub use self::cargo_update::UpdateOptions;
25+
pub use self::common_for_install_and_uninstall::{resolve_root, InstallTracker};
2526
pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions};
2627
pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile};
2728
pub use self::registry::info;

0 commit comments

Comments
 (0)