2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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
|
|
|
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Set of reserved words used by Ruby, you should not use these for
|
|
|
|
# constants or variables
|
2013-06-04 10:17:17 -04:00
|
|
|
ReservedWords = %w[
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
case input
|
2011-06-29 11:08:41 -04:00
|
|
|
when /^((["'`]).*\2)\.([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# String
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($3)
|
2011-06-29 11:08:41 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = String.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
2011-06-29 11:08:41 -04:00
|
|
|
|
2001-04-30 14:25:04 -04:00
|
|
|
when /^(\/[^\/]*\/)\.([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Regexp
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = Regexp.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
when /^([^\]]*\])\.([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Array
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = Array.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
when /^([^\}]*\})\.([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Proc or Hash
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = Proc.instance_methods.collect{|m| m.to_s}
|
|
|
|
candidates |= Hash.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(:[^:.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Symbol
|
|
|
|
if Symbol.respond_to?(:all_symbols)
|
|
|
|
sym = $1
|
|
|
|
candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
|
|
|
|
candidates.grep(/^#{Regexp.quote(sym)}/)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
when /^::([A-Z][^:\.\(]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Absolute Constant or class methods
|
|
|
|
receiver = $1
|
|
|
|
candidates = Object.constants.collect{|m| m.to_s}
|
|
|
|
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2010-04-13 01:01:10 -04:00
|
|
|
when /^([A-Z].*)::([^:.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Constant or class methods
|
|
|
|
receiver = $1
|
|
|
|
message = Regexp.quote($2)
|
|
|
|
begin
|
|
|
|
candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
|
|
|
|
candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
|
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates, "::")
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
when /^(:[^:.]+)(\.|::)([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Symbol
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = Symbol.instance_methods.collect{|m| m.to_s}
|
|
|
|
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]+)?)(\.|::)([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Numeric
|
|
|
|
receiver = $1
|
|
|
|
sep = $5
|
|
|
|
message = Regexp.quote($6)
|
|
|
|
|
|
|
|
begin
|
|
|
|
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
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_]+)(\.|::)([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Numeric(0xFFFF)
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
|
|
|
|
|
|
|
begin
|
|
|
|
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates, sep)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^(\$[^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# global var
|
|
|
|
regmessage = Regexp.new(Regexp.quote($1))
|
|
|
|
candidates = global_variables.collect{|m| m.to_s}.grep(regmessage)
|
2002-07-09 07:17:17 -04:00
|
|
|
|
2011-01-18 04:34:10 -05:00
|
|
|
when /^([^."].*)(\.|::)([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# variable.func or func.func
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
|
|
|
message = Regexp.quote($3)
|
|
|
|
|
|
|
|
gv = eval("global_variables", bind).collect{|m| m.to_s}
|
|
|
|
lv = eval("local_variables", bind).collect{|m| m.to_s}
|
|
|
|
iv = eval("instance_variables", bind).collect{|m| m.to_s}
|
|
|
|
cv = eval("self.class.constants", bind).collect{|m| m.to_s}
|
|
|
|
|
|
|
|
if (gv | lv | iv | cv).include?(receiver) or /^[A-Z]/ =~ receiver && /\./ !~ receiver
|
|
|
|
# foo.func and foo is var. OR
|
|
|
|
# foo::func and foo is var. OR
|
|
|
|
# foo::Const and foo is var. OR
|
|
|
|
# Foo::Bar.func
|
|
|
|
begin
|
|
|
|
candidates = []
|
|
|
|
rec = eval(receiver, bind)
|
|
|
|
if sep == "::" and rec.kind_of?(Module)
|
|
|
|
candidates = rec.constants.collect{|m| m.to_s}
|
|
|
|
end
|
|
|
|
candidates |= rec.methods.collect{|m| m.to_s}
|
|
|
|
rescue Exception
|
|
|
|
candidates = []
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# func1.func2
|
|
|
|
candidates = []
|
|
|
|
ObjectSpace.each_object(Module){|m|
|
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
|
|
|
|
}
|
|
|
|
candidates.sort!
|
|
|
|
candidates.uniq!
|
|
|
|
end
|
|
|
|
select_message(receiver, message, candidates, sep)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
|
|
|
when /^\.([^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# unknown(maybe String)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
receiver = ""
|
|
|
|
message = Regexp.quote($1)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates = String.instance_methods(true).collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates)
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04: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
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
(candidates|ReservedWords).grep(/^#{Regexp.quote(input)}/)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
}
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Set of available operators in Ruby
|
2013-06-04 10:17:17 -04:00
|
|
|
Operators = %w[% & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ ! != !~]
|
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|
|
2014-08-08 21:36:49 -04:00
|
|
|
case e
|
|
|
|
when /^[a-zA-Z_]/
|
|
|
|
receiver + sep + e
|
|
|
|
when /^[0-9]/
|
|
|
|
when *Operators
|
|
|
|
#receiver + " " + e
|
|
|
|
end
|
2001-04-30 14:25:04 -04:00
|
|
|
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=")
|
2014-08-12 23:02:26 -04: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
|