Skip to content

Commit e426f4e

Browse files
committed
fix(complete): Improve handling of current word in Bash
Fixes #5979.
1 parent d522921 commit e426f4e

File tree

16 files changed

+108
-38
lines changed

16 files changed

+108
-38
lines changed

clap_complete/src/aot/shells/bash.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ impl Generator for Bash {
2525
"_{name}() {{
2626
local i cur prev opts cmd
2727
COMPREPLY=()
28-
cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
29-
prev=\"${{COMP_WORDS[COMP_CWORD-1]}}\"
28+
if [[ \"${{BASH_VERSINFO[0]}}\" -ge 4 ]]; then
29+
cur=\"$2\"
30+
else
31+
cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
32+
fi
33+
prev=\"$3\"
3034
cmd=\"\"
3135
opts=\"\"
3236

clap_complete/src/env/shells.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ _clap_complete_NAME() {
3737
else
3838
local _CLAP_COMPLETE_SPACE=true
3939
fi
40+
local words=("${COMP_WORDS[@]}")
41+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
42+
words[COMP_CWORD]="$2"
43+
fi
4044
COMPREPLY=( $( \
4145
_CLAP_IFS="$IFS" \
4246
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
4347
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
4448
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
4549
VAR="bash" \
46-
"COMPLETER" -- "${COMP_WORDS[@]}" \
50+
"COMPLETER" -- "${words[@]}" \
4751
) )
4852
if [[ $? != 0 ]]; then
4953
unset COMPREPLY

clap_complete/tests/snapshots/aliases.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_my-app() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

clap_complete/tests/snapshots/basic.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_my-app() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

clap_complete/tests/snapshots/custom_bin_name.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_bin-name() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

clap_complete/tests/snapshots/feature_sample.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_my-app() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

clap_complete/tests/snapshots/home/dynamic-env/exhaustive/bash/.bashrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ _clap_complete_exhaustive() {
1010
else
1111
local _CLAP_COMPLETE_SPACE=true
1212
fi
13+
local words=("${COMP_WORDS[@]}")
14+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
15+
words[COMP_CWORD]="$2"
16+
fi
1317
COMPREPLY=( $( \
1418
_CLAP_IFS="$IFS" \
1519
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
1620
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
1721
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
1822
COMPLETE="bash" \
19-
"exhaustive" -- "${COMP_WORDS[@]}" \
23+
"exhaustive" -- "${words[@]}" \
2024
) )
2125
if [[ $? != 0 ]]; then
2226
unset COMPREPLY

clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ PS1='% '
33
_exhaustive() {
44
local i cur prev opts cmd
55
COMPREPLY=()
6-
cur="${COMP_WORDS[COMP_CWORD]}"
7-
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
7+
cur="$2"
8+
else
9+
cur="${COMP_WORDS[COMP_CWORD]}"
10+
fi
11+
prev="$3"
812
cmd=""
913
opts=""
1014

clap_complete/tests/snapshots/quoting.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_my-app() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

clap_complete/tests/snapshots/special_commands.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
_my-app() {
22
local i cur prev opts cmd
33
COMPREPLY=()
4-
cur="${COMP_WORDS[COMP_CWORD]}"
5-
prev="${COMP_WORDS[COMP_CWORD-1]}"
4+
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
5+
cur="$2"
6+
else
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
fi
9+
prev="$3"
610
cmd=""
711
opts=""
812

0 commit comments

Comments
 (0)