Skip to content

Commit 4a5c1b8

Browse files
committed
completions-getter: More debug and code cleanups
1 parent 8383b7e commit 4a5c1b8

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

bash-completions-getter.sh

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ parse_complete_options() {
140140
esac
141141
done
142142

143-
[ -z "$COMPLETE_ACTION" ] && [ -z "$COMPLETE_WORDS" ] \
143+
[ -z "$COMPLETE_ACTION" ] && [ ${#COMPLETE_WORDS[@]} -eq 0 ] \
144144
&& return;
145145

146146
while [ ${#@} -gt 0 ]; do
@@ -192,7 +192,7 @@ get_completions() {
192192
fi
193193

194194
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
195-
echo "Using completion $completion_command" >&2
195+
echo "Using completion command '$completion_command'" >&2
196196
fi
197197

198198
# detect completion function or command
@@ -208,17 +208,15 @@ get_completions() {
208208
return 1;
209209
fi
210210

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-
215211
# ensure completion was detected
216-
if ([[ -z "$completion" ]] || [[ "$completion" == "_minimal" ]]); then
217-
if [ -n "$COMPLETE_WORDS" ]; then
212+
if [ -z "$completion" ] || [[ "$completion" == "_minimal" ]]; then
213+
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
214+
echo -n "OPTIONS: " >&2; printf "'%s'," "${_COMP_OPTIONS[@]}" >&2; echo >&2
215+
echo -n "WORDS: " >&2; printf "'%s'," "${COMPLETE_WORDS[@]}" >&2; echo >&2
216+
fi
217+
218+
if [ ${#COMPLETE_WORDS[@]} -gt 0 ]; then
218219
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
222220
printf "%s\n" "${COMPLETE_WORDS[@]}"
223221
return 0
224222
fi
@@ -227,7 +225,7 @@ get_completions() {
227225
fi
228226

229227
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
230-
echo "Calling "$completion" $COMPLETE_ACTION_TYPE" >&2
228+
echo "Completion action is '$completion' of type '$COMPLETE_ACTION_TYPE'" >&2
231229
fi
232230

233231
# execute completion function or command (exporting the needed variables)
@@ -237,27 +235,31 @@ get_completions() {
237235
cmd+=("$cmd_name")
238236
cmd+=("'${COMP_WORDS[${COMP_CWORD}]}'")
239237

240-
if [ ${COMP_CWORD} -gt 0 ]; then
238+
if [ "${COMP_CWORD}" -gt 0 ]; then
241239
cmd+=("'${COMP_WORDS[$((COMP_CWORD-1))]}'");
242240
else
243241
cmd+=('');
244242
fi
245243

244+
errorout=/dev/null
246245
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
246+
errorout=/dev/stderr
247247
echo -n "Calling " >&2; printf "'%s'," "${cmd[@]}" >&2; echo >&2
248248
fi
249249

250250
if [ "$COMPLETE_ACTION_TYPE" == 'C' ]; then
251251
export COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS COMP_WORDBREAKS
252-
COMPREPLY=($("${cmd[@]}" 2>/dev/null))
252+
mapfile -t COMPREPLY < <("${cmd[@]}" 2>"$errorout")
253253
else
254-
${cmd[@]} 2>/dev/null
254+
"${cmd[@]}" 2>"$errorout"
255255
fi
256256

257-
[ -n "$COMPLETE_WORDS" ] &&
257+
[ ${#COMPLETE_WORDS[@]} -gt 0 ] &&
258258
COMPREPLY+=("${COMPLETE_WORDS[@]}")
259259

260260
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
261+
echo -n "OPTIONS: " >&2; printf "'%s'," "${_COMP_OPTIONS[@]}" >&2; echo >&2
262+
echo -n "WORDS: " >&2; printf "'%s'," "${COMPLETE_WORDS[@]}" >&2; echo >&2
261263
echo -n "REPLY: " >&2; printf "'%s'," "${COMPREPLY[@]}" >&2; echo >&2
262264
fi
263265

0 commit comments

Comments
 (0)