Skip to content

Commit f47e2a5

Browse files
committed
feat: Add custom completer for completing test names
1 parent 75ab4e5 commit f47e2a5

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),
@@ -1041,6 +1045,17 @@ pub fn lockfile_path(
10411045
return Ok(Some(path));
10421046
}
10431047

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

0 commit comments

Comments
 (0)