Skip to content

Commit 0a9d931

Browse files
committed
fix(ri): avoid interference on existing COMPREPLY
The current implementation of `_comp_cmd_ri__methods` prefixes $prefix to the generated method names. However, this prefixing also affects the already generated completions in COMPREPLY because it first generates the method names by appending them to COMPRELY and then modifies all the elements of COMPREPLY. In the current usage, it did not produce the problem because prefix is empty when there are existing elements in COMPREPLY, but it's accidentally. Before refactoring the code, we want to resolve this dependency on the subtle condition.
1 parent 116e9fa commit 0a9d931

File tree

1 file changed

+16
-14
lines changed
  • completions

1 file changed

+16
-14
lines changed

completions/ri

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
# ri completion for Ruby documentation -*- shell-script -*-
22
# by Ian Macdonald <[email protected]>
33

4-
_comp_cmd_ri__methods()
4+
# @var[in] ri_version
5+
# @var[in] prefix
6+
# @var[in] classes
7+
_comp_cmd_ri__compgen_methods()
58
{
6-
local regex
9+
local _regex
710
local IFS=$' \t\n' # needed for ${classes[@]+"${classes[@]}"} in bash-5.2
811

12+
local _methods
913
if [[ $ri_version == integrated ]]; then
1014
if [[ ! $separator ]]; then
11-
regex="(Instance|Class)"
15+
_regex="(Instance|Class)"
1216
elif [[ $separator == "#" ]]; then
13-
regex=Instance
17+
_regex=Instance
1418
else
15-
regex=Class
19+
_regex=Class
1620
fi
1721

18-
_comp_split -la COMPREPLY \
22+
_comp_split -la _methods \
1923
"$(ri ${classes[@]+"${classes[@]}"} 2>/dev/null | ruby -ane \
20-
'if /^'"$regex"' methods:/.../^------------------|^$/ and \
24+
'if /^'"$_regex"' methods:/.../^------------------|^$/ and \
2125
/^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \
2226
end' 2>/dev/null | sort -u)"
2327
else
2428
# older versions of ri didn't distinguish between class/module and
2529
# instance methods
26-
_comp_split -la COMPREPLY \
30+
_comp_split -la _methods \
2731
"$(ruby -W0 "$ri_path" ${classes[@]+"${classes[@]}"} 2>/dev/null | ruby -ane \
2832
'if /^-/.../^-/ and ! /^-/ and ! /^ +(class|module): / then \
2933
print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \
3034
end' | sort -u)"
31-
fi
32-
((${#COMPREPLY[@]})) &&
33-
_comp_compgen -c "$method" -- -P "$prefix" -W '"${COMPREPLY[@]}"'
35+
fi &&
36+
_comp_compgen -- -P "$prefix" -W '"${_methods[@]}"'
3437
}
3538

3639
# needs at least Ruby 1.8.0 in order to use -W0
@@ -87,7 +90,7 @@ _comp_cmd_ri()
8790
method=${cur#*"$separator"}
8891
classes=($class)
8992
prefix=$class$separator
90-
_comp_cmd_ri__methods
93+
_comp_compgen -c "$method" -i ri methods
9194
return
9295
fi
9396

@@ -117,8 +120,7 @@ _comp_cmd_ri()
117120
fi
118121

119122
# we're completing on methods
120-
method=$cur
121-
_comp_cmd_ri__methods
123+
_comp_cmd_ri__compgen_methods
122124
} &&
123125
complete -F _comp_cmd_ri ri
124126

0 commit comments

Comments
 (0)