1
1
# ri completion for Ruby documentation -*- shell-script -*-
2
2
# by Ian Macdonald <ian@caliban.org>
3
3
4
- _comp_cmd_ri__methods ()
4
+ # @var[in] ri_version
5
+ # @var[in] prefix
6
+ # @var[in] classes
7
+ _comp_cmd_ri__compgen_methods ()
5
8
{
6
- local regex IFS=$' \t\n '
9
+ local _regex
10
+ local IFS=$' \t\n ' # needed for ${classes[@]+"${classes[@]}"} in bash-5.2
7
11
12
+ local _methods
8
13
if [[ $ri_version == integrated ]]; then
9
14
if [[ ! $separator ]]; then
10
- regex =" (Instance|Class)"
15
+ _regex =" (Instance|Class)"
11
16
elif [[ $separator == " #" ]]; then
12
- regex =Instance
17
+ _regex =Instance
13
18
else
14
- regex =Class
19
+ _regex =Class
15
20
fi
16
21
17
- COMPREPLY+=(
22
+ _comp_split -la _methods \
18
23
" $( ri ${classes[@]+" ${classes[@]} " } 2> /dev/null | ruby -ane \
19
- ' if /^' " $regex " ' methods:/.../^------------------|^$/ and \
24
+ ' if /^' " $_regex " ' methods:/.../^------------------|^$/ and \
20
25
/^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \
21
- end' 2> /dev/null | sort -u) " )
26
+ end' 2> /dev/null | sort -u) "
22
27
else
23
28
# older versions of ri didn't distinguish between class/module and
24
29
# instance methods
25
- COMPREPLY+=(
30
+ _comp_split -la _methods \
26
31
" $( ruby -W0 " $ri_path " ${classes[@]+" ${classes[@]} " } 2> /dev/null | ruby -ane \
27
32
' if /^-/.../^-/ and ! /^-/ and ! /^ +(class|module): / then \
28
33
print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \
29
- end' | sort -u) " )
30
- fi
31
- (( ${# COMPREPLY[@]} )) &&
32
- _comp_compgen -c " $method " -- " $prefix " -W ' "${COMPREPLY[@]}"'
34
+ end' | sort -u) "
35
+ fi &&
36
+ _comp_compgen -- -P " $prefix " -W ' "${_methods[@]}"'
33
37
}
34
38
35
39
# needs at least Ruby 1.8.0 in order to use -W0
@@ -66,7 +70,7 @@ _comp_cmd_ri()
66
70
return
67
71
fi
68
72
69
- local class method prefix ri_path ri_version ri_major separator IFS
73
+ local class method prefix= " " ri_path ri_version ri_major= " " separator
70
74
local -a classes
71
75
72
76
ri_path=$( type -p ri)
@@ -78,35 +82,33 @@ _comp_cmd_ri()
78
82
[[ $ri_version =~ ri[[:space:]]v? ([0-9]+) ]] && ri_major=${BASH_REMATCH[1]}
79
83
80
84
# need to also split on commas
81
- IFS=$' , \n\t '
82
85
if [[ $cur == [A-Z]* [# .]* ]]; then
83
86
[[ $cur == * # * ]] && separator=# || separator=.
84
87
# we're completing on class and method
85
88
class= ${cur% " $separator " * }
86
89
method= ${cur#* " $separator " }
87
- classes= ( $class )
88
- prefix= " -P $class$separator "
89
- _comp_cmd_ri__methods
90
+ _comp_split -F $' , \n\t ' classes " $class "
91
+ prefix= $class$separator
92
+ _comp_compgen -c " $method " -i ri methods
90
93
return
91
94
fi
92
95
93
96
if [[ $ri_version == integrated ]]; then
94
97
# integrated ri from Ruby 1.9
95
- classes=($( ri -c 2> /dev/null | ruby -ne ' if /^\s*$/..$stdin.eof then \
96
- if /^ +[A-Z]/ then print; end; end' 2> /dev/null) )
98
+ _comp_split -F $' , \n\t ' classes \
99
+ " $( ri -c 2> /dev/null | ruby -ne ' if /^\s*$/..$stdin.eof then \
100
+ if /^ +[A-Z]/ then print; end; end' 2> /dev/null) "
97
101
elif [[ $ri_major && $ri_major -ge 3 ]]; then
98
- classes=( $( ri -l 2> /dev/null) )
102
+ _comp_split -F $' , \n\t ' classes " $( ri -l 2> /dev/null) "
99
103
elif [[ $ri_version == " ri 1.8a" ]]; then
100
- classes=( $( ruby -W0 " $ri_path " |
104
+ _comp_split -F $' , \n\t ' classes " $( ruby -W0 " $ri_path " |
101
105
ruby -ne ' if /^' " '" ' ri' " '" ' has/..$stdin.eof then \
102
- if /^ .*[A-Z]/ then print; end; end' ) )
106
+ if /^ .*[A-Z]/ then print; end; end' ) "
103
107
else
104
- classes=( $( ruby -W0 " $ri_path " |
108
+ _comp_split -F $' , \n\t ' classes " $( ruby -W0 " $ri_path " |
105
109
ruby -ne ' if /^I have/..$stdin.eof then \
106
- if /^ .*[A-Z]/ then print; end; end' ) )
107
- fi
108
-
109
- (( ${# classes[@]} )) &&
110
+ if /^ .*[A-Z]/ then print; end; end' ) "
111
+ fi &&
110
112
_comp_compgen -- -W ' "${classes[@]}"'
111
113
_comp_ltrim_colon_completions " $cur "
112
114
@@ -116,8 +118,7 @@ _comp_cmd_ri()
116
118
fi
117
119
118
120
# we're completing on methods
119
- method= $cur
120
- _comp_cmd_ri__methods
121
+ _comp_cmd_ri__compgen_methods
121
122
} &&
122
123
complete -F _comp_cmd_ri ri
123
124
0 commit comments