Complete custom "git-foo" commands from "git foo"

Since #7075, git-foo.fish files are sourced when Git completions are loaded.
However, at least Cobra (CLI framework for Go) provides completions like

	complete git-foo ...

This means that completions are only offered when typing "git-foo <TAB>"
and not on "git foo <TAB>". Fix this by forwarding the completion requests.
Take care to only forward if there are actually completions for "git-foo",
to avoid adding filename completions.
This commit is contained in:
Johannes Altmanninger 2021-01-24 11:48:17 +01:00
parent b59cad3c5b
commit 09161761c1
2 changed files with 15 additions and 0 deletions

View file

@ -356,6 +356,7 @@ Completions
- The ``make`` completions no longer second-guess make's file detection, fixing target completion in some cases (:issue:`7535`).
- The command completions now correctly print the description even if the command was fully matched (like in ``ls<TAB>``).
- The ``set`` completions no longer hide variables starting with ``__``, they are sorted last instead.
- Completion scripts for custom Git subcommands like ``git-xyz`` are now loaded with Git completions. The completions can now be defined directly on the subcommand (using ``complete git-xyz``), and completion for ``git xyz`` will work. (:issue:`7075`, :issue:`7652`)
Changes not visible to users
----------------------------

View file

@ -1899,6 +1899,16 @@ complete -c git -n '__fish_git_using_command bisect; and __fish_seen_argument --
## Custom commands (git-* commands installed in the PATH)
complete -c git -n __fish_git_needs_command -a '(__fish_git_custom_commands)' -d 'Custom command'
function __fish_git_complete_custom_command -a subcommand
set -l cmd (commandline -opc)
set -e cmd[1] # Drop "git".
set -l subcommand_args
if argparse -s (__fish_git_global_optspecs) -- $cmd
set subcommand_args $argv[2..] # Drop the subcommand.
end
complete -C "git-$subcommand $subcommand_args "(commandline -ct)
end
# Get the path to the generated completions
# If $XDG_DATA_HOME is set, that's it, if not, it will be removed and ~/.local/share will remain.
set -l generated_path $XDG_DATA_HOME/fish/generated_completions ~/.local/share/fish/generated_completions
@ -1920,5 +1930,9 @@ for git_ext in $complete_dirs/git-*.fish
contains -- $cmd $__fish_git_custom_commands_completion
and continue
source $git_ext
set -l subcommand (string replace -r '^git-' '' -- $cmd)
if [ (complete git-$subcommand | count) -gt 0 ]
complete git -f -n "__fish_git_using_command $subcommand" -a "(__fish_git_complete_custom_command $subcommand)"
end
set -a __fish_git_custom_commands_completion $cmd
end