3 # $Release Version: 0.7.1$
5 # $Date: 2002/02/01 06:36:22 $
6 # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 # From Original Idea of shugo@ruby-lang.org
15 @RCS_ID='-$Id: completion.rb,v 1.1.1.1 2002/02/01 06:36:22 tacyas Exp $-'
22 "def", "defined", "do",
23 "else", "elsif", "end", "ensure",
29 "redo", "rescue", "retry", "return",
32 "undef", "unless", "until",
37 CompletionProc = proc { |input|
38 bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
40 when /^(\/[^\/]*\/)\.([^.]*)$/
43 message = Regexp.quote($2)
45 candidates = Regexp.instance_methods(true)
46 select_message(receiver, message, candidates)
48 when /^([^\]]*\])\.([^.]*)$/
51 message = Regexp.quote($2)
53 candidates = Array.instance_methods(true)
54 select_message(receiver, message, candidates)
56 when /^([^\}]*\})\.([^.]*)$/
59 message = Regexp.quote($2)
61 candidates = Proc.instance_methods(true) | Hash.instance_methods(true)
62 select_message(receiver, message, candidates)
66 if Symbol.respond_to?(:all_symbols)
68 candidates = Symbol.all_symbols.collect{|s| s.id2name}
69 candidates.grep(/^#{sym}/)
74 when /^::([A-Z][^:\.\(]*)$/
75 # Absolute Constant or class methods
77 candidates = Object.constants
78 candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
80 when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
81 # Constant or class methods
83 message = Regexp.quote($4)
85 candidates = eval("#{receiver}.constants | #{receiver}.methods", bind)
89 candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
91 when /^(:[^.]+)\.([^.]*)$/
94 message = Regexp.quote($2)
96 candidates = Symbol.instance_methods(true)
97 select_message(receiver, message, candidates)
99 when /^([0-9_]+(\.[0-9_]+)?(e[0-9]+)?)\.([^.]*)$/
102 message = Regexp.quote($4)
105 candidates = eval(receiver, bind).methods
109 select_message(receiver, message, candidates)
111 # when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
112 when /^((\.?[^.]+)+)\.([^.]*)$/
115 message = Regexp.quote($3)
117 gv = eval "global_variables", bind
118 lv = eval "local_variables", bind
119 cv = eval "type.constants", bind
121 if (gv | lv | cv).include?(receiver)
122 # foo.func and foo is local var.
123 candidates = eval("#{receiver}.methods", bind)
124 elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
127 candidates = eval("#{receiver}.methods", bind)
134 ObjectSpace.each_object(Module){|m|
135 next if /^(IRB|SLex|RubyLex|RubyToken)/ =~ m.name
136 candidates.concat m.instance_methods
141 select_message(receiver, message, candidates)
144 # unknown(maybe String)
147 message = Regexp.quote($1)
149 candidates = String.instance_methods(true)
150 select_message(receiver, message, candidates)
153 candidates = eval("methods | private_methods | local_variables | type.constants", bind)
155 (candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
159 Operators = ["%", "&", "*", "**", "+", "-", "/",
160 "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
163 def self.select_message(receiver, message, candidates)
164 candidates.grep(/^#{message}/).collect do |e|
177 Readline.completion_proc = IRB::InputCompletor::CompletionProc