Skip to content

Commit b355a81

Browse files
committed
feat(complete): Added completion for --profile
- Added completion for `--profile` in `cargo build` - Loads the built in profile and custom profiles present in `Cargo.toml` and `.cargo/config.toml`
1 parent bc577dc commit b355a81

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/cargo/core/profiles.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ impl Profiles {
391391
.get(name)
392392
.ok_or_else(|| anyhow::format_err!("profile `{}` is not defined", name))
393393
}
394+
395+
/// Returns an iterator over all profile names known to Cargo.
396+
pub fn profile_names(&self) -> impl Iterator<Item = InternedString> + '_ {
397+
self.by_name.keys().copied()
398+
}
394399
}
395400

396401
/// An object used for handling the profile hierarchy.

src/cargo/util/command_prelude.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::core::compiler::{
22
BuildConfig, CompileKind, MessageFormat, RustcTargetData, TimingOutput,
33
};
44
use crate::core::resolver::{CliFeatures, ForceAllTargets, HasDevUnits};
5-
use crate::core::{shell, Edition, Package, Target, TargetKind, Workspace};
5+
use crate::core::{profiles::Profiles, shell, Edition, Package, Target, TargetKind, Workspace};
66
use crate::ops::lockfile::LOCKFILE_NAME;
77
use crate::ops::registry::RegistryOrIndex;
88
use crate::ops::{self, CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
@@ -274,7 +274,11 @@ pub trait CommandExt: Sized {
274274
self._arg(
275275
opt("profile", profile)
276276
.value_name("PROFILE-NAME")
277-
.help_heading(heading::COMPILATION_OPTIONS),
277+
.help_heading(heading::COMPILATION_OPTIONS)
278+
.add(clap_complete::ArgValueCandidates::new(|| {
279+
let candidates = get_profile_candidates();
280+
candidates.unwrap_or_default()
281+
})),
278282
)
279283
}
280284

@@ -1106,6 +1110,56 @@ pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCan
11061110
}
11071111
}
11081112

1113+
fn get_profile_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1114+
match get_workspace_profile_candidates() {
1115+
Ok(candidates) if !candidates.is_empty() => Ok(candidates),
1116+
// fallback to default profile candidates
1117+
_ => Ok(default_profile_candidates()),
1118+
}
1119+
}
1120+
1121+
fn get_workspace_profile_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
1122+
let gctx = new_gctx_for_completions()?;
1123+
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?;
1124+
let profiles = Profiles::new(&ws, InternedString::new("dev"))?;
1125+
1126+
let mut candidates = Vec::new();
1127+
for name in profiles.profile_names() {
1128+
if let Ok(profile_instance) = Profiles::new(&ws, name) {
1129+
let base_profile = profile_instance.base_profile();
1130+
1131+
let mut description = String::from(if base_profile.opt_level.as_str() == "0" {
1132+
"unoptimized"
1133+
} else {
1134+
"optimized"
1135+
});
1136+
1137+
if base_profile.debuginfo.is_turned_on() {
1138+
description.push_str(" + debuginfo");
1139+
}
1140+
1141+
candidates.push(
1142+
clap_complete::CompletionCandidate::new(name.to_string())
1143+
.help(Some(description.into())),
1144+
);
1145+
}
1146+
}
1147+
1148+
Ok(candidates)
1149+
}
1150+
1151+
fn default_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
1152+
vec![
1153+
clap_complete::CompletionCandidate::new("dev".to_string())
1154+
.help(Some("unoptimized + debuginfo".into())),
1155+
clap_complete::CompletionCandidate::new("release".to_string())
1156+
.help(Some("optimized".into())),
1157+
clap_complete::CompletionCandidate::new("test".to_string())
1158+
.help(Some("unoptimized + debuginfo".into())),
1159+
clap_complete::CompletionCandidate::new("bench".to_string()).help(Some("optimized".into())),
1160+
]
1161+
}
1162+
11091163
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
11101164
get_targets_from_metadata()
11111165
.unwrap_or_default()

0 commit comments

Comments
 (0)