Skip to content

Commit 8383b7e

Browse files
committed
completions-getter: Add debugging bits and a function to test completions
Can be used with: bash -c "source ./bash-completions-getter.sh; test_bash_completion 'lftp -'"
1 parent 333312d commit 8383b7e

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

bash-completions-getter.sh

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ source_bash_completion() {
4242
[ -f "${BASH_SOURCE%/*}/$src_name" ]; then
4343
source "${BASH_SOURCE%/*}/$src_name"
4444
else
45-
local -a dirs=()
4645
local OIFS=$IFS IFS=: dir
4746
local lookup_dirs=(${XDG_DATA_DIRS:-/usr/local/share:/usr/share})
4847
IFS=$OIFS
@@ -162,13 +161,26 @@ get_completions() {
162161
COMP_WORDS=(${ZSH_WORDS[@]})
163162
cmd_name=${ZSH_NAME}
164163

164+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
165+
echo -n "INITIAL_WORDS: " >&2; printf "'%s'," "${COMP_WORDS[@]}" >&2; echo >&2
166+
fi
165167

166168
# add '' to COMP_WORDS if the last character of the command line is a space
167169
[[ "${COMP_LINE[@]: -1}" = ' ' ]] && COMP_WORDS+=('')
168170

169171
# index of the last word as fallback
170172
COMP_CWORD=${ZSH_CURRENT:-$(( ${#COMP_WORDS[@]} - 1 ))}
171173

174+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
175+
echo "CWORD: $COMP_CWORD" >&2
176+
echo "LINE: '$COMP_LINE'" >&2
177+
echo "POINT: $COMP_POINT" >&2
178+
echo -n "WORDS: " >&2; printf "'%s'," "${COMP_WORDS[@]}" >&2; echo >&2
179+
echo "WORDBREAKS: $COMP_WORDBREAKS" >&2
180+
181+
echo "loading complete for '$cmd_name'" >&2
182+
fi
183+
172184
# load completion
173185
source_bash_completion
174186

@@ -178,6 +190,11 @@ get_completions() {
178190
_completion_loader "$cmd_name"
179191
completion_command=$(complete -p "$cmd_name" 2>/dev/null)
180192
fi
193+
194+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
195+
echo "Using completion $completion_command" >&2
196+
fi
197+
181198
# detect completion function or command
182199
if [[ "$completion_command" =~ \
183200
^complete[[:space:]]+(.+) ]]; then
@@ -191,17 +208,28 @@ get_completions() {
191208
return 1;
192209
fi
193210

211+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
212+
echo -n "OPTIONS: " >&2; printf "'%s'," "${_COMP_OPTIONS[@]}" >&2; echo >&2
213+
fi
214+
194215
# ensure completion was detected
195216
if ([[ -z "$completion" ]] || [[ "$completion" == "_minimal" ]]); then
196217
if [ -n "$COMPLETE_WORDS" ]; then
197218
echo "${_COMP_OPTIONS[@]}"
219+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
220+
echo -n "WORDS: " >&2; printf "'%s'," "${COMPLETE_WORDS[@]}" >&2; echo >&2
221+
fi
198222
printf "%s\n" "${COMPLETE_WORDS[@]}"
199223
return 0
200224
fi
201225

202226
return 1
203227
fi
204228

229+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
230+
echo "Calling "$completion" $COMPLETE_ACTION_TYPE" >&2
231+
fi
232+
205233
# execute completion function or command (exporting the needed variables)
206234
# This may fail if compopt is called, but there's no easy way to pre-fill
207235
# the bash input with some stuff, using only bashy things.
@@ -215,6 +243,10 @@ get_completions() {
215243
cmd+=('');
216244
fi
217245

246+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
247+
echo -n "Calling " >&2; printf "'%s'," "${cmd[@]}" >&2; echo >&2
248+
fi
249+
218250
if [ "$COMPLETE_ACTION_TYPE" == 'C' ]; then
219251
export COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS COMP_WORDBREAKS
220252
COMPREPLY=($("${cmd[@]}" 2>/dev/null))
@@ -225,6 +257,10 @@ get_completions() {
225257
[ -n "$COMPLETE_WORDS" ] &&
226258
COMPREPLY+=("${COMPLETE_WORDS[@]}")
227259

260+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
261+
echo -n "REPLY: " >&2; printf "'%s'," "${COMPREPLY[@]}" >&2; echo >&2
262+
fi
263+
228264
# print options, followed by completions to stdout
229265
echo "${_COMP_OPTIONS[@]}"
230266
printf "%s\n" "${COMPREPLY[@]}"
@@ -246,3 +282,14 @@ get_defined_completions() {
246282

247283
printf "%s\n" "${defined_completions[@]}"
248284
}
285+
286+
test_bash_completion() {
287+
ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG=1
288+
ZSH_BUFFER="$@"
289+
ZSH_CURSOR=${ZSH_CURSOR:-${#ZSH_BUFFER}}
290+
COMP_WORDBREAKS=${ZSH_WORDBREAKS}
291+
ZSH_WORDS=(${@})
292+
ZSH_NAME="${ZSH_NAME:-${ZSH_WORDS[0]}}"
293+
294+
get_completions
295+
}

0 commit comments

Comments
 (0)