# ri completion for Ruby documentation -*- shell-script -*- # by Ian Macdonald proc _ri_get_methods { var regex = '' if [[ $ri_version == integrated ]] { if [[ -z $separator ]] { regex := '"(Instance|Class)'" } elif [[ $separator == "#" ]] { regex := 'Instance' } else { regex := 'Class' } global COMPREPLY := '( \ "'$( ri ${classes[@]} 2>/dev/null | ruby -ane \ 'if /^'"$regex"' methods:/.../^------------------|^$/ and \ /^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \ end' 2>/dev/null | sort -u )" ) } else { # older versions of ri didn't distinguish between class/module and # instance methods global COMPREPLY := '( \ "'$( ruby -W0 $ri_path ${classes[@]} | ruby -ane \ 'if /^-/.../^-/ and ! /^-/ and ! /^ +(class|module): / then \ print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \ end' | sort -u )" ) } global COMPREPLY := '( '$( compgen $prefix -W '${COMPREPLY[@]}' -- $method ) ) } # needs at least Ruby 1.8.0 in order to use -W0 proc _ri { var cur = '', prev = '', words = '', cword = '' _init_completion -n : || return var class = '', method = '', prefix = '', ri_path = '', ri_version = '', ri_major = '', separator = '', IFS = '' var -a classes = '' ri_path := $[type -p ri] # which version of ri are we using? # -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' [[ $ri_version != ${ri_version%200*} ]] && ri_version := 'integrated' [[ $ri_version =~ ri[[:space:]]v?([0-9]+) ]] && ri_major := $(BASH_REMATCH[1]) # need to also split on commas IFS := '$', \n\t'' if [[ "$cur" == [A-Z]*[#.]* ]] { [[ "$cur" == *#* ]] && separator := '#' || separator := '.' # we're completing on class and method class := $(cur%$separator*) method := $(cur#*$separator) classes := '( '$class ) prefix := ""-P $class$separator"" _ri_get_methods return 0 } if [[ $ri_version == integrated ]] { # integrated ri from Ruby 1.9 classes := '( '$( ri -c 2>/dev/null | ruby -ne 'if /^\s*$/..$stdin.eof then \ if /, [A-Z]+/ then print; end; end' 2>/dev/null ) ) } elif [[ $ri_major && $ri_major -ge 3 ]] { classes := '( '$( ri -l 2>/dev/null ) ) } elif [[ $ri_version == "ri 1.8a" ]] { classes := '( '$( ruby -W0 $ri_path | \ ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \ if /^ .*[A-Z]/ then print; end; end' )) } else { classes := '( '$( ruby -W0 $ri_path | \ ruby -ne 'if /^I have/..$stdin.eof then \ if /^ .*[A-Z]/ then print; end; end' )) } global COMPREPLY := '( '$( compgen -W '${classes[@]}' -- "$cur" ) ) __ltrim_colon_completions $cur if [[ "$cur" == [A-Z]* ]] { # we're completing on class or module alone return 0 } # we're completing on methods method := $cur _ri_get_methods } && complete -F _ri ri # ex: ts=4 sw=4 et filetype=sh (CommandList children: [ (FuncDef name: _ri_get_methods body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:regex) op:Equal spids:[16])] spids: [14] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$ri_version")} right: {(integrated)} ) ) terminator: ) ] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id: BoolUnary_z child: {($ VSub_Name "$separator")} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:regex) op: Equal rhs: {(DQ ("(Instance|Class)"))} spids: [50] ) ] spids: [50] ) ] spids: [-1 47] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$separator")} right: {(DQ ("#"))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:regex) op: Equal rhs: {(Instance)} spids: [74] ) ] spids: [74] ) ] spids: [56 71] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:regex) op: Equal rhs: {(Class)} spids: [81] ) ] spids: [81] ) ] spids: [78 85] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COMPREPLY) op: PlusEqual rhs: { (ArrayLiteralPart words: [ { (DQ (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (SimpleCommand words: [ {(ri)} { (BracedVarSub token: bracket_op: (WholeArray op_id:Lit_At) spids: [99 104] ) } ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [106] ) ] ) (SimpleCommand words: [ {(ruby)} {(-ane)} {(SQ <"if /^">) (DQ ($ VSub_Name "$regex")) (SQ < " methods:/.../^------------------|^$/ and \\\n" > <" /^ / then print $_.split(/, |,$/).grep(/^[^\\[]*$/).join(\"\\n\"); \\\n"> <" end"> ) } ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [129] ) ] ) (C {(sort)} {(-u)}) ] negated: False ) ] ) left_token: spids: [95 138] ) ) } ] ) } spids: [89] ) ] spids: [89] ) ] spids: [-1 33] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COMPREPLY) op: PlusEqual rhs: { (ArrayLiteralPart words: [ { (DQ (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(ruby)} {(-W0)} {($ VSub_Name "$ri_path")} { (BracedVarSub token: bracket_op: (WholeArray op_id:Lit_At) spids: [169 174] ) } ) (C {(ruby)} {(-ane)} { (SQ < "if /^-/.../^-/ and ! /^-/ and ! /^ +(class|module): / then \\\n" > <" print $_.split(/, |,$| +/).grep(/^[^\\[]*$/).join(\"\\n\"); \\\n"> <" end"> ) } ) (C {(sort)} {(-u)}) ] negated: False ) ] ) left_token: spids: [161 196] ) ) } ] ) } spids: [155] ) ] spids: [155] ) ] spids: [144 202] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COMPREPLY) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (C {(compgen)} {($ VSub_Name "$prefix")} {(-W)} {(SQ <"${COMPREPLY[@]}">)} {(--)} {($ VSub_Name "$method")} ) ] ) left_token: spids: [208 224] ) } ] ) } spids: [205] ) ] spids: [205] ) ] spids: [11] ) spids: [7 10] ) (AndOr children: [ (FuncDef name: _ri body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs:(LhsName name:cur) op:Equal spids:[243]) (assign_pair lhs:(LhsName name:prev) op:Equal spids:[245]) (assign_pair lhs:(LhsName name:words) op:Equal spids:[247]) (assign_pair lhs:(LhsName name:cword) op:Equal spids:[249]) ] spids: [241] ) (AndOr children: [ (C {(_init_completion)} {(-n)} {(Lit_Other ":")}) (ControlFlow token:) ] op_id: Op_DPipe ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs:(LhsName name:class) op:Equal spids:[266]) (assign_pair lhs:(LhsName name:method) op:Equal spids:[268]) (assign_pair lhs:(LhsName name:prefix) op:Equal spids:[270]) (assign_pair lhs:(LhsName name:ri_path) op:Equal spids:[272]) (assign_pair lhs: (LhsName name:ri_version) op: Equal spids: [274] ) (assign_pair lhs: (LhsName name:ri_major) op: Equal spids: [276] ) (assign_pair lhs: (LhsName name:separator) op: Equal spids: [278] ) (assign_pair lhs:(LhsName name:IFS) op:Equal spids:[280]) ] spids: [264] ) (Assignment keyword: Assign_Local flags: ["'-a'"] pairs: [(assign_pair lhs:(LhsName name:classes) op:Equal spids:[287])] spids: [283] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ri_path) op: Equal rhs: { (CommandSubPart command_list: (CommandList children:[(C {(type)} {(-p)} {(ri)})]) left_token: spids: [292 298] ) } spids: [291] ) ] spids: [291] ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ri_version) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children: [ (SimpleCommand words: [{(ruby)} {(-W0)} {($ VSub_Name "$ri_path")} {(-v)}] redirects: [ (Redir op_id: Redir_GreatAnd fd: 2 arg_word: {(1)} spids: [324] ) ] ) ] ) left_token: spids: [315 326] ) ) } spids: [313] ) ] spids: [313] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ri_version) op: Equal rhs: {(integrated)} spids: [331] ) ] spids: [331] ) ] op_id: Op_DPipe ) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {($ VSub_Name "$ri_version")} right: { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{("200*")}) spids: [341 345] ) } ) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ri_version) op: Equal rhs: {(integrated)} spids: [351] ) ] spids: [351] ) ] op_id: Op_DAmp ) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_EqualTilde left: {($ VSub_Name "$ri_version")} right: {(ri) (Lit_Other "[") (Lit_Other "[") (Lit_Other ":") (space) (Lit_Other ":") (Lit_Other "]") (Lit_Other "]") (v) (Lit_Other "?") ("(") (Lit_Other "[") (0-9) (Lit_Other "]") (Lit_Other "+") (")") } ) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ri_major) op: Equal rhs: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 1)})) spids: [383 388] ) } spids: [382] ) ] spids: [382] ) ] op_id: Op_DAmp ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:IFS) op: Equal rhs: {(SQ <", "> )} spids: [396] ) ] spids: [396] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$cur"))} right: {(Lit_Other "[") (A-Z) (Lit_Other "]") (Lit_Other "*") (Lit_Other "[") (Lit_Pound "#") (.) (Lit_Other "]") (Lit_Other "*") } ) ) terminator: ) ] action: [ (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$cur"))} right: {(Lit_Other "*") (Lit_Pound "#") (Lit_Other "*")} ) ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:separator) op: Equal rhs: {(Lit_Pound "#")} spids: [446] ) ] spids: [446] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:separator) op: Equal rhs: {(.)} spids: [451] ) ] spids: [451] ) ] op_id: Op_DPipe ) ] op_id: Op_DAmp ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:class) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id: VOp1_Percent arg_word: {($ VSub_Name "$separator") ("*")} ) spids: [460 465] ) } spids: [459] ) ] spids: [459] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:method) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id: VOp1_Pound arg_word: {("*") ($ VSub_Name "$separator")} ) spids: [469 474] ) } spids: [468] ) ] spids: [468] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:classes) op: Equal rhs: {(ArrayLiteralPart words:[{($ VSub_Name "$class")}])} spids: [477] ) ] spids: [477] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:prefix) op: Equal rhs: {(DQ ("-P ") ($ VSub_Name "$class") ($ VSub_Name "$separator"))} spids: [485] ) ] spids: [485] ) (C {(_ri_get_methods)}) (ControlFlow token: arg_word: {(0)} ) ] spids: [-1 427] ) ] spids: [-1 501] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$ri_version")} right: {(integrated)} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:classes) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (SimpleCommand words: [{(ri)} {(-c)}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [534] ) ] ) (SimpleCommand words: [ {(ruby)} {(-ne)} { (SQ <"if /^\\s*$/..$stdin.eof then \\\n"> < " if /, [A-Z]+/ then print; end; end" > ) } ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [548] ) ] ) ] negated: False ) ] ) left_token: spids: [528 551] ) } ] ) } spids: [525] ) ] spids: [525] ) ] spids: [-1 518] ) (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalAnd left: (WordTest w:{($ VSub_Name "$ri_major")}) right: (BoolBinary op_id: BoolBinary_ge left: {($ VSub_Name "$ri_major")} right: {(3)} ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:classes) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (SimpleCommand words: [{(ri)} {(-l)}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [585] ) ] ) ] ) left_token: spids: [579 588] ) } ] ) } spids: [576] ) ] spids: [576] ) ] spids: [556 573] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$ri_version")} right: {(DQ ("ri 1.8a"))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:classes) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(ruby)} {(-W0)} {($ VSub_Name "$ri_path")}) (C {(ruby)} {(-ne)} {(SQ <"if /^">) (DQ ("'")) (SQ ) (DQ ("'")) (SQ <" has/..$stdin.eof then \\\n"> < " if /^ .*[A-Z]/ then print; end; end" > ) } ) ] negated: False ) ] ) left_token: spids: [614 647] ) } ] ) } spids: [611] ) ] spids: [611] ) ] spids: [593 608] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:classes) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(ruby)} {(-W0)} {($ VSub_Name "$ri_path")}) (C {(ruby)} {(-ne)} { (SQ <"if /^I have/..$stdin.eof then \\\n"> < " if /^ .*[A-Z]/ then print; end; end" > ) } ) ] negated: False ) ] ) left_token: spids: [657 678] ) } ] ) } spids: [654] ) ] spids: [654] ) ] spids: [651 682] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COMPREPLY) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (C {(compgen)} {(-W)} {(SQ <"${classes[@]}">)} {(--)} {(DQ ($ VSub_Name "$cur"))} ) ] ) left_token: spids: [689 705] ) } ] ) } spids: [686] ) ] spids: [686] ) (C {(__ltrim_colon_completions)} {(DQ ($ VSub_Name "$cur"))}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$cur"))} right: {(Lit_Other "[") (A-Z) (Lit_Other "]") (Lit_Other "*")} ) ) terminator: ) ] action: [(ControlFlow token: arg_word:{(0)})] spids: [-1 736] ) ] spids: [-1 748] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:method) op: Equal rhs: {($ VSub_Name "$cur")} spids: [756] ) ] spids: [756] ) (C {(_ri_get_methods)}) ] spids: [238] ) spids: [234 237] ) (C {(complete)} {(-F)} {(_ri)} {(ri)}) ] op_id: Op_DAmp ) ] )