Skip to content

Commit 5c093f9

Browse files
committed
bash-completions-getter: Support computing complete actions
ZSH part is still missing, but we're now providing options on the 2nd line of our output, tests parser and zsh plugin have been updated. Shells may be reloaded after updating, or will show an extra empty completion.
1 parent e58a1b7 commit 5c093f9

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

bash-completions-getter.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ parse_quoted_arguments() {
9494
}
9595

9696
parse_complete_options() {
97+
unset COMPLETE_ACTIONS
9798
unset COMPLETE_CALL
9899
unset COMPLETE_CALL_TYPE
99100
unset COMPLETE_SUPPORTED_COMMANDS
100101
unset COMPLETE_OPTIONS
101102
unset COMPLETE_WORDS
102103

104+
COMPLETE_ACTIONS=()
103105
COMPLETE_CALL=
104106
COMPLETE_SUPPORTED_COMMANDS=()
105107
COMPLETE_WORDS=()
@@ -119,7 +121,7 @@ parse_complete_options() {
119121
COMPLETE_ACTIONS+=("${2}")
120122
shift 2
121123
;;
122-
-pr|-D|-E|-G|-F|-C|-P|-S)
124+
-pr|-D|-E|-G|-P|-S)
123125
shift 2
124126
;;
125127
-o)
@@ -264,12 +266,15 @@ get_completions() {
264266
if [ -z "$completion" ] || [[ "$completion" == "_minimal" ]]; then
265267
if [ -n "$ZSH_BASH_COMPLETION_COMPLETION_FALLBACK_DEBUG" ]; then
266268
echo -n "OPTIONS: " >&2; printf "'%s'," "${_COMP_OPTIONS[@]}" >&2; echo >&2
269+
echo -n "ACTIONS: " >&2; printf "'%s'," "${COMPLETE_ACTIONS[@]}" >&2; echo >&2
267270
echo -n "WORDS: " >&2; printf "'%s'," "${COMPLETE_WORDS[@]}" >&2; echo >&2
268271
fi
269272

270273
if [ ${#COMPLETE_WORDS[@]} -gt 0 ] ||
271-
[ ${#COMPLETE_OPTIONS[@]} -gt 0 ]; then
274+
[ ${#COMPLETE_OPTIONS[@]} -gt 0 ] ||
275+
[ ${#COMPLETE_ACTIONS[@]} -gt 0 ]; then
272276
echo "${_COMP_OPTIONS[@]}"
277+
echo "${COMPLETE_ACTIONS[@]}"
273278
printf "%s\n" "${COMPLETE_WORDS[@]}"
274279
return 0
275280
fi
@@ -318,6 +323,7 @@ get_completions() {
318323

319324
# print options, followed by completions to stdout
320325
echo "${_COMP_OPTIONS[@]}"
326+
echo "${COMPLETE_ACTIONS[@]}"
321327
printf "%s\n" "${COMPREPLY[@]}"
322328
}
323329

tests/test-bash-completions-getter.sh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ check_completion() {
2323
local input="$1"
2424
local expected_completions=("$2")
2525
local expected_options=("$3")
26-
local expected_exit_code=${4:-0};
26+
local expected_actions=("$4")
27+
local expected_exit_code=${5:-0};
2728
local -a output
2829
local -a options
30+
local -a actions
2931

3032
echo "Checking completion $input" >&2
3133

@@ -54,9 +56,11 @@ check_completion() {
5456
fi
5557

5658
read -r -a options <<< "${output[0]}"
57-
local completions=("${output[@]:1}")
59+
read -r -a actions <<< "${output[1]}"
60+
local completions=("${output[@]:2}")
5861

5962
echo -n " options: " >&2; printf "'%s'," "${options[@]}" >&2; echo >&2
63+
echo -n " actions: " >&2; printf "'%s'," "${actions[@]}" >&2; echo >&2
6064
echo -n " completions: " >&2; printf "'%s'," "${completions[@]}" >&2; echo >&2
6165

6266
if [[ "${completions[*]}" != "${expected_completions[*]}" ]]; then
@@ -68,10 +72,15 @@ check_completion() {
6872
echo -n "! invalid options, expecting: "; printf "'%s'," "${expected_options[@]}"; echo
6973
exit 1
7074
fi
75+
76+
if [[ "${actions[*]}" != "${expected_actions[*]}" ]]; then
77+
echo -n "! invalid actions, expecting: "; printf "'%s'," "${expected_actions[@]}"; echo
78+
exit 1
79+
fi
7180
}
7281

7382
expect_failure() {
74-
check_completion "$1" '' '' 1
83+
check_completion "$1" '' '' '' 1
7584
}
7685

7786
function test_complete_function() {
@@ -107,15 +116,30 @@ check_completion foo-with-multiple-words "foo --bar"
107116
complete -o nospace -W "foo" foo-with-word-and-option
108117
check_completion foo-with-word-and-option "foo" nospace
109118

119+
complete -W "foo" -f foo-with-word-and-action
120+
check_completion foo-with-word-and-action "foo" '' file
121+
122+
complete -o nospace -W "foo" -f foo-with-word-option-and-action
123+
check_completion foo-with-word-option-and-action "foo" nospace file
124+
110125
complete -o nospace -W "foo" -o nosort foo-with-multiple-options
111126
check_completion foo-with-multiple-options "foo" "nosort nospace"
112127

113128
complete -o nospace -W "foo bar" -o nosort foo-with-multiple-words-and-options
114129
check_completion foo-with-multiple-words-and-options "foo bar" "nosort nospace"
115130

131+
complete -o nospace -W "foo bar" -o nosort -A binding -A job foo-with-multiple-words-options-and-actions
132+
check_completion foo-with-multiple-words-options-and-actions "foo bar" "nosort nospace" "job binding"
133+
116134
complete -o nospace -o nosort -o default foo-with-only-options
117135
check_completion foo-with-only-options '' "default nosort nospace"
118136

137+
complete -a -A 'command' -e -A 'file' foo-with-only-actions
138+
check_completion foo-with-only-actions '' '' 'alias command export file'
139+
140+
complete -acefg foo-with-only-actions-joined
141+
check_completion foo-with-only-actions-joined '' '' 'alias command export file group'
142+
119143
complete -F foo_complete_function_not_existant foo-with-function-invalid
120144
expect_failure foo-with-function-invalid
121145

zsh-bash-completions-fallback.plugin.zsh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function _bash_completions_fallback_completer {
1616
"source ${_bash_completions_getter_path}; get_completions")}");
1717

1818
local -a -U bopts=("${(ps: :)${(@f)out:0:1}}");
19-
local -a -U bcompletions=("${(@f)out:1}")
19+
local -a -U bactions=("${(ps: :)${(@f)out:1:2}}");
20+
local -a -U bcompletions=("${(@f)out:2}")
2021
local -a -U compoptions=()
2122

2223
if ((${bopts[(Ie)nospace]})); then

0 commit comments

Comments
 (0)