river icon indicating copy to clipboard operation
river copied to clipboard

Completion generation

Open skuzzymiglet opened this issue 5 years ago • 3 comments

Right now, completions need to be written manually, and could lag behind the fast pace of development. Automatic completion generation would make this easier.

  • Command names are not hard to extract - either with a big hacky regex kludge or maybe using real Zig parsing (don't know how polished the API is for it though)
  • For descriptions, comments above the called functions could work (though they may be too developer-oriented). But you have to find these comments for each ommand which is harder. A user-oriented description could be added to str_to_impl_fn
  • Subcommands are not declared at the moment, instead switched on with an if. An array with valid subcommands could be added to str_to_impl_fn (maybe with "" representing anything?)
  • To integrate these, probably run some scripts from build.zig?
  • If some refactoring happens, maybe command.zig could be@imported into build.zig to generate completions? .

skuzzymiglet avatar Apr 21 '21 16:04 skuzzymiglet

I'll say again here what I quickly mentioned on IRC.

I believe we probably can not get around defining completions by hand.

Yes, it is theoretically possible to extract all possible command flags from the code, but that would either require a grep hack nightmare or complex parsing code. Also consider options which have options themselves (like set-option has -output and -focused-output). Getting those relations right automatically is not going to be fun.

The completion scripts for all the different shells having to be updated separately certainly is an issue. As an alternative solution I propose the following: Have one file containing the (hand written) completion tree (probably in json format since zig has a parser for that in std) which is parsed at comptime and used to generate the completion scripts. Considering this is something many projects might want to use, it would make sense to implement this functionality as a library.

Leon-Plickat avatar Apr 21 '21 16:04 Leon-Plickat

The completion scripts for all the different shells having to be updated separately certainly is an issue. As an alternative solution I propose the following: Have one file containing the (hand written) completion tree (probably in json format since zig has a parser for that in std) which is parsed at comptime and used to generate the completion scripts. Considering this is something many projects might want to use, it would make sense to implement this functionality as a library.

I think something like this is the most reasonable path forward, with a few adjustments:

  1. We don't need to parse at comptime, just write a custom build step for our build.zig that generates the completion scripts from a spec.
  2. json would be a fine choice for the completion "spec" file, really all we need is some way to encode a tree. If feeling adventurous, https://github.com/gruebite/zzz also looks pretty nice.
  3. I think this should definitely become a library not hosted in the river repo at some point. I'm fine with it starting its life here though if nobody wants to take on that task.

Maintaining a simple "spec" file would be far simpler than maintaining 3 separate completion scripts for shells with their own quirks and inconsistencies, I'd love to have this but it probably won't make it to the top of my todo list so anyone who feels motivated should go for it, I'm happy to do code review.

ifreund avatar Apr 21 '21 16:04 ifreund

I'll try doing it. I wanted to write more zig anyway and this is a nice project.

Leon-Plickat avatar Apr 21 '21 16:04 Leon-Plickat