Completion generation
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 tostr_to_impl_fn(maybe with""representing anything?) - To integrate these, probably run some scripts from
build.zig? - If some refactoring happens, maybe
command.zigcould be@imported intobuild.zigto generate completions? .
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.
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:
- 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.
- 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.
- 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.
I'll try doing it. I wanted to write more zig anyway and this is a nice project.