Skip to content

Commit a6de8c7

Browse files
committed
Auto merge of #14531 - shannmu:_cargo_example_names, r=epage
feat: Add custom completer for `cargo build --example=<TAB>` ### What does this PR try to resolve? Tracking issue #14520 Add custom completer for `cargo build --example=<TAB>`
2 parents 0461165 + 13c4c13 commit a6de8c7

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/cargo/util/command_prelude.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ pub trait CommandExt: Sized {
188188
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
189189
._arg(
190190
optional_multi_opt("example", "NAME", example)
191-
.help_heading(heading::TARGET_SELECTION),
191+
.help_heading(heading::TARGET_SELECTION)
192+
.add(clap_complete::ArgValueCandidates::new(
193+
get_example_candidates,
194+
)),
192195
)
193196
}
194197

@@ -206,7 +209,11 @@ pub trait CommandExt: Sized {
206209
)
207210
._arg(flag("bins", bins).help_heading(heading::TARGET_SELECTION))
208211
._arg(
209-
optional_multi_opt("example", "NAME", example).help_heading(heading::TARGET_SELECTION),
212+
optional_multi_opt("example", "NAME", example)
213+
.help_heading(heading::TARGET_SELECTION)
214+
.add(clap_complete::ArgValueCandidates::new(
215+
get_example_candidates,
216+
)),
210217
)
211218
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
212219
}
@@ -218,7 +225,11 @@ pub trait CommandExt: Sized {
218225
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
219226
)
220227
._arg(
221-
optional_multi_opt("example", "NAME", example).help_heading(heading::TARGET_SELECTION),
228+
optional_multi_opt("example", "NAME", example)
229+
.help_heading(heading::TARGET_SELECTION)
230+
.add(clap_complete::ArgValueCandidates::new(
231+
get_example_candidates,
232+
)),
222233
)
223234
}
224235

@@ -1048,6 +1059,17 @@ pub fn lockfile_path(
10481059
return Ok(Some(path));
10491060
}
10501061

1062+
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
1063+
get_targets_from_metadata()
1064+
.unwrap_or_default()
1065+
.into_iter()
1066+
.filter_map(|target| match target.kind() {
1067+
TargetKind::ExampleBin => Some(clap_complete::CompletionCandidate::new(target.name())),
1068+
_ => None,
1069+
})
1070+
.collect::<Vec<_>>()
1071+
}
1072+
10511073
fn get_bench_candidates() -> Vec<clap_complete::CompletionCandidate> {
10521074
get_targets_from_metadata()
10531075
.unwrap_or_default()

0 commit comments

Comments
 (0)