1# fish completion for git-bug -*- shell-script -*-
2
3function __git-bug_debug
4 set 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 with: $argv"
12
13 set args (string split -- " " "$argv")
14 set lastArg "$args[-1]"
15
16 __git-bug_debug "args: $args"
17 __git-bug_debug "last arg: $lastArg"
18
19 set emptyArg ""
20 if test -z "$lastArg"
21 __git-bug_debug "Setting emptyArg"
22 set emptyArg \"\"
23 end
24 __git-bug_debug "emptyArg: $emptyArg"
25
26 set requestComp "$args[1] __complete $args[2..-1] $emptyArg"
27 __git-bug_debug "Calling $requestComp"
28
29 set results (eval $requestComp 2> /dev/null)
30 set comps $results[1..-2]
31 set directiveLine $results[-1]
32
33 # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
34 # completions must be prefixed with the flag
35 set flagPrefix (string match -r -- '-.*=' "$lastArg")
36
37 __git-bug_debug "Comps: $comps"
38 __git-bug_debug "DirectiveLine: $directiveLine"
39 __git-bug_debug "flagPrefix: $flagPrefix"
40
41 for comp in $comps
42 printf "%s%s\n" "$flagPrefix" "$comp"
43 end
44
45 printf "%s\n" "$directiveLine"
46end
47
48# This function does three things:
49# 1- Obtain the completions and store them in the global __git-bug_comp_results
50# 2- Set the __git-bug_comp_do_file_comp flag if file completion should be performed
51# and unset it otherwise
52# 3- Return true if the completion results are not empty
53function __git-bug_prepare_completions
54 # Start fresh
55 set --erase __git-bug_comp_do_file_comp
56 set --erase __git-bug_comp_results
57
58 # Check if the command-line is already provided. This is useful for testing.
59 if not set --query __git-bug_comp_commandLine
60 set __git-bug_comp_commandLine (commandline)
61 end
62 __git-bug_debug "commandLine is: $__git-bug_comp_commandLine"
63
64 set results (__git-bug_perform_completion "$__git-bug_comp_commandLine")
65 set --erase __git-bug_comp_commandLine
66 __git-bug_debug "Completion results: $results"
67
68 if test -z "$results"
69 __git-bug_debug "No completion, probably due to a failure"
70 # Might as well do file completion, in case it helps
71 set --global __git-bug_comp_do_file_comp 1
72 return 0
73 end
74
75 set directive (string sub --start 2 $results[-1])
76 set --global __git-bug_comp_results $results[1..-2]
77
78 __git-bug_debug "Completions are: $__git-bug_comp_results"
79 __git-bug_debug "Directive is: $directive"
80
81 if test -z "$directive"
82 set directive 0
83 end
84
85 set compErr (math (math --scale 0 $directive / 1) % 2)
86 if test $compErr -eq 1
87 __git-bug_debug "Received error directive: aborting."
88 # Might as well do file completion, in case it helps
89 set --global __git-bug_comp_do_file_comp 1
90 return 0
91 end
92
93 set nospace (math (math --scale 0 $directive / 2) % 2)
94 set nofiles (math (math --scale 0 $directive / 4) % 2)
95
96 __git-bug_debug "nospace: $nospace, nofiles: $nofiles"
97
98 # Important not to quote the variable for count to work
99 set numComps (count $__git-bug_comp_results)
100 __git-bug_debug "numComps: $numComps"
101
102 if test $numComps -eq 1; and test $nospace -ne 0
103 # To support the "nospace" directive we trick the shell
104 # by outputting an extra, longer completion.
105 __git-bug_debug "Adding second completion to perform nospace directive"
106 set --append __git-bug_comp_results $__git-bug_comp_results[1].
107 end
108
109 if test $numComps -eq 0; and test $nofiles -eq 0
110 __git-bug_debug "Requesting file completion"
111 set --global __git-bug_comp_do_file_comp 1
112 end
113
114 # If we don't want file completion, we must return true even if there
115 # are no completions found. This is because fish will perform the last
116 # completion command, even if its condition is false, if no other
117 # completion command was triggered
118 return (not set --query __git-bug_comp_do_file_comp)
119end
120
121# Remove any pre-existing completions for the program since we will be handling all of them
122# TODO this cleanup is not sufficient. Fish completions are only loaded once the user triggers
123# them, so the below deletion will not work as it is run too early. What else can we do?
124complete -c git-bug -e
125
126# The order in which the below two lines are defined is very important so that __git-bug_prepare_completions
127# is called first. It is __git-bug_prepare_completions that sets up the __git-bug_comp_do_file_comp variable.
128#
129# This completion will be run second as complete commands are added FILO.
130# It triggers file completion choices when __git-bug_comp_do_file_comp is set.
131complete -c git-bug -n 'set --query __git-bug_comp_do_file_comp'
132
133# This completion will be run first as complete commands are added FILO.
134# The call to __git-bug_prepare_completions will setup both __git-bug_comp_results abd __git-bug_comp_do_file_comp.
135# It provides the program's completion choices.
136complete -c git-bug -n '__git-bug_prepare_completions' -f -a '$__git-bug_comp_results'
137