Skip to content

Commit c775f34

Browse files
committed
refactor(complete): Simplify crate completions
1 parent 35d05e8 commit c775f34

File tree

1 file changed

+31
-72
lines changed

1 file changed

+31
-72
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::core::compiler::{
44
BuildConfig, CompileKind, MessageFormat, RustcTargetData, TimingOutput,
55
};
66
use crate::core::resolver::{CliFeatures, ForceAllTargets, HasDevUnits};
7-
use crate::core::{Edition, Package, Target, TargetKind, Workspace, profiles::Profiles, shell};
7+
use crate::core::{Edition, Package, TargetKind, Workspace, profiles::Profiles, shell};
88
use crate::ops::lockfile::LOCKFILE_NAME;
99
use crate::ops::registry::RegistryOrIndex;
1010
use crate::ops::{self, CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
@@ -169,13 +169,17 @@ pub trait CommandExt: Sized {
169169
._arg(
170170
optional_multi_opt("test", "NAME", test)
171171
.help_heading(heading::TARGET_SELECTION)
172-
.add(clap_complete::ArgValueCandidates::new(get_test_candidates)),
172+
.add(clap_complete::ArgValueCandidates::new(|| {
173+
get_crate_candidates(TargetKind::Test).unwrap_or_default()
174+
})),
173175
)
174176
._arg(flag("benches", benches).help_heading(heading::TARGET_SELECTION))
175177
._arg(
176178
optional_multi_opt("bench", "NAME", bench)
177179
.help_heading(heading::TARGET_SELECTION)
178-
.add(clap_complete::ArgValueCandidates::new(get_bench_candidates)),
180+
.add(clap_complete::ArgValueCandidates::new(|| {
181+
get_crate_candidates(TargetKind::Bench).unwrap_or_default()
182+
})),
179183
)
180184
._arg(flag("all-targets", all).help_heading(heading::TARGET_SELECTION))
181185
}
@@ -193,15 +197,17 @@ pub trait CommandExt: Sized {
193197
._arg(
194198
optional_multi_opt("bin", "NAME", bin)
195199
.help_heading(heading::TARGET_SELECTION)
196-
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
200+
.add(clap_complete::ArgValueCandidates::new(|| {
201+
get_crate_candidates(TargetKind::Bin).unwrap_or_default()
202+
})),
197203
)
198204
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
199205
._arg(
200206
optional_multi_opt("example", "NAME", example)
201207
.help_heading(heading::TARGET_SELECTION)
202-
.add(clap_complete::ArgValueCandidates::new(
203-
get_example_candidates,
204-
)),
208+
.add(clap_complete::ArgValueCandidates::new(|| {
209+
get_crate_candidates(TargetKind::ExampleBin).unwrap_or_default()
210+
})),
205211
)
206212
}
207213

@@ -215,15 +221,17 @@ pub trait CommandExt: Sized {
215221
self._arg(
216222
optional_multi_opt("bin", "NAME", bin)
217223
.help_heading(heading::TARGET_SELECTION)
218-
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
224+
.add(clap_complete::ArgValueCandidates::new(|| {
225+
get_crate_candidates(TargetKind::Bin).unwrap_or_default()
226+
})),
219227
)
220228
._arg(flag("bins", bins).help_heading(heading::TARGET_SELECTION))
221229
._arg(
222230
optional_multi_opt("example", "NAME", example)
223231
.help_heading(heading::TARGET_SELECTION)
224-
.add(clap_complete::ArgValueCandidates::new(
225-
get_example_candidates,
226-
)),
232+
.add(clap_complete::ArgValueCandidates::new(|| {
233+
get_crate_candidates(TargetKind::ExampleBin).unwrap_or_default()
234+
})),
227235
)
228236
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION))
229237
}
@@ -232,14 +240,16 @@ pub trait CommandExt: Sized {
232240
self._arg(
233241
optional_multi_opt("bin", "NAME", bin)
234242
.help_heading(heading::TARGET_SELECTION)
235-
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
243+
.add(clap_complete::ArgValueCandidates::new(|| {
244+
get_crate_candidates(TargetKind::Bin).unwrap_or_default()
245+
})),
236246
)
237247
._arg(
238248
optional_multi_opt("example", "NAME", example)
239249
.help_heading(heading::TARGET_SELECTION)
240-
.add(clap_complete::ArgValueCandidates::new(
241-
get_example_candidates,
242-
)),
250+
.add(clap_complete::ArgValueCandidates::new(|| {
251+
get_crate_candidates(TargetKind::ExampleBin).unwrap_or_default()
252+
})),
243253
)
244254
}
245255

@@ -1210,70 +1220,19 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
12101220
Ok(feature_candidates)
12111221
}
12121222

1213-
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
1214-
get_targets_from_metadata()
1215-
.unwrap_or_default()
1216-
.into_iter()
1217-
.filter_map(|(pkg_name, target)| match target.kind() {
1218-
TargetKind::ExampleBin => Some(
1219-
clap_complete::CompletionCandidate::new(target.name())
1220-
.help(Some(format!("(from {})", pkg_name).into())),
1221-
),
1222-
_ => None,
1223-
})
1224-
.collect::<Vec<_>>()
1225-
}
1226-
1227-
fn get_bench_candidates() -> Vec<clap_complete::CompletionCandidate> {
1228-
get_targets_from_metadata()
1229-
.unwrap_or_default()
1230-
.into_iter()
1231-
.filter_map(|(pkg_name, target)| match target.kind() {
1232-
TargetKind::Bench => Some(
1233-
clap_complete::CompletionCandidate::new(target.name())
1234-
.help(Some(format!("(from {})", pkg_name).into())),
1235-
),
1236-
_ => None,
1237-
})
1238-
.collect::<Vec<_>>()
1239-
}
1240-
1241-
fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> {
1242-
get_targets_from_metadata()
1243-
.unwrap_or_default()
1244-
.into_iter()
1245-
.filter_map(|(pkg_name, target)| match target.kind() {
1246-
TargetKind::Test => Some(
1247-
clap_complete::CompletionCandidate::new(target.name())
1248-
.help(Some(format!("(from {})", pkg_name).into())),
1249-
),
1250-
_ => None,
1251-
})
1252-
.collect::<Vec<_>>()
1253-
}
1254-
1255-
fn get_bin_candidates() -> Vec<clap_complete::CompletionCandidate> {
1256-
get_targets_from_metadata()
1257-
.unwrap_or_default()
1258-
.into_iter()
1259-
.filter_map(|(pkg_name, target)| match target.kind() {
1260-
TargetKind::Bin => Some(
1261-
clap_complete::CompletionCandidate::new(target.name())
1262-
.help(Some(format!("(from {})", pkg_name).into())),
1263-
),
1264-
_ => None,
1265-
})
1266-
.collect::<Vec<_>>()
1267-
}
1268-
1269-
fn get_targets_from_metadata() -> CargoResult<Vec<(InternedString, Target)>> {
1223+
fn get_crate_candidates(kind: TargetKind) -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
12701224
let gctx = new_gctx_for_completions()?;
12711225

12721226
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?;
12731227

12741228
let targets = ws
12751229
.members()
12761230
.flat_map(|pkg| pkg.targets().into_iter().cloned().map(|t| (pkg.name(), t)))
1231+
.filter(|(_, target)| *target.kind() == kind)
1232+
.map(|(pkg_name, target)| {
1233+
clap_complete::CompletionCandidate::new(target.name())
1234+
.help(Some(format!("(from {})", pkg_name).into()))
1235+
})
12771236
.collect::<Vec<_>>();
12781237

12791238
Ok(targets)

0 commit comments

Comments
 (0)