- 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,49 +1,73 @@
# ri completion for Ruby documentation by Ian Macdonald <ian@caliban.org> # 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() ri_get_methods()
{ {
COMPREPLY=( ${COMPREPLY[@]} \ if [ "$ri_version" = integrated ]; then
"$( ri ${classes[@]} | ruby -ane 'if /^-/.../^-/ and \ # only copes with instance methods at the moment
! /^-/ and ! /^ +(class|module): / then \ COMPREPLY=( ${COMPREPLY[@]} \
print $_.strip.split(/, /).grep(/^[^\[]*$/).join("\n"); \ "$( ri ${classes[@]} 2>/dev/null | \
end' | sort -u )" ) 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 ) ) COMPREPLY=( $( compgen $prefix -W '${COMPREPLY[@]}' -- $method ) )
} }
# needs at least Ruby 1.8.0
_ri() _ri()
{ {
local cur class method prefix IFS local cur class method prefix ri_path ri_version IFS
local -a classes local -a classes
COMPREPLY=() COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]} 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 # need to also split on commas
IFS=$', \n\t' IFS=$', \n\t'
if [[ "$cur" == [A-Z]*#* ]]; then if [[ "$cur" == [A-Z]*#* ]]; then
# we're completing on class#method # we're completing on class#method
class=${cur%#*} class=${cur%#*}
method=${cur#*#} method=${cur#*#}
classes=( $class ) classes=( $class )
prefix="-P $class#" prefix="-P $class#"
ri_get_methods ri_get_methods
return 0 return 0
fi fi
if [ "$( ri -v 2>&1 )" = "ri 1.8a" ]; then if [ "$ri_version" = integrated ]; then
classes=( $( ri | ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \ # integrated ri from Ruby 1.9
if /^ .*[A-Z]/ then print; end; end' )) 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 else
classes=( $( ri | ruby -ne 'if /^I have/..$stdin.eof then \ classes=( $( ruby -W0 $ri_path | \
if /^ .*[A-Z]/ then print; end; end' )) ruby -ne 'if /^I have/..$stdin.eof then \
if /^ .*[A-Z]/ then print; end; end' ))
fi fi
COMPREPLY=( $( compgen -W '${classes[@]}' -- $cur ) ) COMPREPLY=( $( compgen -W '${classes[@]}' -- $cur ) )
if [[ "$cur" == [A-Z]* ]]; then if [[ "$cur" == [A-Z]* ]]; then
# we're completing on class alone # we're completing on class or module alone
return 0 return 0
fi fi
# we're completing on methods # we're completing on methods