2001-04-30 14:25:04 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05: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
|
2012-12-21 00:45:50 -05:00
|
|
|
module InputCompletor # :nodoc:
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Set of reserved words used by Ruby, you should not use these for
|
|
|
|
# constants or variables
|
2000-05-12 05:07:57 -04:00
|
|
|
ReservedWords = [
|
|
|
|
"BEGIN", "END",
|
2009-03-05 22:56:38 -05:00
|
|
|
"alias", "and",
|
|
|
|
"begin", "break",
|
2000-05-12 05:07:57 -04:00
|
|
|
"case", "class",
|
|
|
|
"def", "defined", "do",
|
|
|
|
"else", "elsif", "end", "ensure",
|
2009-03-05 22:56:38 -05:00
|
|
|
"false", "for",
|
|
|
|
"if", "in",
|
|
|
|
"module",
|
2000-05-12 05:07:57 -04:00
|
|
|
"next", "nil", "not",
|
2009-03-05 22:56:38 -05:00
|
|
|
"or",
|
2000-05-12 05:07:57 -04:00
|
|
|
"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
|
|
|
]
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
# puts "input: #{input}"
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
case input
|
2011-06-29 11:08:41 -04:00
|
|
|
when /^((["'`]).*\2)\.([^.]*)$/
|
|
|
|
# String
|
|
|
|
receiver = $1
|
2012-12-29 07:59:06 -05:00
|
|
|
message = Regexp.quote($3)
|
2011-06-29 11:08:41 -04:00
|
|
|
|
|
|
|
candidates = String.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
|
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)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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}
|
2012-12-29 07:59:06 -05:00
|
|
|
candidates.grep(/^#{Regexp.quote(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}
|
|
|
|
|
2010-04-09 08:28:00 -04:00
|
|
|
# when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
|
2010-04-13 01:01:10 -04:00
|
|
|
when /^([A-Z].*)::([^:.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Constant or class methods
|
|
|
|
receiver = $1
|
2010-04-13 01:01:10 -04:00
|
|
|
message = Regexp.quote($2)
|
2001-04-30 14:25:04 -04:00
|
|
|
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
|
2011-01-18 04:34:10 -05:00
|
|
|
select_message(receiver, message, candidates, "::")
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
when /^(:[^:.]+)(\.|::)([^.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Symbol
|
|
|
|
receiver = $1
|
2011-01-18 04:34:10 -05:00
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2007-07-06 07:09:38 -04:00
|
|
|
candidates = Symbol.instance_methods.collect{|m| m.to_s}
|
2011-01-18 04:34:10 -05:00
|
|
|
select_message(receiver, message, candidates, sep)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)(\.|::)([^.]*)$/
|
2001-04-30 14:25:04 -04:00
|
|
|
# Numeric
|
|
|
|
receiver = $1
|
2011-01-18 04:34:10 -05:00
|
|
|
sep = $5
|
|
|
|
message = Regexp.quote($6)
|
2006-07-19 10:18:20 -04:00
|
|
|
|
|
|
|
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
|
2011-01-18 04:34:10 -05:00
|
|
|
select_message(receiver, message, candidates, sep)
|
2006-07-19 10:18:20 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
when /^(-?0x[0-9a-fA-F_]+)(\.|::)([^.]*)$/
|
2006-07-19 10:18:20 -04:00
|
|
|
# Numeric(0xFFFF)
|
|
|
|
receiver = $1
|
2011-01-18 04:34:10 -05:00
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
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
|
2011-01-18 04:34:10 -05:00
|
|
|
select_message(receiver, message, candidates, sep)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(\$[^.]*)$/
|
2011-01-18 04:34:10 -05:00
|
|
|
# global var
|
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 /^(\$?(\.?[^.]+)+)\.([^.]*)$/
|
2010-04-09 08:28:00 -04:00
|
|
|
# when /^((\.?[^.]+)+)\.([^.]*)$/
|
2011-01-18 04:34:10 -05:00
|
|
|
# when /^([^."].*)\.([^.]*)$/
|
|
|
|
when /^([^."].*)(\.|::)([^.]*)$/
|
|
|
|
# variable.func or func.func
|
2001-04-30 14:25:04 -04:00
|
|
|
receiver = $1
|
2011-01-18 04:34:10 -05:00
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
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}
|
2012-12-20 11:02:43 -05:00
|
|
|
iv = eval("instance_variables", bind).collect{|m| m.to_s}
|
2007-07-06 07:09:38 -04:00
|
|
|
cv = eval("self.class.constants", bind).collect{|m| m.to_s}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-20 11:02:43 -05:00
|
|
|
if (gv | lv | iv | cv).include?(receiver) or /^[A-Z]/ =~ receiver && /\./ !~ receiver
|
2011-01-18 04:34:10 -05:00
|
|
|
# foo.func and foo is var. OR
|
|
|
|
# foo::func and foo is var. OR
|
|
|
|
# foo::Const and foo is var. OR
|
2001-04-30 14:25:04 -04:00
|
|
|
# Foo::Bar.func
|
|
|
|
begin
|
2011-01-18 04:34:10 -05:00
|
|
|
candidates = []
|
|
|
|
rec = eval(receiver, bind)
|
2011-05-15 07:55:52 -04:00
|
|
|
if sep == "::" and rec.kind_of?(Module)
|
2011-01-18 04:34:10 -05:00
|
|
|
candidates = rec.constants.collect{|m| m.to_s}
|
|
|
|
end
|
|
|
|
candidates |= rec.methods.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
|
2012-01-27 15:53:37 -05:00
|
|
|
begin
|
|
|
|
next if name != "IRB::Context" and
|
|
|
|
/^(IRB|SLex|RubyLex|RubyToken)/ =~ name
|
|
|
|
rescue Exception
|
|
|
|
next
|
|
|
|
end
|
2008-12-18 02:54:50 -05:00
|
|
|
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
|
2001-04-30 14:25:04 -04:00
|
|
|
}
|
|
|
|
candidates.sort!
|
|
|
|
candidates.uniq!
|
|
|
|
end
|
2011-01-18 04:34:10 -05:00
|
|
|
select_message(receiver, message, candidates, sep)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
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
|
2012-12-20 11:02:43 -05:00
|
|
|
candidates = eval("methods | private_methods | local_variables | instance_variables | self.class.constants", bind).collect{|m| m.to_s}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
(candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
|
|
|
|
end
|
|
|
|
}
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Set of available operators in Ruby
|
2001-04-30 14:25:04 -04:00
|
|
|
Operators = ["%", "&", "*", "**", "+", "-", "/",
|
|
|
|
"<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
|
2009-04-03 01:04:02 -04:00
|
|
|
"[]", "[]=", "^", "!", "!=", "!~"]
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
def self.select_message(receiver, message, candidates, sep = ".")
|
2001-04-30 14:25:04 -04:00
|
|
|
candidates.grep(/^#{message}/).collect do |e|
|
|
|
|
case e
|
|
|
|
when /^[a-zA-Z_]/
|
2011-01-18 04:34:10 -05:00
|
|
|
receiver + sep + e
|
2001-04-30 14:25:04 -04:00
|
|
|
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=")
|
2011-06-29 11:08:41 -04:00
|
|
|
# Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
|
|
|
|
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
|