3 # $Release Version: 0.9$
5 # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 # From Original Idea of shugo@ruby-lang.org
15 @RCS_ID='-$Id: completion.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
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 # puts "input: #{input}"
43 when /^(\/[^\/]*\/)\.([^.]*)$/
46 message = Regexp.quote($2)
48 candidates = Regexp.instance_methods(true)
49 select_message(receiver, message, candidates)
51 when /^([^\]]*\])\.([^.]*)$/
54 message = Regexp.quote($2)
56 candidates = Array.instance_methods(true)
57 select_message(receiver, message, candidates)
59 when /^([^\}]*\})\.([^.]*)$/
62 message = Regexp.quote($2)
64 candidates = Proc.instance_methods(true) | Hash.instance_methods(true)
65 select_message(receiver, message, candidates)
69 if Symbol.respond_to?(:all_symbols)
71 candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
72 candidates.grep(/^#{sym}/)
77 when /^::([A-Z][^:\.\(]*)$/
78 # Absolute Constant or class methods
80 candidates = Object.constants
81 candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
83 when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
84 # Constant or class methods
86 message = Regexp.quote($4)
88 candidates = eval("#{receiver}.constants | #{receiver}.methods", bind)
92 candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
94 when /^(:[^:.]+)\.([^.]*)$/
97 message = Regexp.quote($2)
99 candidates = Symbol.instance_methods(true)
100 select_message(receiver, message, candidates)
102 when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
105 message = Regexp.quote($5)
108 candidates = eval(receiver, bind).methods
112 select_message(receiver, message, candidates)
114 when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
117 message = Regexp.quote($2)
120 candidates = eval(receiver, bind).methods
124 select_message(receiver, message, candidates)
127 candidates = global_variables.grep(Regexp.new(Regexp.quote($1)))
129 # when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
130 when /^((\.?[^.]+)+)\.([^.]*)$/
133 message = Regexp.quote($3)
135 gv = eval("global_variables", bind)
136 lv = eval("local_variables", bind)
137 cv = eval("self.class.constants", bind)
139 if (gv | lv | cv).include?(receiver)
140 # foo.func and foo is local var.
141 candidates = eval("#{receiver}.methods", bind)
142 elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
145 candidates = eval("#{receiver}.methods", bind)
152 ObjectSpace.each_object(Module){|m|
158 next if name != "IRB::Context" and
159 /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
160 candidates.concat m.instance_methods(false)
165 select_message(receiver, message, candidates)
168 # unknown(maybe String)
171 message = Regexp.quote($1)
173 candidates = String.instance_methods(true)
174 select_message(receiver, message, candidates)
177 candidates = eval("methods | private_methods | local_variables | self.class.constants", bind)
179 (candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
183 Operators = ["%", "&", "*", "**", "+", "-", "/",
184 "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
187 def self.select_message(receiver, message, candidates)
188 candidates.grep(/^#{message}/).collect do |e|
201 if Readline.respond_to?("basic_word_break_characters=")
202 Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
204 Readline.completion_append_character = nil
205 Readline.completion_proc = IRB::InputCompletor::CompletionProc