Skip to content

Commit 7f8ed80

Browse files
authored
fix(complete): Show local crates/features over other members (#15956)
### What does this PR try to resolve? Our options are: - Not offer these completions - Hide them - De-prioritize them Since someone could be using `--package`, I felt de-prioritizing would give the best experience. This is a follow up to #15309 ### How to test and review this PR? Along the way, I made things more consistent across completions
2 parents 506023f + 7294675 commit 7f8ed80

File tree

1 file changed

+54
-77
lines changed

1 file changed

+54
-77
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 54 additions & 77 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

@@ -253,8 +263,7 @@ pub trait CommandExt: Sized {
253263
.short('F')
254264
.help_heading(heading::FEATURE_SELECTION)
255265
.add(clap_complete::ArgValueCandidates::new(|| {
256-
let candidates = get_feature_candidates();
257-
candidates.unwrap_or_default()
266+
get_feature_candidates().unwrap_or_default()
258267
})),
259268
)
260269
._arg(
@@ -414,10 +423,7 @@ pub trait CommandExt: Sized {
414423
.value_name("VCS")
415424
.value_parser(["git", "hg", "pijul", "fossil", "none"]),
416425
)
417-
._arg(
418-
flag("bin", "Use a binary (application) template [default]")
419-
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)),
420-
)
426+
._arg(flag("bin", "Use a binary (application) template [default]"))
421427
._arg(flag("lib", "Use a library template"))
422428
._arg(
423429
opt("edition", "Edition to set for the crate generated")
@@ -1194,8 +1200,8 @@ fn default_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
11941200

11951201
fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
11961202
let gctx = new_gctx_for_completions()?;
1197-
let manifest_path = find_root_manifest_for_wd(gctx.cwd())?;
1198-
let ws = Workspace::new(&manifest_path, &gctx)?;
1203+
1204+
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?;
11991205
let mut feature_candidates = Vec::new();
12001206

12011207
// Process all packages in the workspace
@@ -1204,8 +1210,14 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
12041210

12051211
// Add direct features with package info
12061212
for feature_name in package.summary().features().keys() {
1213+
let order = if ws.current_opt().map(|p| p.name()) == Some(package_name) {
1214+
0
1215+
} else {
1216+
1
1217+
};
12071218
feature_candidates.push(
12081219
clap_complete::CompletionCandidate::new(feature_name)
1220+
.display_order(Some(order))
12091221
.help(Some(format!("(from {})", package_name).into())),
12101222
);
12111223
}
@@ -1214,60 +1226,25 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
12141226
Ok(feature_candidates)
12151227
}
12161228

1217-
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
1218-
get_targets_from_metadata()
1219-
.unwrap_or_default()
1220-
.into_iter()
1221-
.filter_map(|target| match target.kind() {
1222-
TargetKind::ExampleBin => Some(clap_complete::CompletionCandidate::new(target.name())),
1223-
_ => None,
1224-
})
1225-
.collect::<Vec<_>>()
1226-
}
1227-
1228-
fn get_bench_candidates() -> Vec<clap_complete::CompletionCandidate> {
1229-
get_targets_from_metadata()
1230-
.unwrap_or_default()
1231-
.into_iter()
1232-
.filter_map(|target| match target.kind() {
1233-
TargetKind::Bench => Some(clap_complete::CompletionCandidate::new(target.name())),
1234-
_ => None,
1235-
})
1236-
.collect::<Vec<_>>()
1237-
}
1229+
fn get_crate_candidates(kind: TargetKind) -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1230+
let gctx = new_gctx_for_completions()?;
12381231

1239-
fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> {
1240-
get_targets_from_metadata()
1241-
.unwrap_or_default()
1242-
.into_iter()
1243-
.filter_map(|target| match target.kind() {
1244-
TargetKind::Test => Some(clap_complete::CompletionCandidate::new(target.name())),
1245-
_ => None,
1246-
})
1247-
.collect::<Vec<_>>()
1248-
}
1232+
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?;
12491233

1250-
fn get_bin_candidates() -> Vec<clap_complete::CompletionCandidate> {
1251-
get_targets_from_metadata()
1252-
.unwrap_or_default()
1253-
.into_iter()
1254-
.filter_map(|target| match target.kind() {
1255-
TargetKind::Bin => Some(clap_complete::CompletionCandidate::new(target.name())),
1256-
_ => None,
1234+
let targets = ws
1235+
.members()
1236+
.flat_map(|pkg| pkg.targets().into_iter().cloned().map(|t| (pkg.name(), t)))
1237+
.filter(|(_, target)| *target.kind() == kind)
1238+
.map(|(pkg_name, target)| {
1239+
let order = if ws.current_opt().map(|p| p.name()) == Some(pkg_name) {
1240+
0
1241+
} else {
1242+
1
1243+
};
1244+
clap_complete::CompletionCandidate::new(target.name())
1245+
.display_order(Some(order))
1246+
.help(Some(format!("(from {})", pkg_name).into()))
12571247
})
1258-
.collect::<Vec<_>>()
1259-
}
1260-
1261-
fn get_targets_from_metadata() -> CargoResult<Vec<Target>> {
1262-
let cwd = std::env::current_dir()?;
1263-
let gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?);
1264-
let ws = Workspace::new(&find_root_manifest_for_wd(&cwd)?, &gctx)?;
1265-
1266-
let packages = ws.members().collect::<Vec<_>>();
1267-
1268-
let targets = packages
1269-
.into_iter()
1270-
.flat_map(|pkg| pkg.targets().into_iter().cloned())
12711248
.collect::<Vec<_>>();
12721249

12731250
Ok(targets)
@@ -1319,9 +1296,9 @@ fn get_target_triples_from_rustup() -> CargoResult<Vec<clap_complete::Completion
13191296
}
13201297

13211298
fn get_target_triples_from_rustc() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1322-
let cwd = std::env::current_dir()?;
1323-
let gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?);
1324-
let ws = Workspace::new(&find_root_manifest_for_wd(&PathBuf::from(&cwd))?, &gctx);
1299+
let gctx = new_gctx_for_completions()?;
1300+
1301+
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx);
13251302

13261303
let rustc = gctx.load_global_rustc(ws.as_ref().ok())?;
13271304

0 commit comments

Comments
 (0)