1# fish completion for git-bug -*- shell-script -*-
2
3function __git_bug_debug
4 set -l file "$BASH_COMP_DEBUG_FILE"
5 if test -n "$file"
6 echo "$argv" >> $file
7 end
8end
9
10function __git_bug_perform_completion
11 __git_bug_debug "Starting __git_bug_perform_completion"
12
13 # Extract all args except the last one
14 set -l args (commandline -opc)
15 # Extract the last arg and escape it in case it is a space
16 set -l lastArg (string escape -- (commandline -ct))
17
18 __git_bug_debug "args: $args"
19 __git_bug_debug "last arg: $lastArg"
20
21 # Disable ActiveHelp which is not supported for fish shell
22 set -l requestComp "GIT_BUG_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg"
23
24 __git_bug_debug "Calling $requestComp"
25 set -l results (eval $requestComp 2> /dev/null)
26
27 # Some programs may output extra empty lines after the directive.
28 # Let's ignore them or else it will break completion.
29 # Ref: https://github.com/spf13/cobra/issues/1279
30 for line in $results[-1..1]
31 if test (string trim -- $line) = ""
32 # Found an empty line, remove it
33 set results $results[1..-2]
34 else
35 # Found non-empty line, we have our proper output
36 break
37 end
38 end
39
40 set -l comps $results[1..-2]
41 set -l directiveLine $results[-1]
42
43 # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
44 # completions must be prefixed with the flag
45 set -l flagPrefix (string match -r -- '-.*=' "$lastArg")
46
47 __git_bug_debug "Comps: $comps"
48 __git_bug_debug "DirectiveLine: $directiveLine"
49 __git_bug_debug "flagPrefix: $flagPrefix"
50
51 for comp in $comps
52 printf "%s%s\n" "$flagPrefix" "$comp"
53 end
54
55 printf "%s\n" "$directiveLine"
56end
57
58# This function does two things:
59# - Obtain the completions and store them in the global __git_bug_comp_results
60# - Return false if file completion should be performed
61function __git_bug_prepare_completions
62 __git_bug_debug ""
63 __git_bug_debug "========= starting completion logic =========="
64
65 # Start fresh
66 set --erase __git_bug_comp_results
67
68 set -l results (__git_bug_perform_completion)
69 __git_bug_debug "Completion results: $results"
70
71 if test -z "$results"
72 __git_bug_debug "No completion, probably due to a failure"
73 # Might as well do file completion, in case it helps
74 return 1
75 end
76
77 set -l directive (string sub --start 2 $results[-1])
78 set --global __git_bug_comp_results $results[1..-2]
79
80 __git_bug_debug "Completions are: $__git_bug_comp_results"
81 __git_bug_debug "Directive is: $directive"
82
83 set -l shellCompDirectiveError 1
84 set -l shellCompDirectiveNoSpace 2
85 set -l shellCompDirectiveNoFileComp 4
86 set -l shellCompDirectiveFilterFileExt 8
87 set -l shellCompDirectiveFilterDirs 16
88
89 if test -z "$directive"
90 set directive 0
91 end
92
93 set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2)
94 if test $compErr -eq 1
95 __git_bug_debug "Received error directive: aborting."
96 # Might as well do file completion, in case it helps
97 return 1
98 end
99
100 set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2)
101 set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)
102 if test $filefilter -eq 1; or test $dirfilter -eq 1
103 __git_bug_debug "File extension filtering or directory filtering not supported"
104 # Do full file completion instead
105 return 1
106 end
107
108 set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2)
109 set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2)
110
111 __git_bug_debug "nospace: $nospace, nofiles: $nofiles"
112
113 # If we want to prevent a space, or if file completion is NOT disabled,
114 # we need to count the number of valid completions.
115 # To do so, we will filter on prefix as the completions we have received
116 # may not already be filtered so as to allow fish to match on different
117 # criteria than the prefix.
118 if test $nospace -ne 0; or test $nofiles -eq 0
119 set -l prefix (commandline -t | string escape --style=regex)
120 __git_bug_debug "prefix: $prefix"
121
122 set -l completions (string match -r -- "^$prefix.*" $__git_bug_comp_results)
123 set --global __git_bug_comp_results $completions
124 __git_bug_debug "Filtered completions are: $__git_bug_comp_results"
125
126 # Important not to quote the variable for count to work
127 set -l numComps (count $__git_bug_comp_results)
128 __git_bug_debug "numComps: $numComps"
129
130 if test $numComps -eq 1; and test $nospace -ne 0
131 # We must first split on \t to get rid of the descriptions to be
132 # able to check what the actual completion will be.
133 # We don't need descriptions anyway since there is only a single
134 # real completion which the shell will expand immediately.
135 set -l split (string split --max 1 \t $__git_bug_comp_results[1])
136
137 # Fish won't add a space if the completion ends with any
138 # of the following characters: @=/:.,
139 set -l lastChar (string sub -s -1 -- $split)
140 if not string match -r -q "[@=/:.,]" -- "$lastChar"
141 # In other cases, to support the "nospace" directive we trick the shell
142 # by outputting an extra, longer completion.
143 __git_bug_debug "Adding second completion to perform nospace directive"
144 set --global __git_bug_comp_results $split[1] $split[1].
145 __git_bug_debug "Completions are now: $__git_bug_comp_results"
146 end
147 end
148
149 if test $numComps -eq 0; and test $nofiles -eq 0
150 # To be consistent with bash and zsh, we only trigger file
151 # completion when there are no other completions
152 __git_bug_debug "Requesting file completion"
153 return 1
154 end
155 end
156
157 return 0
158end
159
160# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves
161# so we can properly delete any completions provided by another script.
162# Only do this if the program can be found, or else fish may print some errors; besides,
163# the existing completions will only be loaded if the program can be found.
164if type -q "git-bug"
165 # The space after the program name is essential to trigger completion for the program
166 # and not completion of the program name itself.
167 # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.
168 complete --do-complete "git-bug " > /dev/null 2>&1
169end
170
171# Remove any pre-existing completions for the program since we will be handling all of them.
172complete -c git-bug -e
173
174# The call to __git_bug_prepare_completions will setup __git_bug_comp_results
175# which provides the program's completion choices.
176complete -c git-bug -n '__git_bug_prepare_completions' -f -a '$__git_bug_comp_results'
177