Skip to content

Commit b577711

Browse files
authored
Merge pull request #886 from akinomyoga/refactor-funcname-2
refactor(bash_completion): rename `_get_comp_words_by_ref`, etc.
2 parents 9e294a7 + e76ca14 commit b577711

File tree

10 files changed

+76
-69
lines changed

10 files changed

+76
-69
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repos:
1212
hooks:
1313
- id: shfmt-docker
1414
types: [text]
15-
files: ^(bash_completion|completions/.+|test/(config/bashrc|update-test-cmd-list)|.+\.sh(\.in)?)$
15+
files: ^(bash_completion(\.d/[0-9]{3}_.+)?|completions/.+|test/(config/bashrc|update-test-cmd-list)|.+\.sh(\.in)?)$
1616
exclude: ^completions/(\.gitignore|Makefile.*)$
1717

1818
- repo: https://github.com/shellcheck-py/shellcheck-py
@@ -21,7 +21,7 @@ repos:
2121
- id: shellcheck
2222
args: [-f, gcc]
2323
types: [text]
24-
files: ^(bash_completion|completions/.+|test/(config/bashrc|update-test-cmd-list)|.+\.sh(\.in)?)$
24+
files: ^(bash_completion(\.d/[0-9]{3}_.+)?|completions/.+|test/(config/bashrc|update-test-cmd-list)|.+\.sh(\.in)?)$
2525
exclude: ^completions/(\.gitignore|Makefile.*)$
2626
require_serial: false # We disable SC1090 anyway, so parallel is ok
2727

bash_completion

