Skip to content

Commit

Permalink
feat: Add custom completer for completing test names
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu committed Sep 17, 2024
1 parent 75ab4e5 commit f47e2a5
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 @@ -1041,6 +1045,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 f47e2a5

Please sign in to comment.