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$
|
|
|
|
# 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)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Regexp.instance_methods.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
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)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Array.instance_methods.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^([^\}]*\})\.([^.]*)$/
|
|
|
|
# Proc or Hash
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Proc.instance_methods.collect{|m| m.to_s}
|
|
|
|
candidates |= Hash.instance_methods.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
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
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Object.constants.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
|
|
|
|
|
|
|
|
when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
|
|
|
|
# Constant or class methods
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($4)
|
|
|
|
begin
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
|
|
|
|
candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
|
2001-04-30 14:25:04 -04:00
|
|
|
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)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Symbol.instance_methods.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2006-07-19 10:18:20 -04:00
|
|
|
when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Numeric
|
|
|
|
receiver = $1
|
2006-07-19 10:18:20 -04:00
|
|
|
message = Regexp.quote($5)
|
|
|
|
|
|
|
|
begin
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
2006-07-19 10:18:20 -04:00
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
|
|
|
when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
|
|
|
|
# Numeric(0xFFFF)
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
begin
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
rescue Exception
|
2006-07-19 10:18:20 -04:00
|
|
|
candidates = []
|
2001-04-30 14:25:04 -04:00
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(\$[^.]*)$/
|
2007-07-06 07:09:38 -04:00
|
|
|
regmessage = Regexp.new(Regexp.quote($1))
|
|
|
|
candidates = global_variables.collect{|m| m.to_s}.grep(regmessage)
|
2002-07-09 07:17:17 -04:00
|
|
|
|
2001-04-30 14:25:04 -04:00
|
|
|
# when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
|
|
|
|
when /^((\.?[^.]+)+)\.([^.]*)$/
|
|
|
|
# variable
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($3)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
gv = eval("global_variables", bind).collect{|m| m.to_s}
|
|
|
|
lv = eval("local_variables", bind).collect{|m| m.to_s}
|
|
|
|
cv = eval("self.class.constants", bind).collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
if (gv | lv | cv).include?(receiver)
|
|
|
|
# foo.func and foo is local var.
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval("#{receiver}.methods", bind).collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
|
|
|
|
# Foo::Bar.func
|
|
|
|
begin
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval("#{receiver}.methods", bind).collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# func1.func2
|
|
|
|
candidates = []
|
|
|
|
ObjectSpace.each_object(Module){|m|
|
2006-07-19 10:18:20 -04:00
|
|
|
begin
|
|
|
|
name = m.name
|
|
|
|
rescue Exception
|
|
|
|
name = ""
|
|
|
|
end
|
2006-07-08 19:17:53 -04:00
|
|
|
next if name != "IRB::Context" and
|
|
|
|
/^(IRB|SLex|RubyLex|RubyToken)/ =~ name
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates.concat m.instance_methods(false).collect{|m| m.to_s}
|
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)
|
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = String.instance_methods(true).collect{|m| m.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = eval("methods | private_methods | local_variables | self.class.constants", bind).collect{|m| m.to_s}
|
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=")
|
2004-03-10 02:05:19 -05:00
|
|
|
Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
Readline.completion_append_character = nil
|
2001-04-30 14:25:04 -04:00
|
|
|
Readline.completion_proc = IRB::InputCompletor::CompletionProc
|