Skip to content

Shell completions #3220

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

itowlson
Copy link
Collaborator

@itowlson itowlson commented Aug 7, 2025

I should know by now that these PRs only end in tears.

This uses a technique explored in https://github.com/karthik2804/spin-autocomplete. The idea is, instead of generating completions directly or going through the flawed clap-complete, we generate a YAML file (it's JSON, but JSON is YAML so I don't have to deal with actual YAML), then we can run completely over it, thus:

spin maintenance generate-shell-completions -o completely.yaml  # the name matters
docker run --rm --user $(id -u):$(id -g) --volume "$PWD:/app" -e COMPLETELY_OUTPUT_PATH=spin_comps.sh dannyben/completely generate
source ./spin_comps.sh

So we can get a high level of control over the completions, but without a separate binary or handcrafting bash.

Note as per Karthik's repo this relies on bashcompinit to work on zsh - sorry.

Draft because file completions are not cat-like and I am recruiting fellow believers to fix my ignorance.

@itowlson itowlson force-pushed the completions-3 branch 3 times, most recently from 434c4ec to ab407ab Compare August 8, 2025 00:32
@lann
Copy link
Collaborator

lann commented Aug 8, 2025

Feel free to reject this suggestion as naive and/or too terrible to contemplate:

Would it be plausible to use the clap builder API to construct a parallel Command that reuses those bits of derived clap types that aren't broken and manually reconstruct (or omit) the bits that are broken? (and then use e.g. clap_complete)

@lann
Copy link
Collaborator

lann commented Aug 8, 2025

...though it occurs to me that you may be complaining about clap_complete itself and not our weird subcommand shenanigans.

@itowlson
Copy link
Collaborator Author

I would dearly love to use clap_complete. Unfortunately:

  1. clap_complete doesn't have the vocabulary to express things like "list of templates" or "list of plugins." It only allows the ValueHint enum.
  2. The version of clap_complete that we are, for now, stuck with ignores ValueHint and completes EVERYTHING with paths. (This is fixed in v4.x, but that would require "up"-grading to clap 4.)

So I don't see a path to success with it for now, even with constructing a parallel Command tree, sorry.

@itowlson
Copy link
Collaborator Author

In shock news, completely has also broken my heart.

The trouble is that for parameter completion it relies on a first-last pattern, e.g. spin up*--from, which works great for the first TAB after the expression, but apparently not so great for subsequent tabs, because the last thing on the line is no longer --from but the initial completion. The impact is that if you have a file nested in a directory, completion stops at the directory e.g. if you are trying to get to foo/spin.toml you go spin up --from fo<TAB>, and it completes foo then leaves a space instead of including a / and allowing you to continue typing s<TAB> - you have to backspace over the space, then type /s<TAB> which is quite the speed bump.

In theory -o bashdefault -o default would fix the "not continuing" thing but in practice I think the first-last thing is messing it up.

By contrast, clap_complete uses a cleverer technique for figuring out which flag governs the current completion, then goes "compgen -f -o bashdefault -o default YOLO" so that everything gets completed as files.

Well, time to put this away for another year I guess, until I feel ready to fall in love with another completions technology that will also disappoint me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants