picocli icon indicating copy to clipboard operation
picocli copied to clipboard

[Feature request] Native zsh completion

Open gyscos opened this issue 6 years ago • 11 comments

Picocli currently can create bash completion files, and zsh has a compatibility tool to read bash completion files.

However, this only exposes basic autocompletion features (what bash itself support), and does not take advantage of the features zsh offer.

For instance, zsh can show a description for each command/option, as well as option aliases (short vs long options). There's a thousands pictures on the internet; here's one example for the git command:

screenshot from 2018-03-05 10-48-22

This behavior cannot be obtained from a bash completion file.

Therefore, I propose that a new Autocomplete.zsh static method be added, to output a zsh-native completion script that includes more details, like command and option description and aliases.

gyscos avatar Mar 05 '18 18:03 gyscos

I think that's great idea and that would be a very cool feature! Will you be able to provide a pull request?

remkop avatar Mar 05 '18 22:03 remkop

+1, this would be awesome

hristo-vrigazov avatar Mar 10 '18 13:03 hristo-vrigazov

The other relevant thing is that the file should be in FPATH and start with the #compdef line so shell login doesn't involve running all the scripts

https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org

yschimke avatar Jun 16 '19 06:06 yschimke

@yschimke thanks for the tip!

remkop avatar Jun 16 '19 07:06 remkop

The testing and debugging section of that doc is good also e.g.

| _complete_help | Ctrl+x h | displays information about context names, tags, and completion functions used when completing at the current cursor position |

yschimke avatar Jun 16 '19 08:06 yschimke

It's super hacky, and only supports the bare minimum needed for one-level descriptions on my specific app, but I made a first attempt at this on https://github.com/byteit101/picocli/commit/7f9d1cfd5e48fa3729adb3e36098f2bf1e32cdd0

I do plan on eventually getting around to cleaning it up to make it into an integrateable PR, but unfortunately I'm rather busy with other things for the next month or so, if anyone else wants to run with it.

byteit101 avatar Jun 21 '19 18:06 byteit101

@byteit101 Thank you! I need a bit more time to look in detail, but this seems like a good start!

remkop avatar Jun 22 '19 00:06 remkop

Going to play around with my specific script for now, but this would be high impact given it's the default shell in OSX now.

yschimke avatar May 16 '20 17:05 yschimke

That would be great! PRs are always welcome!

remkop avatar May 16 '20 20:05 remkop

@yschimke I saw your message on #502. Thanks! I am still working on some parser issues, but ZSH completion is high on my list of priorities. Once I make some progress on this your feedback will be very welcome!

UPDATE 2020/11/16

I was thinking about redesigning the logic for generating completion scripts to follow the Visitor pattern; I need to prototype this a bit to see how useful this would be. Something like this:

interface ModelVisitor<R,P> {
  R visit(CommandSpec command, P p);
  R visit(ArgGroupSpec group, P p);
  R visit(OptionSpec option, P p);
  R visit(PositionalParamSpec positional, P p);
}

Current call tree:

bash(String, CommandLine)
  generateEntryPointFunction
    generateFunctionCallsToArrContains
      [generateFunctionCallsToArrContains...] - recursively for nested subcommands
  generateFunctionForCommand - for all subcommands and nested descendant sub-subcmds
    generateCompletionCandidates - options with completion candidates
    generateOptionsSwitch - other options for which completions can be generated
    generatePositionParamCompletionCandidates - positionals with completion candidates
    generatePositionalParamsCases - other positionals for which completions can be generated

remkop avatar Jun 17 '20 22:06 remkop

Reference material:

  • https://blog.mads-hartmann.com/2017/08/06/writing-zsh-completion-scripts.html
  • https://thevaluable.dev/zsh-completion-guide-examples/
  • https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org

remkop avatar Mar 09 '22 02:03 remkop