Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(completer): Added completion for --features flag #15309

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Gmin2
Copy link
Contributor

@Gmin2 Gmin2 commented Mar 13, 2025

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

Related to #14520

How should we test and review this PR?

by running cargo build --features <TAB>, cargo run --features <TAB>

@rustbot
Copy link
Collaborator

rustbot commented Mar 13, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-cli Area: Command-line interface, option parsing, etc. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 13, 2025
@Gmin2
Copy link
Contributor Author

Gmin2 commented Mar 13, 2025

@epage suggestion will be appreciated on how can i improve this like should i include some description ? or something like that, as i have not discussed with yu earlier and pick directly from the parent issue

Comment on lines +1142 to +1146
for dep in package.dependencies() {
if dep.is_optional() {
features.insert(dep.name_in_toml().to_string());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these in the package's summary?

Copy link
Contributor Author

@Gmin2 Gmin2 Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we capture optional dependencies as features even if they aren't explicitly referenced in any feature declaration, whilw cargo automatically creates implicit features for optional dependencies, they might not appear in the features() map if they aren't referenced by other features, this ensures we provide completion for all valid feature names, including those implicit ones

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented out this block and still get the same completion results

Comment on lines 1183 to 1186
Ok(features
.into_iter()
.map(|name| clap_complete::CompletionCandidate::new(name))
.collect())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we sort or de-duplicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have sorted it in b615137

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about de-duplicate?

@ehuss ehuss assigned epage and unassigned ehuss Mar 15, 2025
@Gmin2 Gmin2 requested a review from epage March 22, 2025 09:25
@Gmin2 Gmin2 force-pushed the completer-features branch from 9674bef to b615137 Compare March 22, 2025 09:31
Comment on lines +1117 to +1120
let manifest_path = match find_root_manifest_for_wd(gctx.cwd()) {
Ok(path) => path,
Err(_) => return Ok(Vec::new()),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use ? if the caller does unwrap_or_default()?

Comment on lines +1122 to +1125
let ws = match Workspace::new(&manifest_path, &gctx) {
Ok(ws) => ws,
Err(_) => return Ok(Vec::new()),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto about ?

Comment on lines +1155 to +1156
// always include the default feature
features.insert("default".to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we do this? We don't implicitly create a default feature, see #8164.

Comment on lines +1129 to +1132
// Process all packages in the workspace, collect features from all packages since --package could be used to select any of them
for package in ws.members() {
for feature_name in package.summary().features().keys() {
features.insert(feature_name.as_str().to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the candidate description, should we mention the package it comes from?

// try to find this dependency in the workspace
if let Some(dep_pkg) = ws.members().find(|p| p.name().as_str() == dep_name) {
for feat in dep_pkg.summary().features().keys() {
features.insert(format!("{}/{}", dep_name, feat));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be name_in_toml and
not package_name

// try to find this dependency in the workspace
if let Some(dep_pkg) = ws.members().find(|p| p.name().as_str() == dep_name) {
for feat in dep_pkg.summary().features().keys() {
features.insert(format!("{}/{}", dep_name, feat));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we hide these? Seems like they could bloat the output


// Add qualified features for dependencies
for dep in package.dependencies() {
let dep_name = dep.package_name().as_str().to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this to be owned if we're just comparing it and printing it?

@rustbot

This comment has been minimized.

@epage
Copy link
Contributor

epage commented Mar 27, 2025

FYI I'm fine with you cleaning up your commits through the review process. This is small and easy to track the differences between each posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command-line interface, option parsing, etc. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants