nushell / reedline

A feature-rich line editor - powering Nushell
https://docs.rs/reedline/
MIT License
520 stars 144 forks source link

Completion based on man page/help page and/or zsh #508

Open tobiasBora opened 1 year ago

tobiasBora commented 1 year ago

The current completion system is quite poor: it is impossible to generate completion for command arguments without manually writting an extern completion for any new command. Carapace-bin and the user script repository provide some pre-made extern for some commands, but the list of supported commands is really small. In any case, it can't generate completion for tools that have not been manually added.

On the other hand, fish has a python script that parses man pages to generate automatically the completion. It would be great to have it in nushell. Here is the repo that adapts the script for bash: https://github.com/nevesnunes/sh-manpage-completions

We could also maybe parse bash/zsh completion files to get more completions for free. For instance this project has a great autocomplete functionality (no idea how it's working…), it can even complete nix packages when typing nix-shell -p vim, even as you type: https://github.com/marlonrichert/zsh-autocomplete/

References

Fish script adapted to bash: https://github.com/nevesnunes/sh-manpage-completions

fdncred commented 1 year ago

we've had some attempts at that, and you can see the results at the nu_scripts/custom_completions/auto-generate repo. we'd like to expand this as well because many of these are poorly done, although it was a great first start.

tobiasBora commented 1 year ago

Thanks for this interesting pointer (this list is obtained from fish completions files as I understand). But most of the generated stuff are basically empty, see e.g. https://github.com/nushell/nu_scripts/blob/main/custom-completions/auto-generate/completions/rsync.nu

Also, I guess it won't adapt to the currently installed version/programs that are not in the list. I'm not sure if it would be possible to generate it on the go by reading the man page directly on the user computer. It's certainly harder to do I guess, espetially if one wants to improve the record manually.

fdncred commented 1 year ago

Agreed. Lots of useless ones in there. Like I said, it was a great first start and someone just needs to pick up the torch and tweak these scripts https://github.com/nushell/nu_scripts/tree/main/custom-completions/auto-generate to capture more information.

I myself looked into parsing man page files written in TROFF but found it was a totally non-trivial process. There was a process to convert man pages to html, which I found and thought would be easier, but it wasn't easy enough for me to work on it. LOL. It would still require hours of work probably.

tobiasBora commented 1 year ago

Yeah, that's part of the reason why I was linking to https://github.com/nevesnunes/sh-manpage-completions that is supposed to already provide multiple parsers for man pages (I can see 6 parsers here). But it's certainly a bit of work anyway, not sure how it compares with parsing zsh completion.

It is also maybe possible to use the help page using https://github.com/RobSis/zsh-completion-generator.

Another quick-and-dirty approach is to search all words starting with -- in the man page or help page, and to display the help by displaying the line that starts with this option. (it will not be as powerful of course, but could be a good starting point)