Skip to content

Commit 6d14916

Browse files
Vampirescop
authored andcommitted
fix: cword calculation w/custom wordbreaks and trailing whitespace
cword calculation was done wrongly with custom wordbreaks, if there was only trailing whitespace after the caret, i.e. no additional words.
1 parent 9cde158 commit 6d14916

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

bash_completion

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,16 @@ __reassemble_comp_words_by_ref()
433433
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
434434
# Remove optional whitespace + word separator from line copy
435435
line=${line#*"${COMP_WORDS[i]}"}
436-
# Start new word if word separator in original line is
437-
# followed by whitespace.
438-
[[ $line == [[:blank:]]* ]] && ((j++))
439436
# Indicate next word if available, else end *both* while and
440437
# for loop
441438
if ((i < ${#COMP_WORDS[@]} - 1)); then
442439
((i++))
443440
else
444441
break 2
445442
fi
443+
# Start new word if word separator in original line is
444+
# followed by whitespace.
445+
[[ $line == [[:blank:]]* ]] && ((j++))
446446
done
447447
# Append word to current word
448448
ref="$2[$j]"

test/t/unit/test_unit_get_cword.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,12 @@ def test_24(self, bash):
153153
]
154154
)
155155
assert got == 1
156+
157+
def test_25(self, bash):
158+
"""
159+
a b c:| with trailing whitespace after the caret (no more words) and
160+
with WORDBREAKS -= : should return c:
161+
"""
162+
assert_bash_exec(bash, "add_comp_wordbreak_char :")
163+
output = self._test(bash, "(a b c :)", 3, "a b c: ", 6, arg=":")
164+
assert output == "c:"

0 commit comments

Comments
 (0)