Lines changed: 20 additions & 34 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_words()
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_words()
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_words()
467+
_comp__get_cword_at_cursor()
468468
{
469469
local cword words=()
470-
__reassemble_comp_words_by_ref "$1" words cword
470+
_comp__reassemble_words "$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_words [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_words -n : cur prev
531531
#
532-
_get_comp_words_by_ref()
532+
_comp_get_words()
533533
{
534534
local exclude="" flag i OPTIND=1
535535
local cur cword words=()
@@ -569,7 +569,7 @@ _get_comp_words_by_ref()
569569
((OPTIND += 1))
570570
done
571571

572-
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
572+
_comp__get_cword_at_cursor "${exclude-}" words cword cur
573573

574574
[[ $vcur ]] && {
575575
upvars+=("$vcur")
@@ -588,21 +588,7 @@ _get_comp_words_by_ref()
588588
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
589589
}
590590

591-
((${#upvars[@]})) && local "${upvars[@]}" && _upvars "${upargs[@]}"
592-
}
593-
594-
# Get word previous to the current word.
595-
# This is a good alternative to `prev=${COMP_WORDS[COMP_CWORD-1]}' because bash4
596-
# will properly return the previous word with respect to any given exclusions to
597-
# COMP_WORDBREAKS.
598-
# @deprecated Use `_get_comp_words_by_ref cur prev' instead
599-
# @see _get_comp_words_by_ref()
600-
#
601-
_get_pword()
602-
{
603-
if ((COMP_CWORD >= 1)); then
604-
_get_cword "${@-}" 1
605-
fi
591+
((${#upvars[@]})) && local "${upvars[@]}" && _comp_upvars "${upargs[@]}"
606592
}
607593

608594
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
@@ -667,7 +653,7 @@ _quote_readline_by_ref()
667653
value=${value//'%'/%%} # Escape % for printf format.
668654
# shellcheck disable=SC2059
669655
printf -v value "$value" # Decode escape sequences of \....
670-
local "$2" && _upvars -v "$2" "$value"
656+
local "$2" && _comp_upvars -v "$2" "$value"
671657
fi
672658
fi
673659
} # _quote_readline_by_ref()
@@ -914,7 +900,7 @@ _comp_variable_assignments()
914900
# cur, prev, words, and cword are local, ditto split if you use -s.
915901
#
916902
# Options:
917-
# -n EXCLUDE Passed to _get_comp_words_by_ref -n with redirection chars
903+
# -n EXCLUDE Passed to _comp_get_words -n with redirection chars
918904
# -e XSPEC Passed to _filedir as first arg for stderr redirections
919905
# -o XSPEC Passed to _filedir as first arg for other output redirections
920906
# -i XSPEC Passed to _filedir as first arg for stdin redirections
@@ -962,7 +948,7 @@ _comp_initialize()
962948

963949
COMPREPLY=()
964950
local redir='@(?(+([0-9])|{[a-zA-Z_]*([a-zA-Z_0-9])})@(>?([>|&])|<?([>&])|<<?([-<]))|&>?(>))'
965-
_get_comp_words_by_ref -n "$exclude<>&" cur prev words cword
951+
_comp_get_words -n "$exclude<>&" cur prev words cword
966952

967953
# Complete variable names.
968954
_variables && return 1
@@ -1701,7 +1687,7 @@ _realcommand()
17011687

17021688
# This function returns the first argument, excluding options
17031689
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
1704-
# NOT be considered word breaks. See __reassemble_comp_words_by_ref.
1690+
# NOT be considered word breaks. See _comp__reassemble_words.
17051691
_get_first_arg()
17061692
{
17071693
local i
@@ -1717,13 +1703,13 @@ _get_first_arg()
17171703

17181704
# This function counts the number of args, excluding options
17191705
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
1720-
# NOT be considered word breaks. See __reassemble_comp_words_by_ref.
1706+
# NOT be considered word breaks. See _comp__reassemble_words.
17211707
# @param $2 glob Options whose following argument should not be counted
17221708
# @param $3 glob Options that should be counted as args
17231709
_count_args()
17241710
{
17251711
local i cword words
1726-
__reassemble_comp_words_by_ref "${1-}" words cword
1712+
_comp__reassemble_words "${1-}" words cword
17271713

17281714
args=1
17291715
for ((i = 1; i < cword; i++)); do
@@ -2188,7 +2174,7 @@ _comp_command_offset()
21882174

21892175
COMPREPLY=()
21902176
local cur
2191-
_get_comp_words_by_ref cur
2177+
_comp_get_words cur
21922178

21932179
if ((COMP_CWORD == 0)); then
21942180
local IFS=$'\n'

bash_completion.d/000_bash_completion_compat

Lines changed: 25 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_words
14+
_comp_deprecate_func __get_cword_at_cursor_by_ref _comp__get_cword_at_cursor
15+
_comp_deprecate_func _get_comp_words_by_ref _comp_get_words
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_words cur' instead
87+
# @see _comp_get_words()
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_words "${1-}" words cword
8893

8994
# return previous word offset by $2
9095
if [[ ${2-} && ${2//[^0-9]/} ]]; then
@@ -125,8 +130,23 @@ _get_cword()
125130
fi
126131
} # _get_cword()
127132

133+
# Get word previous to the current word.
134+
# This is a good alternative to `prev=${COMP_WORDS[COMP_CWORD-1]}' because bash4
135+
# will properly return the previous word with respect to any given exclusions to
136+
# COMP_WORDBREAKS.
137+
# @deprecated Use `_comp_get_words cur prev' instead
138+
# @see _comp_get_words()
139+
#
140+
_get_pword()
141+
{
142+
if ((COMP_CWORD >= 1)); then
143+
_get_cword "${@-}" 1
144+
fi
145+
}
146+
128147
# @deprecated Use the variable `_comp_backup_glob` instead. This is the
129148
# backward-compatibility name.
149+
# shellcheck disable=SC2154 # defined in the main "bash_completion"
130150
_backup_glob=$_comp_backup_glob
131151

132152
# ex: filetype=sh

completions/ssh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ _ssh_suboption()
217217
_comp_xfunc_ssh_suboption_check()
218218
{
219219
# Get prev and cur words without splitting on =
220-
local cureq=$(_get_cword :=) preveq=$(_get_pword :=)
221-
if [[ $cureq == *=* && $preveq == -*o ]]; then
222-
_ssh_suboption "$cureq" "${1-}"
220+
local cur prev
221+
_comp_get_words -n := cur prev
222+
if [[ $cur == *=* && $prev == -*o ]]; then
223+
_ssh_suboption "$cur" "${1-}"
223224
return $?
224225
fi
225226
return 1

completions/xsltproc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _xsltproc()
3434
;;
3535
esac
3636

37-
[[ $cword -gt 2 && $(_get_cword '' 2) == --?(string)param ]] && return
37+
[[ $cword -gt 2 && ${words[cword - 2]} == --?(string)param ]] && return
3838

3939
if [[ $cur == -* ]]; then
4040
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))

test/t/test_umount.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def dummy_mnt(self, request, bash):
1919
assert_bash_exec(
2020
bash,
2121
"_mnt_completion() { "
22-
"local cur=$(_get_cword); "
23-
"_comp_cmd_umount__linux_fstab $(_get_pword) < mount/test-fstab; "
22+
"local cur prev;_comp_get_words cur prev; "
23+
'_comp_cmd_umount__linux_fstab "$prev" < mount/test-fstab; '
2424
"} && complete -F _mnt_completion _mnt",
2525
)
2626
request.addfinalizer(

test/t/unit/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ EXTRA_DIST = \
88
test_unit_expand_tilde_by_ref.py \
99
test_unit_filedir.py \
1010
test_unit_find_unique_completion_pair.py \
11-
test_unit_get_comp_words_by_ref.py \
11+
test_unit_get_words.py \
1212
test_unit_get_cword.py \
1313
test_unit_initialize.py \
1414
test_unit_ip_addresses.py \

test/t/unit/test_unit_filedir.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class TestUnitFiledir:
1515
def functions(self, request, bash):
1616
assert_bash_exec(
1717
bash,
18-
"_f() { local cur=$(_get_cword); unset -v COMPREPLY; _filedir; }; "
18+
"_f() { local cur;_comp_get_words cur; unset -v COMPREPLY; _filedir; }; "
1919
"complete -F _f f; "
2020
"complete -F _f -o filenames f2",
2121
)
2222
assert_bash_exec(
2323
bash,
24-
"_g() { local cur=$(_get_cword); unset -v COMPREPLY; _filedir e1; }; "
24+
"_g() { local cur;_comp_get_words cur; unset -v COMPREPLY; _filedir e1; }; "
2525
"complete -F _g g",
2626
)
2727
assert_bash_exec(
2828
bash,
29-
"_fd() { local cur=$(_get_cword); unset -v COMPREPLY; _filedir -d; };"
29+
"_fd() { local cur;_comp_get_words cur; unset -v COMPREPLY; _filedir -d; };"
3030
"complete -F _fd fd",
3131
)
3232

0 commit comments

Comments
 (0)