Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add custom completer for completing installed binaries #14534

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we change this to

Suggested change
|| get_installed_crates(),
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));
epage marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
Loading