amber-lang / amber

💎 Amber the programming language compiled to Bash
https://amber-lang.com
GNU General Public License v3.0
3.94k stars 89 forks source link

[Feature] Add option to print Bash completion script #558

Open hdwalters opened 3 weeks ago

hdwalters commented 3 weeks ago

Is your feature request related to a problem? Please describe. A lot of Bash CLI tools provide a command line option to output a completion script. For example, ripgrep provides an option:

$ rg --generate=complete-bash | head -5
_rg() {
  local i cur prev opts cmds
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

This can be called in a .bashrc file, to enable command line completion:

source <(rg --generate=complete-bash)

It would be nice if Amber supported this.

Describe the solution you'd like Since we already use the clap library, we can use the complementary clap_complete library to do this.

Describe alternatives you've considered N/A

Additional context See https://docs.rs/clap_complete/latest/clap_complete/ for more details.

b1ek commented 3 weeks ago

what kind of completition scripts are you talking about? im pretty sure that @KrosFire is working on an LSP server, so they might not be necessary

hdwalters commented 3 weeks ago

No, I'm talking about tab completion on the command line, not keyword completion in an IDE. Suppose I want to type git commit. If I type this:

$ git co<Tab><Tab>

I get this, where | represents the cursor:

$ git co
co       commit   config   
$ git co|

If I then type this:

$ git com<Tab>

I get this:

$ git commit |

Whenever the user presses the Tab key, Bash parses the command line so far, and calls the relevant completion script. It then offers completion options according to the output of that script. The clap library has a feature where you can generate completion scripts automatically.

hdwalters commented 3 weeks ago

I've assigned this and #411 to myself; might as well do both at the same time.