Skip to content

Commit 9814499

Browse files
committed
Better handles completions for candidates from multiple sources
1 parent 9fc5d45 commit 9814499

3 files changed

Lines changed: 42 additions & 53 deletions

File tree

lib/internal/path

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,40 @@ _@go.set_command_path_and_argv() {
2323
return 1
2424
fi
2525

26-
local cmd_args=("$@")
27-
local cmd_name="${cmd_args[0]}"
28-
local cmd_paths=()
26+
local path_suffix
2927
local try_path
3028

31-
unset 'cmd_args[0]'
32-
33-
for try_path in "${_GO_SEARCH_PATHS[@]}"; do
34-
try_path="$try_path/$cmd_name"
35-
36-
if [[ -f "$try_path" && -x "$try_path" ]]; then
37-
cmd_paths+=("$try_path")
38-
fi
39-
done
40-
41-
if [[ "${#cmd_paths[*]}" -eq 0 ]]; then
42-
printf "Unknown command: ${cmd_name}\n\n" >&2
43-
_@go.list_available_commands "${_GO_SEARCH_PATHS[@]}" >&2
44-
return 1
45-
fi
46-
47-
local -i cmd_arg_index=1
48-
cmd_name=("$cmd_name")
49-
50-
local -i i=0
51-
local -i longest_path=()
5229
__go_cmd_name=()
53-
for try_path in "${cmd_paths[@]}"; do
54-
for arg in "${cmd_args[@]}"; do
55-
# This is most likely to happen during argument completion.
56-
if [[ -z "$arg" ]]; then
57-
break
30+
__go_argv=()
31+
__go_cmd_path=()
32+
33+
for ((i = "$#"; i > 0; i--)); do
34+
for try_path in "${_GO_SEARCH_PATHS[@]}"; do
35+
path_suffix="$(printf '%s.d/' "${@:1:i}")"
36+
path_suffix="${path_suffix%.d/}"
37+
try_path="$try_path/$path_suffix"
38+
39+
if [[ -f "$try_path" && -x "$try_path" ]]; then
40+
__go_cmd_path+=("$try_path")
41+
if [[ "${#__go_argv[@]}" -eq 0 ]]; then
42+
__go_cmd_name=("${@:1:i}")
43+
__go_argv=("${@:i+1}")
44+
fi
5845
fi
59-
60-
try_path="${cmd_paths[$i]}.d/$arg"
61-
62-
if [[ ! -e "$try_path" ]]; then
63-
break
64-
elif [[ ! (-f "$try_path" && -x "$try_path") ]]; then
65-
break
66-
fi
67-
68-
cmd_paths[$i]="$try_path"
69-
70-
cmd_name+=("$arg")
71-
cmd_arg_index=cmd_arg_index+1
7246
done
7347

74-
if [[ "${#cmd_name[@]}" -gt "${#__go_cmd_name[@]}" ]]; then
75-
longest_path="$i"
76-
__go_cmd_name=("${cmd_name[@]}")
77-
__go_argv=("${cmd_args[@]:cmd_arg_index}")
48+
if [[ "${#__go_cmd_path[@]}" -ne 0 ]]; then
49+
break
7850
fi
79-
80-
i=i+1
8151
done
8252

8353
# The command that is the most nested one takes precedence. Eg
8454
# `scripts/foobar/aaa/bbb/ccc arg1 arg2` takes precedence over
8555
# `scripts/foobar/aaa bbb ccc arg1 arg2`.
86-
__go_cmd_path=('' "${cmd_paths[@]}")
87-
__go_cmd_path[0]="${cmd_paths[longest_path]}"
88-
unset "__go_cmd_path[longest_path+1]"
89-
__go_cmd_path=("${__go_cmd_path[@]}")
56+
57+
if [[ "${#__go_cmd_name[*]}" -eq 0 ]]; then
58+
printf "Unknown command: $1\n\n" >&2
59+
_@go.list_available_commands "${_GO_SEARCH_PATHS[@]}" >&2
60+
return 1
61+
fi
9062
}

libexec/complete

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ _@go.complete_command() {
5858

5959
if _@go.complete_command_path "$@" \
6060
&& [[ "$(<"${__go_cmd_path[0]}")" =~ $tab_completions_pattern ]]; then
61-
_@go.run_command_script "$__go_cmd_path" --complete \
61+
_@go.run_command_script "${__go_cmd_path[0]}" --complete \
6262
"$__go_complete_word_index" "${__go_argv[@]}"
6363
fi
6464
}

tests/complete.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,23 @@ teardown() {
185185
assert_success "${expected[@]}"
186186
}
187187

188+
@test "$SUITE: get completions only for the longest candidate command" {
189+
create_bats_test_script scripts/foobar
190+
create_bats_test_script scripts/foobar.d/baz 'echo ibaz'
191+
create_bats_test_script scripts-2/foobar
192+
create_bats_test_script scripts-2/foobar.d/baz
193+
create_bats_test_script scripts-2/foobar.d/baz.d/aaa \
194+
'# Tab completions' 'echo bazi'
195+
196+
create_bats_test_script 'go' \
197+
". '$_GO_CORE_DIR/go-core.bash' 'scripts' 'scripts-2'" \
198+
'@go "$@"'
199+
200+
run "$TEST_GO_SCRIPT" complete 2 'foobar' 'baz' ''
201+
assert_success
202+
assert_output_matches aaa
203+
}
204+
188205
@test "$SUITE: -h, -help, and --help invoke help command completion" {
189206
run "$TEST_GO_SCRIPT" complete 1 -h 'complet'
190207
assert_success 'complete '

0 commit comments

Comments
 (0)