Skip to content

Allows users to set their own tab completions pattern #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion libexec/complete
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
# scripts, your script must contain a `# Tab completions` comment and respond to
# the `--complete` command line interface described above.
#
# - You can change the `# Tab completions` hint to a different pattern using the
# GO_TAB_COMPLETIONS_PATTERN variable.
#
# - The argument list must/will always contain at least one element, even if
# it is the empty string, which represents the user completing an argument
# without typing anything first. Take this into account when implementing
Expand All @@ -51,7 +54,14 @@ [email protected]_command() {
local __go_complete_word_index
local __go_cmd_path
local __go_argv
local tab_completions_pattern='# [Tt]ab [Cc]ompletions['$'\n\r'']'
local tab_completions_pattern

local default_pattern='# [Tt]ab [Cc]ompletions['$'\n\r'']'
if [[ "${GO_TAB_COMPLETIONS_PATTERN-__undefined__}" == '__undefined__' ]]; then
tab_completions_pattern="$default_pattern"
else
tab_completions_pattern="$GO_TAB_COMPLETIONS_PATTERN"
fi

. "$_GO_CORE_DIR/lib/internal/complete"
exec 2>/dev/null
Expand Down
19 changes: 19 additions & 0 deletions tests/complete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ teardown() {
assert_failure ''
}

@test "$SUITE: change tab completion patter" {
@go.create_test_go_script \
'GO_TAB_COMPLETIONS_PATTERN="# New tab completions pattern"' \
'@go "$@"'

@go.create_test_command_script foo \
'if [[ "$1" == "--complete" ]]; then ' \
' # New tab completions pattern' \
' echo "bar" "baz" "quux"' \
'fi'

run "$TEST_GO_SCRIPT" complete 0 foo
assert_success 'foo '

local expected=('bar' 'baz' 'quux')
run "$TEST_GO_SCRIPT" complete 1 foo ''
assert_success "${expected[@]}"
}

@test "$SUITE: command script completion not detected without comment" {
@go.create_test_command_script foo \
'if [[ "$1" == "--complete" ]]; then ' \
Expand Down