Skip to content

Commit

Permalink
feat: Add custom completer for completing example names
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu committed Sep 18, 2024
1 parent 0461165 commit 13c4c13
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ pub trait CommandExt: Sized {
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
._arg(
optional_multi_opt("example", "NAME", example)
.help_heading(heading::TARGET_SELECTION),
.help_heading(heading::TARGET_SELECTION)
.add(clap_complete::ArgValueCandidates::new(
get_example_candidates,
)),
)
}

Expand All @@ -206,7 +209,11 @@ pub trait CommandExt: Sized {
)
._arg(flag("bins", bins).help_heading(heading::TARGET_SELECTION))
._arg(
optional_multi_opt("example", "NAME", example).help_heading(heading::TARGET_SELECTION),
optional_multi_opt("example", "NAME", example)
.help_heading(heading::TARGET_SELECTION)
.add(clap_complete::ArgValueCandidates::new(
get_example_candidates,
)),
)
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
}
Expand All @@ -218,7 +225,11 @@ pub trait CommandExt: Sized {
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
)
._arg(
optional_multi_opt("example", "NAME", example).help_heading(heading::TARGET_SELECTION),
optional_multi_opt("example", "NAME", example)
.help_heading(heading::TARGET_SELECTION)
.add(clap_complete::ArgValueCandidates::new(
get_example_candidates,
)),
)
}

Expand Down Expand Up @@ -1048,6 +1059,17 @@ pub fn lockfile_path(
return Ok(Some(path));
}

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

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

0 comments on commit 13c4c13

Please sign in to comment.