Skip to content

Commit a9fb232

Browse files
committed
refactor: { => _comp}{_upvars,_get_comp_words_by_ref,...}
* refactor: `{ => _comp}_upvars` * refactor: `{ => _comp}__reassemble_comp_words_by_ref` * refactor: `{ => _comp}__get_cword_at_cursor_by_ref` * refactor: `{ => _comp}_get_comp_words_by_ref`
1 parent cb78194 commit a9fb232

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

bash_completion

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ _comp_unlocal()
223223

224224
# Assign variables one scope above the caller
225225
# Usage: local varname [varname ...] &&
226-
# _upvars [-v varname value] | [-aN varname [value ...]] ...
226+
# _comp_upvars [-v varname value] | [-aN varname [value ...]] ...
227227
# Available OPTIONS:
228228
# -aN Assign next N values to varname as array
229229
# -v Assign single value to varname
230230
# @return 1 if error occurs
231231
# @see https://fvue.nl/wiki/Bash:_Passing_variables_by_reference
232-
_upvars()
232+
_comp_upvars()
233233
{
234234
if ! (($#)); then
235235
echo "bash_completion: $FUNCNAME: usage: $FUNCNAME" \
@@ -394,7 +394,7 @@ _comp_looks_like_path()
394394
# @param $2 words Name of variable to return words to
395395
# @param $3 cword Name of variable to return cword to
396396
#
397-
__reassemble_comp_words_by_ref()
397+
_comp__reassemble_comp_words_by_ref()
398398
{
399399
local exclude i j line ref
400400
# Exclude word separator characters?
@@ -454,7 +454,7 @@ __reassemble_comp_words_by_ref()
454454
printf -v "$2[i]" %s "${COMP_WORDS[i]}"
455455
done
456456
fi
457-
} # __reassemble_comp_words_by_ref()
457+
} # _comp__reassemble_comp_words_by_ref()
458458

459459
# @param $1 exclude Characters out of $COMP_WORDBREAKS which should NOT be
460460
# considered word breaks. This is useful for things like scp where
@@ -463,11 +463,11 @@ __reassemble_comp_words_by_ref()
463463
# @param $2 words Name of variable to return words to
464464
# @param $3 cword Name of variable to return cword to
465465
# @param $4 cur Name of variable to return current word to complete to
466-
# @see __reassemble_comp_words_by_ref()
467-
__get_cword_at_cursor_by_ref()
466+
# @see _comp__reassemble_comp_words_by_ref()
467+
_comp__get_cword_at_cursor_by_ref()
468468
{
469469
local cword words=()
470-
__reassemble_comp_words_by_ref "$1" words cword
470+
_comp__reassemble_comp_words_by_ref "$1" words cword
471471

472472
local i cur="" index=$COMP_POINT lead=${COMP_LINE:0:COMP_POINT}
473473
# Cursor not at position 0 and not led by just space(s)?
@@ -498,7 +498,7 @@ __get_cword_at_cursor_by_ref()
498498
((index < 0)) && index=0
499499
fi
500500

501-
local "$2" "$3" "$4" && _upvars -a${#words[@]} "$2" ${words+"${words[@]}"} \
501+
local "$2" "$3" "$4" && _comp_upvars -a${#words[@]} "$2" ${words+"${words[@]}"} \
502502
-v "$3" "$cword" -v "$4" "${cur:0:index}"
503503
}
504504

@@ -508,7 +508,7 @@ __get_cword_at_cursor_by_ref()
508508
# (For example, if the line is "ls foobar",
509509
# and the cursor is here --------> ^
510510
# Also one is able to cross over possible wordbreak characters.
511-
# Usage: _get_comp_words_by_ref [OPTIONS] [VARNAMES]
511+
# Usage: _comp_get_comp_words_by_ref [OPTIONS] [VARNAMES]
512512
# Available VARNAMES:
513513
# cur Return cur via $cur
514514
# prev Return prev via $prev
@@ -527,9 +527,9 @@ __get_cword_at_cursor_by_ref()
527527
#
528528
# Example usage:
529529
#
530-
# $ _get_comp_words_by_ref -n : cur prev
530+
# $ _comp_get_comp_words_by_ref -n : cur prev
531531
#
532-
_get_comp_words_by_ref()
532+
_comp_get_comp_words_by_ref()
533533
{
534534
local exclude flag i OPTIND=1
535535
local cur cword words=()
@@ -564,7 +564,7 @@ _get_comp_words_by_ref()
564564
((OPTIND += 1))
565565
done
566566

567-
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
567+
_comp__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
568568

569569
[[ -v vcur ]] && {
570570
upvars+=("$vcur")
@@ -583,15 +583,15 @@ _get_comp_words_by_ref()
583583
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
584584
}
585585

586-
((${#upvars[@]})) && local "${upvars[@]}" && _upvars "${upargs[@]}"
586+
((${#upvars[@]})) && local "${upvars[@]}" && _comp_upvars "${upargs[@]}"
587587
}
588588

589589
# Get word previous to the current word.
590590
# This is a good alternative to `prev=${COMP_WORDS[COMP_CWORD-1]}' because bash4
591591
# will properly return the previous word with respect to any given exclusions to
592592
# COMP_WORDBREAKS.
593-
# @deprecated Use `_get_comp_words_by_ref cur prev' instead
594-
# @see _get_comp_words_by_ref()
593+
# @deprecated Use `_comp_get_comp_words_by_ref cur prev' instead
594+
# @see _comp_get_comp_words_by_ref()
595595
#
596596
_get_pword()
597597
{
@@ -662,7 +662,7 @@ _quote_readline_by_ref()
662662
value=${value//'%'/%%} # Escape % for printf format.
663663
# shellcheck disable=SC2059
664664
printf -v value "$value" # Decode escape sequences of \....
665-
local "$2" && _upvars -v "$2" "$value"
665+
local "$2" && _comp_upvars -v "$2" "$value"
666666
fi
667667
fi
668668
} # _quote_readline_by_ref()
@@ -909,7 +909,7 @@ _comp_variable_assignments()
909909
# cur, prev, words, and cword are local, ditto split if you use -s.
910910
#
911911
# Options:
912-
# -n EXCLUDE Passed to _get_comp_words_by_ref -n with redirection chars
912+
# -n EXCLUDE Passed to _comp_get_comp_words_by_ref -n with redirection chars
913913
# -e XSPEC Passed to _filedir as first arg for stderr redirections
914914
# -o XSPEC Passed to _filedir as first arg for other output redirections
915915
# -i XSPEC Passed to _filedir as first arg for stdin redirections
@@ -957,7 +957,7 @@ _comp_initialize()
957957

958958
COMPREPLY=()
959959
local redir='@(?(+([0-9])|{[a-zA-Z_]*([a-zA-Z_0-9])})@(>?([>|&])|<?([>&])|<<?([-<]))|&>?(>))'
960-
_get_comp_words_by_ref -n "$exclude<>&" cur prev words cword
960+
_comp_get_comp_words_by_ref -n "$exclude<>&" cur prev words cword
961961

962962
# Complete variable names.
963963
_variables && return 1
@@ -1696,7 +1696,7 @@ _realcommand()
16961696

16971697
# This function returns the first argument, excluding options
16981698
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
1699-
# NOT be considered word breaks. See __reassemble_comp_words_by_ref.
1699+
# NOT be considered word breaks. See _comp__reassemble_comp_words_by_ref.
17001700
_get_first_arg()
17011701
{
17021702
local i
@@ -1712,13 +1712,13 @@ _get_first_arg()
17121712

17131713
# This function counts the number of args, excluding options
17141714
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
1715-
# NOT be considered word breaks. See __reassemble_comp_words_by_ref.
1715+
# NOT be considered word breaks. See _comp__reassemble_comp_words_by_ref.
17161716
# @param $2 glob Options whose following argument should not be counted
17171717
# @param $3 glob Options that should be counted as args
17181718
_count_args()
17191719
{
17201720
local i cword words
1721-
__reassemble_comp_words_by_ref "${1-}" words cword
1721+
_comp__reassemble_comp_words_by_ref "${1-}" words cword
17221722

17231723
args=1
17241724
for ((i = 1; i < cword; i++)); do
@@ -2177,7 +2177,7 @@ _comp_command_offset()
21772177

21782178
COMPREPLY=()
21792179
local cur
2180-
_get_comp_words_by_ref cur
2180+
_comp_get_comp_words_by_ref cur
21812181

21822182
if ((COMP_CWORD == 0)); then
21832183
local IFS=$'\n'

bash_completion.d/000_bash_completion_compat

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ _comp_deprecate_func _command_offset _comp_command_offset
99
_comp_deprecate_func _command _comp_command
1010
_comp_deprecate_func _root_command _comp_root_command
1111
_comp_deprecate_func _xfunc _comp_xfunc
12+
_comp_deprecate_func _upvars _comp_upvars
13+
_comp_deprecate_func __reassemble_comp_words_by_ref _comp__reassemble_comp_words_by_ref
14+
_comp_deprecate_func __get_cword_at_cursor_by_ref _comp__get_cword_at_cursor_by_ref
15+
_comp_deprecate_func _get_comp_words_by_ref _comp_get_comp_words_by_ref
1216

1317
# Backwards compatibility for compat completions that use have().
1418
# @deprecated should no longer be used; generally not needed with dynamically
@@ -47,14 +51,15 @@ dequote()
4751
# @param $1 Variable name to assign value to
4852
# @param $* Value(s) to assign. If multiple values, an array is
4953
# assigned, otherwise a single value is assigned.
50-
# NOTE: For assigning multiple variables, use '_upvars'. Do NOT
54+
# NOTE: For assigning multiple variables, use '_comp_upvars'. Do NOT
5155
# use multiple '_upvar' calls, since one '_upvar' call might
5256
# reassign a variable to be used by another '_upvar' call.
5357
# @see https://fvue.nl/wiki/Bash:_Passing_variables_by_reference
58+
# @deprecated Use `_comp_upvars' instead
5459
_upvar()
5560
{
5661
echo "bash_completion: $FUNCNAME: deprecated function," \
57-
"use _upvars instead" >&2
62+
"use _comp_upvars instead" >&2
5863
if unset -v "$1"; then # Unset & validate varname
5964
# shellcheck disable=SC2140 # TODO
6065
if (($# == 2)); then
@@ -78,13 +83,13 @@ _upvar()
7883
# current word (default is 0, previous is 1), respecting the exclusions
7984
# given at $1. For example, `_get_cword "=:" 1' returns the word left of
8085
# the current word, respecting the exclusions "=:".
81-
# @deprecated Use `_get_comp_words_by_ref cur' instead
82-
# @see _get_comp_words_by_ref()
86+
# @deprecated Use `_comp_get_comp_words_by_ref cur' instead
87+
# @see _comp_get_comp_words_by_ref()
8388
_get_cword()
8489
{
8590
local LC_CTYPE=C
8691
local cword words
87-
__reassemble_comp_words_by_ref "${1-}" words cword
92+
_comp__reassemble_comp_words_by_ref "${1-}" words cword
8893

8994
# return previous word offset by $2
9095
if [[ ${2-} && ${2//[^0-9]/} ]]; then

test/t/unit/test_unit_get_comp_words_by_ref.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestUnitGetCompWordsByRef(TestUnitBase):
1111
def _test(self, bash, *args, **kwargs):
1212
assert_bash_exec(bash, "unset cur prev")
1313
output = self._test_unit(
14-
"_get_comp_words_by_ref %s cur prev; echo $cur,${prev-}",
14+
"_comp_get_comp_words_by_ref %s cur prev; echo $cur,${prev-}",
1515
bash,
1616
*args,
1717
**kwargs,
@@ -22,7 +22,7 @@ def test_1(self, bash):
2222
assert_bash_exec(
2323
bash,
2424
"COMP_WORDS=() COMP_CWORD= COMP_POINT= COMP_LINE= "
25-
"_get_comp_words_by_ref cur >/dev/null",
25+
"_comp_get_comp_words_by_ref cur >/dev/null",
2626
)
2727

2828
def test_2(self, bash):
@@ -175,7 +175,7 @@ def test_30(self, bash):
175175
"""a b| to all vars"""
176176
assert_bash_exec(bash, "unset words cword cur prev")
177177
output = self._test_unit(
178-
"_get_comp_words_by_ref words cword cur prev%s; "
178+
"_comp_get_comp_words_by_ref words cword cur prev%s; "
179179
'echo "${words[@]}",$cword,$cur,$prev',
180180
bash,
181181
"(a b)",
@@ -189,7 +189,7 @@ def test_31(self, bash):
189189
"""a b| to alternate vars"""
190190
assert_bash_exec(bash, "unset words2 cword2 cur2 prev2")
191191
output = self._test_unit(
192-
"_get_comp_words_by_ref -w words2 -i cword2 -c cur2 -p prev2%s; "
192+
"_comp_get_comp_words_by_ref -w words2 -i cword2 -c cur2 -p prev2%s; "
193193
'echo $cur2,$prev2,"${words2[@]}",$cword2',
194194
bash,
195195
"(a b)",
@@ -204,7 +204,7 @@ def test_32(self, bash):
204204
"""a b : c| with wordbreaks -= :"""
205205
assert_bash_exec(bash, "unset words")
206206
output = self._test_unit(
207-
'_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
207+
'_comp_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
208208
bash,
209209
"(a b : c)",
210210
3,
@@ -217,7 +217,7 @@ def test_33(self, bash):
217217
"""a b: c| with wordbreaks -= :"""
218218
assert_bash_exec(bash, "unset words")
219219
output = self._test_unit(
220-
'_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
220+
'_comp_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
221221
bash,
222222
"(a b : c)",
223223
3,
@@ -230,7 +230,7 @@ def test_34(self, bash):
230230
"""a b :c| with wordbreaks -= :"""
231231
assert_bash_exec(bash, "unset words")
232232
output = self._test_unit(
233-
'_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
233+
'_comp_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
234234
bash,
235235
"(a b : c)",
236236
3,
@@ -243,7 +243,7 @@ def test_35(self, bash):
243243
r"""a b\ :c| with wordbreaks -= :"""
244244
assert_bash_exec(bash, "unset words")
245245
output = self._test_unit(
246-
'_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
246+
'_comp_get_comp_words_by_ref -n : words%s; echo "${words[@]}"',
247247
bash,
248248
"(a 'b ' : c)",
249249
3,
@@ -255,6 +255,6 @@ def test_35(self, bash):
255255
def test_unknown_arg_error(self, bash):
256256
with pytest.raises(AssertionError) as ex:
257257
_ = assert_bash_exec(
258-
bash, "_get_comp_words_by_ref dummy", want_output=True
258+
bash, "_comp_get_comp_words_by_ref dummy", want_output=True
259259
)
260260
ex.match("dummy.* unknown argument")

0 commit comments

Comments
 (0)