Skip to content

Commit

Permalink
Auto merge of #14548 - shannmu:_cargo_test_names, r=epage
Browse files Browse the repository at this point in the history
feat: Add custom completer for completing test names

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

Add custom completer for `cargo test --test <TAB>`
  • Loading branch information
bors committed Sep 17, 2024
2 parents 32f024f + f47e2a5 commit 25456ea
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ pub trait CommandExt: Sized {
) -> Self {
self.arg_targets_lib_bin_example(lib, bin, bins, example, examples)
._arg(flag("tests", tests).help_heading(heading::TARGET_SELECTION))
._arg(optional_multi_opt("test", "NAME", test).help_heading(heading::TARGET_SELECTION))
._arg(
optional_multi_opt("test", "NAME", test)
.help_heading(heading::TARGET_SELECTION)
.add(clap_complete::ArgValueCandidates::new(get_test_candidates)),
)
._arg(flag("benches", benches).help_heading(heading::TARGET_SELECTION))
._arg(
optional_multi_opt("bench", "NAME", bench).help_heading(heading::TARGET_SELECTION),
Expand Down Expand Up @@ -1042,6 +1046,17 @@ pub fn lockfile_path(
return Ok(Some(path));
}

fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> {
get_targets_from_metadata()
.unwrap_or_default()
.into_iter()
.filter_map(|target| match target.kind() {
TargetKind::Test => Some(clap_complete::CompletionCandidate::new(target.name())),
_ => None,
})
.collect::<Vec<_>>()
}

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

0 comments on commit 25456ea

Please sign in to comment.