carapace-sh / carapace-bridge

completion bridge
https://carapace.sh
MIT License
8 stars 1 forks source link

Look for bash completions in all $XDG_DATA_DIRS #187

Closed r0uv3n closed 8 months ago

r0uv3n commented 8 months ago

On NixOS, completions are often surfaced to bash (and shells with compatible completers) via $XDG_DATA_DIRS. I tried to use nushell with carapace's bridge to bash enabled, but none of these completions were found, since carapace only looks in the directories listed here.

Contrast this with where bash looks for completions, listed in the bash-completion FAQ:

Q. What is the search order for the completion file of each target command?

A. The completion files of commands are looked up by the shell function __load_completion. Here, the search order in bash-completion >= 2.12 is explained.

  1. BASH_COMPLETION_USER_DIR. The subdirectory completions of each paths in BASH_COMPLETION_USER_DIR separated by colons is considered for a completion directory.
  2. The location of the main bash_completion file. The subdirectory completions in the same directory as bash_completion is considered.
  3. The location of the target command. When the real location of the command is in the directory <prefix>/bin or <prefix>/sbin, the directory <prefix>/share/bash-completion/completions is considered.
  4. XDG_DATA_DIRS (or the system directories /usr/local/share:/usr/share if empty). The subdirectory bash-completion/completions of each paths in XDG_DATA_DIRS separated by colons is considered.

    The completion files of the name <cmd> or <cmd>.bash, where <cmd> is the name of the target command, are searched in the above completion directories in order. The file that is found first is used. When no completion file is found in any completion directories in this process, the completion files of the name _<cmd> is next searched in the completion directories in order.

The relevant code in the bashrc file is here.

It would be great if carapace could look in all of these directories as well. I'm not familiar with go, otherwise I'd just directly submit a PR. Do you have time to look into this? Otherwise I might try to figure this out myself some time later, and submit a PR, but that could take a while.

rsteube commented 8 months ago

Thanks for the link. I've put sth. together, let's see if it works...

r0uv3n commented 8 months ago

Hi I'm trying to test this right now (am on commit c79961be8d9876ffb71cf2bc7c2a338fa4ab5569 of carapace-bin) and tried to manually use the bridge using carapace --macro bridge.Bash "c" (to get autocompletions starting with "c"), but I get back

{"version":"v0.50.0","messages":["missing argument [ActionBash]"],"nospace":"","usage":"","values":[]}

Am I passing in the command to autocomplete wrong or something similar? Doing carapace --macro bridge.CarapaceBin "c" instead works just fine.

rsteube commented 8 months ago

bridge.CarapaceBin completes path executables as first position arguments are optional. bridge.Bash does not and needs at least one argument. Might be good to support this there as well though.

I think it isn't working yet. Setting up nix is a bit of trouble, but I'm looking into it.

rsteube commented 8 months ago

So it works for me now.

  1. some scripts have a .bash suffix which needed to be trimmed
  2. list of completions is cached which needed to be cleared
r0uv3n commented 8 months ago

bridge.CarapaceBin completes path executables as first position arguments are optional. bridge.Bash does not and needs at least one argument. Might be good to support this there as well though.

I think I got the argument passing to work? Is the following correct?

carapace --macro bridge.Bash(["c"]) -h

Without the -h it doesn't seem to output anything. The output is still not what one would want, but at least I don't get the missing argument message anymore:

{"version":"v0.50.0","messages":[],"nospace":",./:=@","usage":"","values":[]}

I'm on commit 31bd8bb right now. Am I using the command wrong? Or can I help in some other way in debugging this?

rsteube commented 8 months ago

Quotes are important due to the round brackets:

carapace --macro 'bridge.Bash(["git"])' ""
r0uv3n commented 8 months ago

Ah yes, thank you, that works now! But completions for nixos-rebuild (should offer boot, build, etc.) still don't work, and similarly for e.g. nixos-install . Completions for the nixos- commands on my system are under /run/current-system/sw/share/bash-completion/completions (which is in $XDG_DATA_DIRS).

  • list of completions is cached which needed to be cleared

Could it be that I still need to do this?

rsteube commented 8 months ago

You should have a carapace --clear-cache command now, try that one.

r0uv3n commented 8 months ago

Yes, that works! Thanks a lot!