Skip to content

Commit 25456ea

Browse files
committed
Auto merge of #14548 - shannmu:_cargo_test_names, r=epage
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>`
2 parents 32f024f + f47e2a5 commit 25456ea

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/cargo/util/command_prelude.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ pub trait CommandExt: Sized {
156156
) -> Self {
157157
self.arg_targets_lib_bin_example(lib, bin, bins, example, examples)
158158
._arg(flag("tests", tests).help_heading(heading::TARGET_SELECTION))
159-
._arg(optional_multi_opt("test", "NAME", test).help_heading(heading::TARGET_SELECTION))
159+
._arg(
160+
optional_multi_opt("test", "NAME", test)
161+
.help_heading(heading::TARGET_SELECTION)
162+
.add(clap_complete::ArgValueCandidates::new(get_test_candidates)),
163+
)
160164
._arg(flag("benches", benches).help_heading(heading::TARGET_SELECTION))
161165
._arg(
162166
optional_multi_opt("bench", "NAME", bench).help_heading(heading::TARGET_SELECTION),
@@ -1042,6 +1046,17 @@ pub fn lockfile_path(
10421046
return Ok(Some(path));
10431047
}
10441048

1049+
fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> {
1050+
get_targets_from_metadata()
1051+
.unwrap_or_default()
1052+
.into_iter()
1053+
.filter_map(|target| match target.kind() {
1054+
TargetKind::Test => Some(clap_complete::CompletionCandidate::new(target.name())),
1055+
_ => None,
1056+
})
1057+
.collect::<Vec<_>>()
1058+
}
1059+
10451060
fn get_bin_candidates() -> Vec<clap_complete::CompletionCandidate> {
10461061
get_targets_from_metadata()
10471062
.unwrap_or_default()

0 commit comments

Comments
 (0)