Skip to content

Commit 9b1cb54

Browse files
authored
Merge pull request #913 from Rogach/pr/fix-comp-delimited-on-ambiguous-args
fix(_comp_delimited): prepend prefix to all compreply args
2 parents 787ad5c + e3840d0 commit 9b1cb54

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

bash_completion

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,18 @@ _comp_delimited()
895895
_comp_compgen COMPREPLY "$@" -- "${cur##*"$delimiter"}"
896896
fi
897897

898-
((${#COMPREPLY[@]} == 1)) && COMPREPLY=("$prefix$COMPREPLY")
898+
# It would seem that in some specific cases we could avoid adding the
899+
# prefix to all completions, thereby making the list of suggestions
900+
# cleaner, and only adding it when there's exactly one completion.
901+
# The cases where this opportunity has been observed involve having
902+
# `show-all-if-ambiguous` on, but even that has cases where it fails
903+
# and the last separator including everything before it is lost.
904+
# https://github.com/scop/bash-completion/pull/913#issuecomment-1490140309
905+
local i
906+
for i in "${!COMPREPLY[@]}"; do
907+
COMPREPLY[i]="$prefix${COMPREPLY[i]}"
908+
done
909+
899910
[[ $delimiter != : ]] || __ltrim_colon_completions "$cur"
900911
}
901912

test/t/test_pgrep.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ def test_nslist(self, completion):
3131
)
3232
def test_nslist_after_comma(self, completion):
3333
assert completion
34-
assert not any("," in x for x in completion)

test/t/test_ssh_keygen.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ def test_filedir_pub_at_end_of_s(self, completion):
2727
@pytest.mark.complete("ssh-keygen -s foo_key -n foo,")
2828
def test_usernames_for_n(self, completion):
2929
assert completion
30-
assert not any("," in x for x in completion)
3130
# TODO check that these are usernames
3231

3332
@pytest.mark.complete("ssh-keygen -s foo_key -h -n foo,")
3433
def test_host_for_h_n(self, completion):
3534
assert completion
36-
assert not any("," in x for x in completion)
3735
# TODO check that these are hostnames
3836

3937
@pytest.mark.complete("ssh-keygen -Y foo -n ")

test/t/test_tox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_2(self, completion):
1212

1313
@pytest.mark.complete("tox -e foo,", cwd="tox")
1414
def test_3(self, completion):
15-
assert all(x in completion for x in "py37 ALL".split())
15+
assert all("foo," + x in completion for x in "py37 ALL".split())
1616

1717
@pytest.mark.complete("tox -e foo -- ", cwd="tox")
1818
def test_default_after_dashdash(self, completion):

test/t/test_tshark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_2(self, completion):
1414
@pytest.mark.complete("tshark -O foo,htt", require_cmd=True)
1515
def test_3(self, completion):
1616
# p: one completion only; http: e.g. http and http2
17-
assert completion == "p" or "http" in completion
17+
assert completion == "p" or "foo,http" in completion
1818

1919
@pytest.mark.complete("tshark -o tcp", require_cmd=True)
2020
def test_4(self, completion):

test/t/unit/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ EXTRA_DIST = \
33
test_unit_command_offset.py \
44
test_unit_compgen.py \
55
test_unit_count_args.py \
6+
test_unit_delimited.py \
67
test_unit_deprecate_func.py \
78
test_unit_dequote.py \
89
test_unit_expand.py \

test/t/unit/test_unit_delimited.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
3+
from conftest import assert_bash_exec
4+
5+
6+
@pytest.mark.bashcomp(cmd=None)
7+
class TestUnitDelimited:
8+
@pytest.fixture(scope="class")
9+
def functions(self, request, bash):
10+
assert_bash_exec(
11+
bash,
12+
"_comp_cmd_test_delim() {"
13+
" local cur prev words cword comp_args;"
14+
" _comp_get_words cur;"
15+
" _comp_delimited , -W 'alpha beta bravo';"
16+
"};"
17+
"complete -F _comp_cmd_test_delim test_delim",
18+
)
19+
20+
@pytest.mark.complete("test_delim --opt=a")
21+
def test_1(self, functions, completion):
22+
assert completion == ["lpha"]
23+
24+
@pytest.mark.complete("test_delim --opt=b")
25+
def test_2(self, functions, completion):
26+
assert completion == ["beta", "bravo"]
27+
28+
@pytest.mark.complete("test_delim --opt=alpha,b")
29+
def test_3(self, functions, completion):
30+
assert completion == ["alpha,beta", "alpha,bravo"]
31+
32+
@pytest.mark.complete("test_delim --opt=alpha,be")
33+
def test_4(self, functions, completion):
34+
assert completion == ["ta"]
35+
36+
@pytest.mark.complete("test_delim --opt=beta,a")
37+
def test_5(self, functions, completion):
38+
assert completion == ["lpha"]
39+
40+
@pytest.mark.complete("test_delim --opt=c")
41+
def test_6(self, functions, completion):
42+
assert not completion

0 commit comments

Comments
 (0)