File: __fish_complete_subcommand.fish

package info (click to toggle)
fish 3.1.2-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 38,672 kB
  • sloc: ansic: 80,259; cpp: 47,069; javascript: 17,087; sh: 6,163; python: 3,429; makefile: 669; perl: 367; objc: 78; xml: 18
file content (57 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 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 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 $token
                case '-*' '*=*'
                    continue
                case '*'
                    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

    if test $allow_functions_and_builtins = false && test (count $subcommand) -eq 1
        __fish_complete_external_command "$subcommand"
    else
        printf "%s\n" (complete -C "$subcommand")
    end

end