- large-scale overhaul:

- now also works with the version of ri that is in the Ruby 1.9 CVS tree
  - works with 1.8.1 when older versions of ri generate warnings
  - some minor code clean-up
This commit is contained in:
ianmacd 2003-12-31 06:48:05 +00:00
parent 19d715515e
commit 8e29a9897c

View File

@ -1,25 +1,43 @@
# ri completion for Ruby documentation by Ian Macdonald <ian@caliban.org>
#
# $Id: ri,v 1.3 2003/11/19 12:19:05 ianmacd Exp $
# $Id: ri,v 1.4 2003/12/31 07:48:05 ianmacd Exp $
#
ri_get_methods()
{
if [ "$ri_version" = integrated ]; then
# only copes with instance methods at the moment
COMPREPLY=( ${COMPREPLY[@]} \
"$( ri ${classes[@]} | ruby -ane 'if /^-/.../^-/ and \
! /^-/ and ! /^ +(class|module): / then \
print $_.strip.split(/, /).grep(/^[^\[]*$/).join("\n"); \
"$( ri ${classes[@]} 2>/dev/null | \
ruby -ane 'if /^Instance methods:/.../^------------------/ and \
/^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \
end' | sort -u )" )
else
# older versions of ri didn't distinguish between class/module and
# instance methods
COMPREPLY=( ${COMPREPLY[@]} \
"$( ruby -W0 $ri_path ${classes[@]} | ruby -ane 'if /^-/.../^-/ and \
! /^-/ and ! /^ +(class|module): / then \
print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \
end' | sort -u )" )
fi
COMPREPLY=( $( compgen $prefix -W '${COMPREPLY[@]}' -- $method ) )
}
# needs at least Ruby 1.8.0
_ri()
{
local cur class method prefix IFS
local cur class method prefix ri_path ri_version IFS
local -a classes
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# which version of ri are we using?
ri_path=$(type -p ri)
# -W0 is required here to stop warnings from older versions of ri
# from being captured when used with Ruby 1.8.1 and later
ri_version="$(ruby -W0 $ri_path -v 2>&1)" || ri_version=integrated
# need to also split on commas
IFS=$', \n\t'
if [[ "$cur" == [A-Z]*#* ]]; then
@ -32,17 +50,23 @@ _ri()
return 0
fi
if [ "$( ri -v 2>&1 )" = "ri 1.8a" ]; then
classes=( $( ri | ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \
if [ "$ri_version" = integrated ]; then
# integrated ri from Ruby 1.9
classes=( $( ri | ruby -ne 'if /I know about:/../For help/ then \
if /, [A-Z]+/ then print; end; end' ) )
elif [ "$ri_version" = "ri 1.8a" ]; then
classes=( $( ruby -W0 $ri_path | \
ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \
if /^ .*[A-Z]/ then print; end; end' ))
else
classes=( $( ri | ruby -ne 'if /^I have/..$stdin.eof then \
classes=( $( ruby -W0 $ri_path | \
ruby -ne 'if /^I have/..$stdin.eof then \
if /^ .*[A-Z]/ then print; end; end' ))
fi
COMPREPLY=( $( compgen -W '${classes[@]}' -- $cur ) )
if [[ "$cur" == [A-Z]* ]]; then
# we're completing on class alone
# we're completing on class or module alone
return 0
fi