#compdef ri

local curcontext="$curcontext" state line ret=1
typeset -A opt_args

_arguments -C \
  '(- *)'{-h,--help}'[print help information and exit]' \
  '(- *)'{-v,--version}'[display the version of ri]' \
  '*'{-d+,--doc-dir=}'[directory to search for documentation]:ri doc directory:_directories' \
  '(-f --format)'{-f+,--format=}'[format to use when displaying output]:output format [bs]:(ansi bs markdown rdoc)' \
  '(-T --no-pager)'{-T,--no-pager}'[send output directly to stdout]' \
  '(-i --interactive)'{-i,--interactive}'[interactive mode]' \
  '(-a --all)'{-a,--all}'[show all documentation for a class or module]' \
  '(-l --list)'{-l,--list}'[list classes ri knows about]' \
  '--list-doc-dirs[list the directories from which ri will source documentation]' \
  '(-w --width)'{-w+,--width=}'[set the width of the output]:output width [72]' \
  '(-l --list)--server=-[run RDoc server on the given port]::port [8214]:_ports' \
  "--no-standard-docs[don't include documentation from the Ruby standard library, site_lib, installed gems, or ~/.rdoc]" \
  '(--no-use-cache --use-cache)--'{no-,}"use-cache[whether or not to use ri's cache]" \
  '(--no-system --system)--'{no-,}"system[include documentation from Ruby's standard library]" \
  '(--no-site --site)--'{no-,}'site[include documentation from libraries installed in site_lib]' \
  '(--no-gems --gems)--'{no-,}'gems[include documentation from RubyGems]' \
  '(--no-home --home)--'{no-,}'home[include documentation stored in ~/.rdoc]' \
  '--profile[run with the Ruby profiler]' \
  '--dump=[dump data from an ri cache or data file]:cache:_files' \
  '*:ri name:->ri-name' && return

if [[ "$state" = ri-name ]]; then
  local -a ri_dirs ri_names ri_wants ri_names
  local class_dir esc_name dir curtag tag descr expl

  ri_dirs=( ${(f)"$(_call_program ri-names "$words[1]" ${(kv)opt_args[(I)-d|--doc-dir|--((no-|)(system|site|gems|home)|standard-docs)]} --list-doc-dirs -f bs -T)"} )

  if compset -P '?*(::|:|\#|.)'; then
    class_dir=${IPREFIX//(::|\#|.)/\/}
  fi
  esc_name=${${(Q)PREFIX}//(#b)([^A-Za-z0-9_])/$(printf %%%x ${(qq)match[1]})}

  case "$IPREFIX" in
    (*::) ri_wants=( 'classes:class name' 'class-methods:class method' );;
    (*:) ri_wants=( 'docs:documentation file' );;
    (*\#) ri_wants=( 'instance-methods:instance method' );;
    (*.) ri_wants=( 'class-methods:class method' 'instance-methods:instance method' );;
    (*) ri_wants=( 'classes:class name' 'gems:gem' )
  esac

  for curtag in $ri_wants; do
    tag=${curtag%%:*}
    descr=${curtag#*:}

    _tags "$tag"
    while _tags; do
      while _next_label "$tag" expl "$descr"; do
        ri_wants=()
        suf=()
        case "$tag" in
          (gems)
            ri_wants=( ruby ${${(f)"$(_call_program gems gem${words[1]#ri} list -q --no-versions)"}%% *} )
            suf=( -S : )
          ;;
          (classes)
          for dir in $ri_dirs[@]; do
            ri_wants+=( $dir/$class_dir*(-/:t) )
          done
          suf=( -S '::' )
          ;;
          (class-methods)
          for dir in $ri_dirs[@]; do
            fnames=( $dir/$class_dir*-c.ri(-.:t) )
            ri_wants+=( ${${fnames%-c.ri}//(#b)%(??)/$(print "\\x$match[1]")} )
          done
          ;;
          (instance-methods)
          for dir in $ri_dirs[@]; do
            fnames=( $dir/$class_dir*-i.ri(-.:t) )
            ri_wants+=( ${${fnames%-i.ri}//(#b)%(??)/$(print "\\x$match[1]")} )
          done
          ;;
          (docs)
            ri_wants=( ${${(f)"$(_call_program docs $words[1] ${(kv)opt_args[(I)-d|--doc-dir|--((no-|)(system|site|gems|home)|standard-docs)]} $IPREFIX)"}:#=*} )
          ;;
        esac
        ri_names=( ${(Q)ri_wants} )
        compadd $suf -d ri_names -a "$expl[@]" ri_wants && ret=0
      done
      (( ret )) || break
    done
  done
fi

return ret
