diff --git a/contrib/ri b/contrib/ri index f79de0a0..4c144239 100644 --- a/contrib/ri +++ b/contrib/ri @@ -1,49 +1,73 @@ # ri completion for Ruby documentation by Ian Macdonald # -# $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() { - COMPREPLY=( ${COMPREPLY[@]} \ - "$( ri ${classes[@]} | ruby -ane 'if /^-/.../^-/ and \ - ! /^-/ and ! /^ +(class|module): / then \ - print $_.strip.split(/, /).grep(/^[^\[]*$/).join("\n"); \ - end' | sort -u )" ) + if [ "$ri_version" = integrated ]; then + # only copes with instance methods at the moment + COMPREPLY=( ${COMPREPLY[@]} \ + "$( 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 - # we're completing on class#method - class=${cur%#*} - method=${cur#*#} - classes=( $class ) - prefix="-P $class#" - ri_get_methods - return 0 + # we're completing on class#method + class=${cur%#*} + method=${cur#*#} + classes=( $class ) + prefix="-P $class#" + ri_get_methods + return 0 fi - if [ "$( ri -v 2>&1 )" = "ri 1.8a" ]; then - classes=( $( ri | ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \ - if /^ .*[A-Z]/ then print; end; end' )) + 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 \ - if /^ .*[A-Z]/ then print; end; end' )) + 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 - return 0 + # we're completing on class or module alone + return 0 fi # we're completing on methods