2001-04-30 14:25:04 -04:00
|
|
|
#
|
|
|
|
# irb/completor.rb -
|
2002-07-09 07:17:17 -04:00
|
|
|
# $Release Version: 0.9$
|
2001-04-30 14:25:04 -04:00
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
|
|
|
# From Original Idea of shugo@ruby-lang.org
|
|
|
|
#
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
require "readline"
|
|
|
|
|
|
|
|
module IRB
|
2001-04-30 14:25:04 -04:00
|
|
|
module InputCompletor
|
|
|
|
|
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
ReservedWords = [
|
|
|
|
"BEGIN", "END",
|
|
|
|
"alias", "and",
|
|
|
|
"begin", "break",
|
|
|
|
"case", "class",
|
|
|
|
"def", "defined", "do",
|
|
|
|
"else", "elsif", "end", "ensure",
|
|
|
|
"false", "for",
|
|
|
|
"if", "in",
|
|
|
|
"module",
|
|
|
|
"next", "nil", "not",
|
|
|
|
"or",
|
|
|
|
"redo", "rescue", "retry", "return",
|
|
|
|
"self", "super",
|
|
|
|
"then", "true",
|
|
|
|
"undef", "unless", "until",
|
|
|
|
"when", "while",
|
2001-04-30 14:25:04 -04:00
|
|
|
"yield",
|
2000-05-12 05:07:57 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
CompletionProc = proc { |input|
|
2001-04-30 14:25:04 -04:00
|
|
|
bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
|
2002-07-09 07:17:17 -04:00
|
|
|
|
|
|
|
# puts "input: #{input}"
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
case input
|
2001-04-30 14:25:04 -04:00
|
|
|
when /^(\/[^\/]*\/)\.([^.]*)$/
|
|
|
|
# Regexp
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
|
|
|
|
|
|
|
candidates = Regexp.instance_methods(true)
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^([^\]]*\])\.([^.]*)$/
|
|
|
|
# Array
|
2000-05-12 05:07:57 -04:00
|
|
|
receiver = $1
|
2001-04-30 14:25:04 -04:00
|
|
|
message = Regexp.quote($2)
|
|
|
|
|
|
|
|
candidates = Array.instance_methods(true)
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^([^\}]*\})\.([^.]*)$/
|
|
|
|
# Proc or Hash
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
|
|
|
|
|
|
|
candidates = Proc.instance_methods(true) | Hash.instance_methods(true)
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(:[^:.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Symbol
|
|
|
|
if Symbol.respond_to?(:all_symbols)
|
|
|
|
sym = $1
|
2002-07-09 07:17:17 -04:00
|
|
|
candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
|
2001-04-30 14:25:04 -04:00
|
|
|
candidates.grep(/^#{sym}/)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2001-04-30 14:25:04 -04:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
when /^::([A-Z][^:\.\(]*)$/
|
|
|
|
# Absolute Constant or class methods
|
|
|
|
receiver = $1
|
|
|
|
candidates = Object.constants
|
|
|
|
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
|
|
|
|
|
|
|
|
when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
|
|
|
|
# Constant or class methods
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($4)
|
|
|
|
begin
|
|
|
|
candidates = eval("#{receiver}.constants | #{receiver}.methods", bind)
|
|
|
|
rescue Exception
|
2000-05-12 05:07:57 -04:00
|
|
|
candidates = []
|
|
|
|
end
|
2001-04-30 14:25:04 -04:00
|
|
|
candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(:[^:.]+)\.([^.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Symbol
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
|
|
|
|
|
|
|
candidates = Symbol.instance_methods(true)
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^([0-9_]+(\.[0-9_]+)?(e[0-9]+)?)\.([^.]*)$/
|
|
|
|
# Numeric
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($4)
|
|
|
|
|
|
|
|
begin
|
|
|
|
candidates = eval(receiver, bind).methods
|
|
|
|
rescue Exception
|
|
|
|
candidates
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(\$[^.]*)$/
|
|
|
|
candidates = global_variables.grep Regexp.new(Regexp.quote($1))
|
|
|
|
|
2001-04-30 14:25:04 -04:00
|
|
|
# when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
|
|
|
|
when /^((\.?[^.]+)+)\.([^.]*)$/
|
|
|
|
# variable
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($3)
|
|
|
|
|
|
|
|
gv = eval "global_variables", bind
|
|
|
|
lv = eval "local_variables", bind
|
2002-12-24 09:50:10 -05:00
|
|
|
cv = eval "self.class.constants", bind
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
if (gv | lv | cv).include?(receiver)
|
|
|
|
# foo.func and foo is local var.
|
|
|
|
candidates = eval("#{receiver}.methods", bind)
|
|
|
|
elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
|
|
|
|
# Foo::Bar.func
|
|
|
|
begin
|
|
|
|
candidates = eval("#{receiver}.methods", bind)
|
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# func1.func2
|
|
|
|
candidates = []
|
|
|
|
ObjectSpace.each_object(Module){|m|
|
2002-07-09 07:17:17 -04:00
|
|
|
next if m.name != "IRB::Context" and
|
|
|
|
/^(IRB|SLex|RubyLex|RubyToken)/ =~ m.name
|
2003-05-22 12:37:22 -04:00
|
|
|
candidates.concat m.instance_methods(false)
|
2001-04-30 14:25:04 -04:00
|
|
|
}
|
|
|
|
candidates.sort!
|
|
|
|
candidates.uniq!
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^\.([^.]*)$/
|
|
|
|
# unknown(maybe String)
|
|
|
|
|
|
|
|
receiver = ""
|
|
|
|
message = Regexp.quote($1)
|
|
|
|
|
|
|
|
candidates = String.instance_methods(true)
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2002-10-02 12:45:35 -04:00
|
|
|
candidates = eval("methods | private_methods | local_variables | self.class.constants", bind)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
(candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
|
|
|
|
end
|
|
|
|
}
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
Operators = ["%", "&", "*", "**", "+", "-", "/",
|
|
|
|
"<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
|
|
|
|
"[]", "[]=", "^",]
|
|
|
|
|
|
|
|
def self.select_message(receiver, message, candidates)
|
|
|
|
candidates.grep(/^#{message}/).collect do |e|
|
|
|
|
case e
|
|
|
|
when /^[a-zA-Z_]/
|
|
|
|
receiver + "." + e
|
|
|
|
when /^[0-9]/
|
|
|
|
when *Operators
|
|
|
|
#receiver + " " + e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
if Readline.respond_to?("basic_word_break_characters=")
|
|
|
|
Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
|
|
|
|
end
|
|
|
|
Readline.completion_append_character = nil
|
2001-04-30 14:25:04 -04:00
|
|
|
Readline.completion_proc = IRB::InputCompletor::CompletionProc
|