Skip to content

Commit 00c7c80

Browse files
authored
feat(completer): Added completion for --features flag (#15309)
### What does this PR try to resolve? This attempts to complete the autocompleter for `cargo build --features <TAB>`, `cargo run --features <TAB>` It loads all the features that are there in the profile section of Cargo.toml ![Screenshot from 2025-03-14 03-36-53](https://github.com/user-attachments/assets/efeb07a1-60e0-4b77-bb47-05f39cd15fc7) Related to #14520 ### How should we test and review this PR? by running `cargo build --features <TAB>`, `cargo run --features <TAB>`
2 parents 4f2f1d7 + c5d4950 commit 00c7c80

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ pub trait CommandExt: Sized {
251251
"Space or comma separated list of features to activate",
252252
)
253253
.short('F')
254-
.help_heading(heading::FEATURE_SELECTION),
254+
.help_heading(heading::FEATURE_SELECTION)
255+
.add(clap_complete::ArgValueCandidates::new(|| {
256+
let candidates = get_feature_candidates();
257+
candidates.unwrap_or_default()
258+
})),
255259
)
256260
._arg(
257261
flag("all-features", "Activate all available features")
@@ -1188,6 +1192,28 @@ fn default_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
11881192
]
11891193
}
11901194

1195+
fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1196+
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)?;
1199+
let mut feature_candidates = Vec::new();
1200+
1201+
// Process all packages in the workspace
1202+
for package in ws.members() {
1203+
let package_name = package.name();
1204+
1205+
// Add direct features with package info
1206+
for feature_name in package.summary().features().keys() {
1207+
feature_candidates.push(
1208+
clap_complete::CompletionCandidate::new(feature_name)
1209+
.help(Some(format!("(from {})", package_name).into())),
1210+
);
1211+
}
1212+
}
1213+
1214+
Ok(feature_candidates)
1215+
}
1216+
11911217
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
11921218
get_targets_from_metadata()
11931219
.unwrap_or_default()

0 commit comments

Comments
 (0)