Skip to content

Add autocomplete for nested examples #15574

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MrMinimal
Copy link

@MrMinimal MrMinimal commented May 22, 2025

What does this PR try to resolve?

Not all examples show up in auto-completion.
If the examples directory has nested examples, only the top level ones will be shown with cargo build --example<TAB>.cargo build --example<TAB>.
Bevy examples only show hello_world instead of all available examples.

image

Old:
image

New:
image

How should we test and review this PR?

The completion logic for me is in ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/etc/bash_completion.d/cargo.
So to test, this has to be edited as it doesn't get updated when you simply build cargo (I think rustup installs it).

Then go into a Rust project with nested examples (e. g. Bevy) and type cargo build --example and press <TAB>.

@rustbot
Copy link
Collaborator

rustbot commented May 22, 2025

r? @epage

rustbot has assigned @epage.
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-completions Area: shell completions S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 22, 2025
@@ -246,7 +246,7 @@ _get_examples(){
local manifest=$(_locate_manifest)
[ -z "$manifest" ] && return 0

local files=("${manifest%/*}"/examples/*.rs)
local files=("${manifest%/*}"/examples/**/*.rs)
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 do something similar for zsh?

cargo/src/etc/_cargo

Lines 472 to 477 in c2c636a

_cargo_example_names() {
if [[ -d examples ]]; then
local -a files=(${(@f)$(echo examples/*.rs(:t:r))})
_values 'example' "${files[@]}"
fi
}

@@ -246,7 +246,7 @@ _get_examples(){
local manifest=$(_locate_manifest)
[ -z "$manifest" ] && return 0

local files=("${manifest%/*}"/examples/*.rs)
local files=("${manifest%/*}"/examples/**/*.rs)
Copy link
Contributor

Choose a reason for hiding this comment

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

This could pick up both examples and submodules. Unsure whether that is worth it or not

Copy link
Contributor

Choose a reason for hiding this comment

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

I would be a little worried that the false-positive rate would be too high. In bevy for example, this would auto-complete a ton of names that aren't examples.

I think the only real solution here is to parse Cargo.toml, and I think native-completions is the only way to realistically do that. This bashcomp could do some json parsing of cargo metadata, but I wouldn't want to go there.

@epage
Copy link
Contributor

epage commented May 22, 2025

Are you aware of our experimental completion system (docs)? It uses cargo to do things like look up all examples.

@MrMinimal
Copy link
Author

@epage I wasn't aware of the experimental system, just wanted to contribute back a quick fix I had.
If the new system replaces this one, I'll close the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completions Area: shell completions 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