# frozen_string_literal: false
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
# From Original Idea of shugo@ruby-lang.org
module InputCompletor # :nodoc:
# Set of reserved words used by Ruby, you should not use these for
__ENCODING__ __LINE__ __FILE__
BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
CompletionProc = proc { |input|
retrieve_completion_data(input).compact.map{ |i| i.encode(Encoding.default_external) }
def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace.binding, doc_namespace: false)
when /^((["'`]).*\2)\.([^.]*)$/
message = Regexp.quote($3)
candidates = String.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
when /^(\/[^\/]*\/)\.([^.]*)$/
message = Regexp.quote($2)
candidates = Regexp.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
when /^([^\]]*\])\.([^.]*)$/
message = Regexp.quote($2)
candidates = Array.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
when /^([^\}]*\})\.([^.]*)$/
message = Regexp.quote($2)
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
["Proc.#{message}", "Hash.#{message}"]
select_message(receiver, message, proc_candidates | hash_candidates)
return nil if doc_namespace
candidates = Symbol.all_symbols.collect do |s|
":" + s.id2name.encode(Encoding.default_external)
rescue Encoding::UndefinedConversionError
candidates.grep(/^#{Regexp.quote(sym)}/)
when /^::([A-Z][^:\.\(]*)$/
# Absolute Constant or class methods
candidates = Object.constants.collect{|m| m.to_s}
candidates.find { |i| i == receiver }
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
when /^([A-Z].*)::([^:.]*)$/
# Constant or class methods
message = Regexp.quote($2)
candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
"#{receiver}::#{message}"
select_message(receiver, message, candidates, "::")
when /^(:[^:.]+)(\.|::)([^.]*)$/
message = Regexp.quote($3)
candidates = Symbol.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates, sep)
when /^(?<num>-?(?:0[dbo])?[0-9_]+(?:\.[0-9_]+)?(?:(?:[eE][+-]?[0-9]+)?i?|r)?)(?<sep>\.|::)(?<mes>[^.]*)$/
message = Regexp.quote($~[:mes])
instance = eval(receiver, bind)
"#{instance.class.name}.#{message}"
candidates = instance.methods.collect{|m| m.to_s}
select_message(receiver, message, candidates, sep)
when /^(-?0x[0-9a-fA-F_]+)(\.|::)([^.]*)$/
message = Regexp.quote($3)
instance = eval(receiver, bind)
"#{instance.class.name}.#{message}"
candidates = instance.methods.collect{|m| m.to_s}
select_message(receiver, message, candidates, sep)
all_gvars = global_variables.collect{|m| m.to_s}
all_gvars.find{ |i| i == gvar }
all_gvars.grep(Regexp.new(Regexp.quote(gvar)))
when /^([^."].*)(\.|::)([^.]*)$/
# variable.func or func.func
message = Regexp.quote($3)
gv = eval("global_variables", bind).collect{|m| m.to_s}.push("true", "false", "nil")
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
rec = eval(receiver, bind)
if sep == "::" and rec.kind_of?(Module)
candidates = rec.constants.collect{|m| m.to_s}
candidates |= rec.methods.collect{|m| m.to_s}
to_ignore = ignored_modules
ObjectSpace.each_object(Module){|m|
next if (to_ignore.include?(m) rescue true)
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
"#{rec.class.name}#{sep}#{candidates.find{ |i| i == message }}"
select_message(receiver, message, candidates, sep)
message = Regexp.quote($1)
candidates = String.instance_methods(true).collect{|m| m.to_s}
"String.#{candidates.find{ |i| i == message }}"
select_message(receiver, message, candidates)
candidates = eval("methods | private_methods | local_variables | instance_variables | self.class.constants", bind).collect{|m| m.to_s}
candidates |= ReservedWords
candidates.find{ |i| i == input }
candidates.grep(/^#{Regexp.quote(input)}/)
PerfectMatchedProc = ->(matched, bind: IRB.conf[:MAIN_CONTEXT].workspace.binding) {
RDocRIDriver ||= RDoc::RI::Driver.new
if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
namespace = retrieve_completion_data(matched, bind: bind, doc_namespace: true)
if namespace.is_a?(Array)
out = RDoc::Markup::Document.new
RDocRIDriver.add_method(out, m)
rescue RDoc::RI::Driver::NotFoundError
RDocRIDriver.display(out)
RDocRIDriver.display_names([namespace])
rescue RDoc::RI::Driver::NotFoundError
# Set of available operators in Ruby
Operators = %w[% & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ ! != !~]
def self.select_message(receiver, message, candidates, sep = ".")
candidates.grep(/^#{message}/).collect do |e|
# 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.
next if s.include?(m) # IRB::ExtendCommandBundle::EXCB recurses.
m.constants(false).each do |c|
scanner.call(value) if value.is_a?(Module)
%i(IRB RubyLex).each do |sym|
next unless Object.const_defined?(sym)
scanner.call(Object.const_get(sym))
s.delete(IRB::Context) if defined?(IRB::Context)