2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2001-04-30 14:25:04 -04:00
|
|
|
#
|
2018-07-28 06:00:29 -04:00
|
|
|
# irb/completion.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"
|
2019-06-10 08:07:24 -04:00
|
|
|
autoload :RDoc, "rdoc"
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
module IRB
|
2012-12-21 00:45:50 -05:00
|
|
|
module InputCompletor # :nodoc:
|
2019-04-23 08:55:29 -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
|
|
|
|
2019-04-27 17:37:39 -04:00
|
|
|
BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
CompletionProc = proc { |input|
|
2019-05-28 17:54:49 -04:00
|
|
|
retrieve_completion_data(input).compact.map{ |i| i.encode(Encoding.default_external) }
|
2019-05-26 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def self.retrieve_completion_data(input, doc_namespace = false)
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"String.#{message}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"Regexp.#{message}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"Array.#{message}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
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
|
|
|
|
2019-05-26 12:24:03 -04:00
|
|
|
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
|
|
|
|
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
|
|
|
|
if doc_namespace
|
|
|
|
["Proc.#{message}", "Hash.#{message}"]
|
|
|
|
else
|
|
|
|
select_message(receiver, message, proc_candidates | hash_candidates)
|
|
|
|
end
|
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
|
2019-05-26 12:24:03 -04:00
|
|
|
return nil if doc_namespace
|
2014-08-08 21:36:49 -04:00
|
|
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
candidates.find { |i| i == receiver }
|
|
|
|
else
|
|
|
|
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
|
|
|
|
end
|
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
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"#{receiver}::#{message}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates, "::")
|
|
|
|
end
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"Symbol.#{message}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2019-05-26 12:59:17 -04:00
|
|
|
when /^(?<num>-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE][+-]?[0-9]+i?|r)?)(?<sep>\.|::)(?<mes>[^.]*)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
# Numeric
|
2019-05-26 12:24:03 -04:00
|
|
|
receiver = $~[:num]
|
|
|
|
sep = $~[:sep]
|
|
|
|
message = Regexp.quote($~[:mes])
|
2014-08-08 21:36:49 -04:00
|
|
|
|
|
|
|
begin
|
2019-05-26 12:24:03 -04:00
|
|
|
instance = eval(receiver, bind)
|
|
|
|
if doc_namespace
|
|
|
|
"#{instance.class.name}.#{message}"
|
|
|
|
else
|
|
|
|
candidates = instance.methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
rescue Exception
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
candidates = []
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
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
|
2019-05-26 12:24:03 -04:00
|
|
|
instance = eval(receiver, bind)
|
|
|
|
if doc_namespace
|
|
|
|
"#{instance.class.name}.#{message}"
|
|
|
|
else
|
|
|
|
candidates = instance.methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
rescue Exception
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
candidates = []
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
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
|
2019-05-26 12:24:03 -04:00
|
|
|
gvar = $1
|
|
|
|
all_gvars = global_variables.collect{|m| m.to_s}
|
|
|
|
if doc_namespace
|
|
|
|
all_gvars.find{ |i| i == gvar }
|
|
|
|
else
|
|
|
|
all_gvars.grep(Regexp.new(Regexp.quote(gvar)))
|
|
|
|
end
|
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)
|
|
|
|
|
2019-07-19 13:53:40 -04:00
|
|
|
gv = eval("global_variables", bind).collect{|m| m.to_s}.append("true", "false", "nil")
|
2014-08-08 21:36:49 -04:00
|
|
|
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 = []
|
2018-03-12 21:28:27 -04:00
|
|
|
to_ignore = ignored_modules
|
2014-08-08 21:36:49 -04:00
|
|
|
ObjectSpace.each_object(Module){|m|
|
2018-03-12 21:28:27 -04:00
|
|
|
next if (to_ignore.include?(m) rescue true)
|
2014-08-08 21:36:49 -04:00
|
|
|
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
|
|
|
|
}
|
|
|
|
candidates.sort!
|
|
|
|
candidates.uniq!
|
|
|
|
end
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
2019-07-18 19:23:37 -04:00
|
|
|
"#{rec.class.name}#{sep}#{candidates.find{ |i| i == message }}"
|
2019-05-26 12:24:03 -04:00
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
"String.#{candidates.find{ |i| i == message }}"
|
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
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}
|
2019-05-26 12:24:03 -04:00
|
|
|
conditions |= ReservedWords
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2019-05-26 12:24:03 -04:00
|
|
|
if doc_namespace
|
|
|
|
candidates.find{ |i| i == input }
|
|
|
|
else
|
|
|
|
candidates.grep(/^#{Regexp.quote(input)}/)
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2019-05-26 12:24:03 -04:00
|
|
|
end
|
2001-04-30 14:25:04 -04:00
|
|
|
|
2019-05-28 17:08:25 -04:00
|
|
|
PerfectMatchedProc = ->(matched) {
|
2019-06-10 08:07:24 -04:00
|
|
|
RDocRIDriver ||= RDoc::RI::Driver.new
|
2019-05-28 17:11:24 -04:00
|
|
|
if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
|
2019-05-28 17:08:25 -04:00
|
|
|
File.open(File.join(__dir__, 'ruby_logo.aa')) do |f|
|
|
|
|
RDocRIDriver.page do |io|
|
2019-05-28 17:53:18 -04:00
|
|
|
IO.copy_stream(f, io)
|
2019-05-28 17:08:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2019-05-26 12:24:03 -04:00
|
|
|
namespace = retrieve_completion_data(matched, true)
|
|
|
|
return unless matched
|
|
|
|
if namespace.is_a?(Array)
|
|
|
|
out = RDoc::Markup::Document.new
|
|
|
|
namespace.each do |m|
|
|
|
|
begin
|
|
|
|
RDocRIDriver.add_method(out, m)
|
|
|
|
rescue RDoc::RI::Driver::NotFoundError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RDocRIDriver.display(out)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
RDocRIDriver.display_names([namespace])
|
|
|
|
rescue RDoc::RI::Driver::NotFoundError
|
|
|
|
end
|
2019-04-27 18:47:33 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
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
|
2018-03-12 21:28:27 -04:00
|
|
|
|
|
|
|
def self.ignored_modules
|
|
|
|
# We could cache the result, but this is very fast already.
|
|
|
|
# By using this approach, we avoid Module#name calls, which are
|
|
|
|
# relatively slow when there are a lot of anonymous modules defined.
|
2018-03-12 21:28:28 -04:00
|
|
|
s = {}
|
2018-03-12 21:28:27 -04:00
|
|
|
|
|
|
|
scanner = lambda do |m|
|
|
|
|
next if s.include?(m) # IRB::ExtendCommandBundle::EXCB recurses.
|
2018-03-12 21:28:28 -04:00
|
|
|
s[m] = true
|
2018-03-12 21:28:27 -04:00
|
|
|
m.constants(false).each do |c|
|
|
|
|
value = m.const_get(c)
|
|
|
|
scanner.call(value) if value.is_a?(Module)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
%i(IRB SLex RubyLex RubyToken).each do |sym|
|
|
|
|
next unless Object.const_defined?(sym)
|
|
|
|
scanner.call(Object.const_get(sym))
|
|
|
|
end
|
|
|
|
|
|
|
|
s.delete(IRB::Context) if defined?(IRB::Context)
|
|
|
|
|
|
|
|
s
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|