Make most subcommand completions take external commands only

Also fix ssh completions which were broken by
277fca9c6a.
This commit is contained in:
Johannes Altmanninger
2019-11-05 10:56:03 +01:00
parent d8b305d6e4
commit 4f1fa9513c
14 changed files with 56 additions and 45 deletions

View File

@@ -1,45 +1,58 @@
function __fish_complete_subcommand -d "Complete subcommand" --no-scope-shadowing
# Pass --commandline to complete the remainder of the arguments instead of the commandline.
# Pass --allow-functions-and-builtins to enable the completion of the first token as function or builtin.
# Other args are considered flags to the supercommand that require an option.
# How many non-option tokens we skip in the input commandline before completing the subcommand
# Usually 1; for ssh 2.
set -l skip_next 1
set -l test
switch "$argv[1]"
case '--fcs-skip=*'
set -l rest
string replace -a = ' ' -- $argv[1] | read test skip_next
set -e argv[1]
end
set -l res ""
set -l had_cmd 0
set -l cmd (commandline -cop) (commandline -ct)
for i in $cmd
if test $skip_next -gt 0
set skip_next (math $skip_next - 1)
continue
set -l allow_functions_and_builtins false
set -l subcommand
while string match -rq -- '^--[a-z]' $argv[1]
set -l arg $argv[1]
set -e argv[1]
switch $arg
case '--fcs-skip=*'
set skip_next (string split = -- $arg)[2]
case '--allow-functions-and-builtins'
set allow_functions_and_builtins true
case '--commandline'
set subcommand $argv
set -e argv
break
end
end
set -l options_with_param $argv
if test "$had_cmd" = 1
set res "$res $i"
else
if contains -- $i $argv
if not string length -q $subcommand
set cmd (commandline -cop) (commandline -ct)
while set -q cmd[1]
set -l token $cmd[1]
set -e cmd[1]
if contains -- $token $options_with_param
set skip_next (math $skip_next + 1)
continue
end
switch $i
case '-*'
case '*=*'
switch $token
case '-*' '*=*'
continue
case '*'
set had_cmd 1
set res $i
if test $skip_next -gt 0
set skip_next (math $skip_next - 1)
continue
end
# found the start of our command
set subcommand $token $cmd
break
end
end
end
printf "%s\n" (complete -C "$res")
if test $allow_functions_and_builtins = false && test (count $subcommand) -eq 1
__fish_complete_external_command
else
printf "%s\n" (complete -C "$subcommand")
end
end