spin icon indicating copy to clipboard operation
spin copied to clipboard

Shell completions

Open itowlson opened this issue 5 months ago • 4 comments

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 avatar Aug 07 '25 23:08 itowlson

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 avatar Aug 08 '25 16:08 lann

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

lann avatar Aug 08 '25 17:08 lann

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 avatar Aug 10 '25 20:08 itowlson

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.

itowlson avatar Aug 11 '25 22:08 itowlson