Skip to content

Commit fe4c33c

Browse files
authored
fix: preserve bash wrapper completion context (#159)
1 parent 5afbfa8 commit fe4c33c

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

lib/commands/init.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ __FUNC__() {
229229
}
230230
231231
# Completion for __FUNC__ wrapper
232+
___FUNC___delegate_completion() {
233+
local _gtr_wrapper_prefix="__FUNC__ "
234+
local _gtr_delegate_prefix="git gtr "
235+
COMP_WORDS=(git gtr "${COMP_WORDS[@]:1}")
236+
(( COMP_CWORD += 1 ))
237+
COMP_LINE="$_gtr_delegate_prefix${COMP_LINE#$_gtr_wrapper_prefix}"
238+
(( COMP_POINT += ${#_gtr_delegate_prefix} - ${#_gtr_wrapper_prefix} ))
239+
_git_gtr
240+
}
241+
232242
___FUNC___completion() {
233243
local cur
234244
cur="${COMP_WORDS[COMP_CWORD]}"
@@ -243,16 +253,12 @@ ___FUNC___completion() {
243253
COMPREPLY=($(compgen -W "$worktrees" -- "$cur"))
244254
elif [ "${COMP_WORDS[1]}" = "new" ] && [[ "$cur" == -* ]]; then
245255
if type _git_gtr &>/dev/null; then
246-
COMP_WORDS=(git gtr "${COMP_WORDS[@]:1}")
247-
(( COMP_CWORD += 1 ))
248-
_git_gtr
256+
___FUNC___delegate_completion
249257
fi
250258
COMPREPLY+=($(compgen -W "--cd" -- "$cur"))
251259
elif type _git_gtr &>/dev/null; then
252260
# Delegate to git-gtr completions (adjust words to match expected format)
253-
COMP_WORDS=(git gtr "${COMP_WORDS[@]:1}")
254-
(( COMP_CWORD += 1 ))
255-
_git_gtr
261+
___FUNC___delegate_completion
256262
fi
257263
}
258264
complete -F ___FUNC___completion __FUNC__

tests/init.bats

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,62 @@ teardown() {
1414
rm -rf "$BATS_TMPDIR/gtr-init-cache-$$"
1515
}
1616

17+
run_generated_bash_wrapper_completion() {
18+
local wrapper_name="$1"
19+
local comp_line="$2"
20+
local comp_cword="$3"
21+
local comp_point="$4"
22+
shift 4
23+
24+
bash -s -- "$PROJECT_ROOT" "$XDG_CACHE_HOME" "$wrapper_name" "$comp_line" "$comp_cword" "$comp_point" "$@" <<'BASH'
25+
set -euo pipefail
26+
27+
PROJECT_ROOT="$1"
28+
XDG_CACHE_HOME="$2"
29+
wrapper_name="$3"
30+
comp_line="$4"
31+
comp_cword="$5"
32+
comp_point="$6"
33+
shift 6
34+
words=("$@")
35+
36+
export XDG_CACHE_HOME
37+
export GTR_VERSION="test"
38+
39+
log_info() { :; }
40+
log_warn() { :; }
41+
log_error() { :; }
42+
show_command_help() { :; }
43+
44+
# shellcheck disable=SC1090
45+
. "$PROJECT_ROOT/lib/commands/init.sh"
46+
47+
if [ "$wrapper_name" = "gtr" ]; then
48+
eval "$(cmd_init bash)"
49+
else
50+
eval "$(cmd_init bash --as "$wrapper_name")"
51+
fi
52+
53+
_git_gtr() {
54+
printf 'WORDS=%s\n' "${COMP_WORDS[*]}"
55+
printf 'CWORD=%s\n' "$COMP_CWORD"
56+
printf 'LINE=%s\n' "$COMP_LINE"
57+
printf 'POINT=%s\n' "$COMP_POINT"
58+
COMPREPLY=(--from)
59+
}
60+
61+
completion_fn="_${wrapper_name}_completion"
62+
COMP_WORDS=("${words[@]}")
63+
COMP_CWORD="$comp_cword"
64+
COMP_LINE="$comp_line"
65+
COMP_POINT="$comp_point"
66+
COMPREPLY=()
67+
"$completion_fn"
68+
69+
printf 'REPLY=%s\n' "${COMPREPLY[*]}"
70+
BASH
71+
}
72+
1773
# ── Default function name ────────────────────────────────────────────────────
1874

1975
@test "bash output defines gtr() function by default" {
@@ -211,6 +267,39 @@ teardown() {
211267
[[ "$output" == *'compgen -W "--cd"'* ]]
212268
}
213269
270+
@test "bash wrapper completion rewrites delegated context for ai targets" {
271+
run run_generated_bash_wrapper_completion gtr "gtr ai fe" 2 9 gtr ai fe
272+
273+
[ "$status" -eq 0 ]
274+
[[ "$output" == *"WORDS=git gtr ai fe"* ]]
275+
[[ "$output" == *"CWORD=3"* ]]
276+
[[ "$output" == *"LINE=git gtr ai fe"* ]]
277+
[[ "$output" == *"POINT=13"* ]]
278+
[[ "$output" == *"REPLY=--from"* ]]
279+
}
280+
281+
@test "bash wrapper completion rewrites delegated context for custom wrapper names" {
282+
run run_generated_bash_wrapper_completion gwtr "gwtr ai fe" 2 10 gwtr ai fe
283+
284+
[ "$status" -eq 0 ]
285+
[[ "$output" == *"WORDS=git gtr ai fe"* ]]
286+
[[ "$output" == *"CWORD=3"* ]]
287+
[[ "$output" == *"LINE=git gtr ai fe"* ]]
288+
[[ "$output" == *"POINT=13"* ]]
289+
[[ "$output" == *"REPLY=--from"* ]]
290+
}
291+
292+
@test "bash wrapper completion preserves delegated replies and appends --cd for new flags" {
293+
run run_generated_bash_wrapper_completion gtr "gtr new --c" 2 11 gtr new --c
294+
295+
[ "$status" -eq 0 ]
296+
[[ "$output" == *"WORDS=git gtr new --c"* ]]
297+
[[ "$output" == *"CWORD=3"* ]]
298+
[[ "$output" == *"LINE=git gtr new --c"* ]]
299+
[[ "$output" == *"POINT=15"* ]]
300+
[[ "$output" == *"REPLY=--from --cd"* ]]
301+
}
302+
214303
@test "zsh wrapper completions include --cd for new" {
215304
run cmd_init zsh
216305
[ "$status" -eq 0 ]

0 commit comments

Comments
 (0)