Skip to content

Commit a0315e2

Browse files
committed
refactor: used vector instead of hashmap
1 parent f210054 commit a0315e2

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

src/cargo/util/command_prelude.rs

+23-37
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use clap::builder::UnknownArgumentValueParser;
2525
use home::cargo_home_with_cwd;
2626
use semver::Version;
2727
use std::collections::HashMap;
28-
use std::collections::HashSet;
2928
use std::ffi::{OsStr, OsString};
3029
use std::path::Path;
3130
use std::path::PathBuf;
@@ -1129,53 +1128,40 @@ pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCan
11291128

11301129
fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
11311130
let gctx = new_gctx_for_completions()?;
1131+
let manifest_path = find_root_manifest_for_wd(gctx.cwd())?;
1132+
let ws = Workspace::new(&manifest_path, &gctx)?;
1133+
let mut feature_candidates = Vec::new();
11321134

1133-
let manifest_path = match find_root_manifest_for_wd(gctx.cwd()) {
1134-
Ok(path) => path,
1135-
Err(_) => return Ok(Vec::new()),
1136-
};
1137-
let ws = match Workspace::new(&manifest_path, &gctx) {
1138-
Ok(ws) => ws,
1139-
Err(_) => return Ok(Vec::new()),
1140-
};
1141-
1142-
let mut features = HashSet::new();
1143-
1144-
// Process all packages in the workspace, collect features from all packages since --package could be used to select any of them
1135+
// Process all packages in the workspace
11451136
for package in ws.members() {
1137+
let package_name = package.name();
1138+
1139+
// Add direct features with package info
11461140
for feature_name in package.summary().features().keys() {
1147-
features.insert(feature_name.as_str().to_string());
1148-
}
1149-
1150-
// Add optional dependencies as they can be enabled as features, these might not be in the features map if they are not explicitly referenced
1151-
for dep in package.dependencies() {
1152-
if dep.is_optional() {
1153-
features.insert(dep.name_in_toml().to_string());
1154-
}
1141+
feature_candidates.push(
1142+
clap_complete::CompletionCandidate::new(feature_name)
1143+
.help(Some(format!("(from {})", package_name).into()))
1144+
);
11551145
}
1156-
1146+
11571147
// Add qualified features for dependencies
11581148
for dep in package.dependencies() {
1159-
let dep_name = dep.package_name().as_str().to_string();
1160-
// try to find this dependency in the workspace
1161-
if let Some(dep_pkg) = ws.members().find(|p| p.name().as_str() == dep_name) {
1149+
let dep_name = dep.name_in_toml();
1150+
1151+
// Try to find this dependency in the workspace
1152+
if let Some(dep_pkg) = ws.members().find(|p| p.name() == dep_name) {
11621153
for feat in dep_pkg.summary().features().keys() {
1163-
features.insert(format!("{}/{}", dep_name, feat));
1154+
let qualified_name = format!("{}/{}", dep_name, feat);
1155+
feature_candidates.push(
1156+
clap_complete::CompletionCandidate::new(qualified_name)
1157+
.help(Some(format!("(from {})", dep_pkg.name()).into()))
1158+
);
11641159
}
11651160
}
11661161
}
11671162
}
1168-
1169-
// always include the default feature
1170-
features.insert("default".to_string());
1171-
1172-
let mut sorted_features: Vec<_> = features.into_iter().collect();
1173-
sorted_features.sort();
1174-
1175-
Ok(sorted_features
1176-
.into_iter()
1177-
.map(|name| clap_complete::CompletionCandidate::new(name))
1178-
.collect())
1163+
1164+
Ok(feature_candidates)
11791165
}
11801166

11811167
fn get_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {

0 commit comments

Comments
 (0)