Skip to content

feat(complete): Added completion for --profile #15308

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ impl Profiles {
.get(name)
.ok_or_else(|| anyhow::format_err!("profile `{}` is not defined", name))
}

/// Returns an iterator over all profile names known to Cargo.
pub fn profile_names(&self) -> impl Iterator<Item = InternedString> + '_ {
self.by_name.keys().copied()
}
}

/// An object used for handling the profile hierarchy.
Expand Down
55 changes: 53 additions & 2 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core::compiler::{
BuildConfig, CompileKind, MessageFormat, RustcTargetData, TimingOutput,
};
use crate::core::resolver::{CliFeatures, ForceAllTargets, HasDevUnits};
use crate::core::{shell, Edition, Package, Target, TargetKind, Workspace};
use crate::core::{profiles::Profiles, shell, Edition, Package, Target, TargetKind, Workspace};
use crate::ops::lockfile::LOCKFILE_NAME;
use crate::ops::registry::RegistryOrIndex;
use crate::ops::{self, CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
Expand Down Expand Up @@ -274,7 +274,11 @@ pub trait CommandExt: Sized {
self._arg(
opt("profile", profile)
.value_name("PROFILE-NAME")
.help_heading(heading::COMPILATION_OPTIONS),
.help_heading(heading::COMPILATION_OPTIONS)
.add(clap_complete::ArgValueCandidates::new(|| {
let candidates = get_profile_candidates();
candidates
})),
)
}

Expand Down Expand Up @@ -1106,6 +1110,53 @@ pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCan
}
}

fn get_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
match get_workspace_profile_candidates() {
Ok(candidates) if !candidates.is_empty() => candidates,
// fallback to default profile candidates
_ => default_profile_candidates(),
}
}

fn get_workspace_profile_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
let gctx = new_gctx_for_completions()?;
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?;
let profiles = Profiles::new(&ws, InternedString::new("dev"))?;

let mut candidates = Vec::new();
for name in profiles.profile_names() {
let Ok(profile_instance) = Profiles::new(&ws, name) else {
continue;
};
let base_profile = profile_instance.base_profile();

let mut description = String::from(if base_profile.opt_level.as_str() == "0" {
"unoptimized"
} else {
"optimized"
});

if base_profile.debuginfo.is_turned_on() {
description.push_str(" + debuginfo");
}

candidates
.push(clap_complete::CompletionCandidate::new(&name).help(Some(description.into())));
}

Ok(candidates)
}

fn default_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
vec![
clap_complete::CompletionCandidate::new("dev").help(Some("unoptimized + debuginfo".into())),
clap_complete::CompletionCandidate::new("release").help(Some("optimized".into())),
clap_complete::CompletionCandidate::new("test")
.help(Some("unoptimized + debuginfo".into())),
clap_complete::CompletionCandidate::new("bench").help(Some("optimized".into())),
]
}

fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
get_targets_from_metadata()
.unwrap_or_default()
Expand Down