home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / bash / completion-contrib / ri < prev    next >
Encoding:
Text File  |  2006-08-28  |  2.6 KB  |  89 lines

  1. # ri completion for Ruby documentation by Ian Macdonald <ian@caliban.org>
  2. #
  3. # $Id: ri,v 1.8 2006/02/25 14:25:59 ianmacd Exp $
  4. #
  5. ri_get_methods()
  6. {
  7.     local regex
  8.  
  9.     if [ "$ri_version" = integrated ]; then
  10.       if [ -z "$separator" ]; then
  11.         regex="(Instance|Class)"
  12.       elif [ "$separator" = "#" ]; then
  13.         regex=Instance
  14.       else
  15.         regex=Class
  16.       fi
  17.  
  18.       COMPREPLY=( ${COMPREPLY[@]} \
  19.         "$( ri ${classes[@]} 2>/dev/null | \
  20.           ruby -ane 'if /^'"$regex"' methods:/.../^------------------|^$/ and \
  21.           /^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \
  22.           end' | sort -u )" )
  23.     else
  24.       # older versions of ri didn't distinguish between class/module and
  25.       # instance methods
  26.       COMPREPLY=( ${COMPREPLY[@]} \
  27.         "$( ruby -W0 $ri_path ${classes[@]} | ruby -ane 'if /^-/.../^-/ and \
  28.           ! /^-/ and ! /^ +(class|module): / then \
  29.           print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \
  30.           end' | sort -u )" )
  31.     fi
  32.     COMPREPLY=( $( compgen $prefix -W '${COMPREPLY[@]}' -- $method ) )
  33. }
  34.  
  35. # needs at least Ruby 1.8.0 in order to use -W0
  36. _ri()
  37. {
  38.     local cur class method prefix ri_path ri_version separator IFS
  39.     local -a classes
  40.  
  41.     COMPREPLY=()
  42.     cur=${COMP_WORDS[COMP_CWORD]}
  43.  
  44.     ri_path=$(type -p ri)
  45.     # which version of ri are we using?
  46.     # -W0 is required here to stop warnings from older versions of ri
  47.     # from being captured when used with Ruby 1.8.1 and later
  48.     ri_version="$(ruby -W0 $ri_path -v 2>&1)" || ri_version=integrated
  49.     [ "$ri_version" != "${ri_version%200*}" ] && ri_version=integrated
  50.  
  51.     # need to also split on commas
  52.     IFS=$', \n\t'
  53.     if [[ "$cur" == [A-Z]*[#.]* ]]; then
  54.       [[ "$cur" == *#* ]] && separator=# || separator=.
  55.       # we're completing on class and method
  56.       class=${cur%$separator*}
  57.       method=${cur#*$separator}
  58.       classes=( $class )
  59.       prefix="-P $class$separator"
  60.       ri_get_methods 
  61.       return 0
  62.     fi
  63.  
  64.     if [ "$ri_version" = integrated ]; then
  65.       # integrated ri from Ruby 1.9
  66.       classes=( $( ri -c | ruby -ne 'if /^\s*$/..$stdin.eof then \
  67.                       if /, [A-Z]+/ then print; end; end' ) )
  68.     elif [ "$ri_version" = "ri 1.8a" ]; then
  69.       classes=( $( ruby -W0 $ri_path | \
  70.                ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \
  71.                  if /^ .*[A-Z]/ then print; end; end' ))
  72.     else
  73.       classes=( $( ruby -W0 $ri_path | \
  74.                ruby -ne 'if /^I have/..$stdin.eof then \
  75.                  if /^ .*[A-Z]/ then print; end; end' ))
  76.     fi
  77.  
  78.     COMPREPLY=( $( compgen -W '${classes[@]}' -- $cur ) )
  79.     if [[ "$cur" == [A-Z]* ]]; then
  80.       # we're completing on class or module alone
  81.       return 0
  82.     fi
  83.  
  84.     # we're completing on methods
  85.     method=$cur
  86.     ri_get_methods
  87. }
  88. complete -F _ri ri
  89.