2024-02-23 13:53:50 +03:00
|
|
|
# frozen_string_literal: true
|
2001-04-30 22:25:04 +04:00
|
|
|
#
|
2018-07-28 13:00:29 +03:00
|
|
|
# irb/completion.rb -
|
2001-04-30 22:25:04 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
|
|
|
# From Original Idea of shugo@ruby-lang.org
|
|
|
|
#
|
2000-05-12 13:07:57 +04:00
|
|
|
|
2021-03-24 08:55:05 +03:00
|
|
|
require_relative 'ruby-lex'
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
module IRB
|
2023-10-11 20:08:59 +03:00
|
|
|
class BaseCompletor # :nodoc:
|
2023-11-08 05:46:24 +03:00
|
|
|
|
|
|
|
# Set of reserved words used by Ruby, you should not use these for
|
|
|
|
# constants or variables
|
|
|
|
ReservedWords = %w[
|
|
|
|
__ENCODING__ __LINE__ __FILE__
|
|
|
|
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
|
|
|
|
]
|
|
|
|
|
2024-07-05 20:25:06 +03:00
|
|
|
HELP_COMMAND_PREPOSING = /\Ahelp\s+/
|
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def completion_candidates(preposing, target, postposing, bind:)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def doc_namespace(preposing, matched, postposing, bind:)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2019-04-28 00:37:39 +03:00
|
|
|
|
2022-11-06 17:55:24 +03:00
|
|
|
GEM_PATHS =
|
|
|
|
if defined?(Gem::Specification)
|
|
|
|
Gem::Specification.latest_specs(true).map { |s|
|
|
|
|
s.require_paths.map { |p|
|
2023-04-06 00:40:34 +03:00
|
|
|
if File.absolute_path?(p)
|
2022-11-06 17:55:24 +03:00
|
|
|
p
|
|
|
|
else
|
|
|
|
File.join(s.full_gem_path, p)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}.flatten
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end.freeze
|
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def retrieve_gem_and_system_load_path
|
2022-11-06 17:55:24 +03:00
|
|
|
candidates = (GEM_PATHS | $LOAD_PATH)
|
2022-09-19 18:14:03 +03:00
|
|
|
candidates.map do |p|
|
|
|
|
if p.respond_to?(:to_path)
|
|
|
|
p.to_path
|
|
|
|
else
|
|
|
|
String(p) rescue nil
|
|
|
|
end
|
|
|
|
end.compact.sort
|
2021-09-07 22:42:26 +03:00
|
|
|
end
|
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def retrieve_files_to_require_from_load_path
|
|
|
|
@files_from_load_path ||=
|
2021-09-17 14:30:16 +03:00
|
|
|
(
|
|
|
|
shortest = []
|
|
|
|
rest = retrieve_gem_and_system_load_path.each_with_object([]) { |path, result|
|
|
|
|
begin
|
|
|
|
names = Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
next if names.empty?
|
|
|
|
names.map! { |n| n.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '') }.sort!
|
|
|
|
shortest << names.shift
|
|
|
|
result.concat(names)
|
|
|
|
}
|
|
|
|
shortest.sort! | rest
|
|
|
|
)
|
2021-03-24 09:33:07 +03:00
|
|
|
end
|
|
|
|
|
2024-07-05 20:25:06 +03:00
|
|
|
def command_candidates(target)
|
|
|
|
if !target.empty?
|
2024-04-20 21:55:51 +03:00
|
|
|
IRB::Command.command_names.select { _1.start_with?(target) }
|
2024-04-10 19:52:47 +03:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def retrieve_files_to_require_relative_from_current_dir
|
|
|
|
@files_from_current_dir ||= Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
|
2021-03-24 09:33:07 +03:00
|
|
|
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
|
|
|
|
}
|
|
|
|
end
|
2023-10-11 20:08:59 +03:00
|
|
|
end
|
2021-03-24 09:33:07 +03:00
|
|
|
|
2023-11-29 19:30:08 +03:00
|
|
|
class TypeCompletor < BaseCompletor # :nodoc:
|
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
ReplTypeCompletor.info
|
|
|
|
end
|
|
|
|
|
|
|
|
def completion_candidates(preposing, target, _postposing, bind:)
|
2024-07-05 20:25:06 +03:00
|
|
|
# When completing the argument of `help` command, only commands should be candidates
|
|
|
|
return command_candidates(target) if preposing.match?(HELP_COMMAND_PREPOSING)
|
|
|
|
|
|
|
|
commands = if preposing.empty?
|
|
|
|
command_candidates(target)
|
|
|
|
# It doesn't make sense to propose commands with other preposing
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2023-11-29 19:30:08 +03:00
|
|
|
result = ReplTypeCompletor.analyze(preposing + target, binding: bind, filename: @context.irb_path)
|
2024-07-05 20:25:06 +03:00
|
|
|
|
2024-04-10 19:52:47 +03:00
|
|
|
return commands unless result
|
|
|
|
|
|
|
|
commands | result.completion_candidates.map { target + _1 }
|
2023-11-29 19:30:08 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def doc_namespace(preposing, matched, _postposing, bind:)
|
|
|
|
result = ReplTypeCompletor.analyze(preposing + matched, binding: bind, filename: @context.irb_path)
|
|
|
|
result&.doc_namespace('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
class RegexpCompletor < BaseCompletor # :nodoc:
|
|
|
|
using Module.new {
|
|
|
|
refine ::Binding do
|
|
|
|
def eval_methods
|
|
|
|
::Kernel.instance_method(:methods).bind(eval("self")).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval_private_methods
|
|
|
|
::Kernel.instance_method(:private_methods).bind(eval("self")).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval_instance_variables
|
|
|
|
::Kernel.instance_method(:instance_variables).bind(eval("self")).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval_global_variables
|
|
|
|
::Kernel.instance_method(:global_variables).bind(eval("self")).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval_class_constants
|
|
|
|
::Module.instance_method(:constants).bind(eval("self.class")).call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2023-11-08 05:46:24 +03:00
|
|
|
def inspect
|
|
|
|
'RegexpCompletor'
|
|
|
|
end
|
2023-10-11 20:08:59 +03:00
|
|
|
|
|
|
|
def complete_require_path(target, preposing, postposing)
|
2021-03-24 08:55:05 +03:00
|
|
|
if target =~ /\A(['"])([^'"]+)\Z/
|
|
|
|
quote = $1
|
|
|
|
actual_target = $2
|
|
|
|
else
|
|
|
|
return nil # It's not String literal
|
|
|
|
end
|
|
|
|
tokens = RubyLex.ripper_lex_without_warning(preposing.gsub(/\s*\z/, ''))
|
|
|
|
tok = nil
|
|
|
|
tokens.reverse_each do |t|
|
|
|
|
unless [:on_lparen, :on_sp, :on_ignored_sp, :on_nl, :on_ignored_nl, :on_comment].include?(t.event)
|
|
|
|
tok = t
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-10-12 19:54:04 +03:00
|
|
|
return unless tok&.event == :on_ident && tok.state == Ripper::EXPR_CMDARG
|
|
|
|
|
|
|
|
case tok.tok
|
|
|
|
when 'require'
|
|
|
|
retrieve_files_to_require_from_load_path.select { |path|
|
|
|
|
path.start_with?(actual_target)
|
|
|
|
}.map { |path|
|
|
|
|
quote + path
|
|
|
|
}
|
|
|
|
when 'require_relative'
|
|
|
|
retrieve_files_to_require_relative_from_current_dir.select { |path|
|
|
|
|
path.start_with?(actual_target)
|
|
|
|
}.map { |path|
|
|
|
|
quote + path
|
|
|
|
}
|
2021-03-24 08:55:05 +03:00
|
|
|
end
|
2023-10-11 20:08:59 +03:00
|
|
|
end
|
2021-03-24 08:55:05 +03:00
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def completion_candidates(preposing, target, postposing, bind:)
|
2024-07-05 20:25:06 +03:00
|
|
|
if result = complete_require_path(target, preposing, postposing)
|
|
|
|
return result
|
2021-03-24 08:55:05 +03:00
|
|
|
end
|
2024-07-05 20:25:06 +03:00
|
|
|
|
|
|
|
commands = command_candidates(target)
|
|
|
|
|
|
|
|
# When completing the argument of `help` command, only commands should be candidates
|
|
|
|
return commands if preposing.match?(HELP_COMMAND_PREPOSING)
|
|
|
|
|
|
|
|
# It doesn't make sense to propose commands with other preposing
|
|
|
|
commands = [] unless preposing.empty?
|
|
|
|
|
|
|
|
completion_data = retrieve_completion_data(target, bind: bind, doc_namespace: false).compact.map{ |i| i.encode(Encoding.default_external) }
|
|
|
|
commands | completion_data
|
2023-10-11 20:08:59 +03:00
|
|
|
end
|
2019-05-26 19:24:03 +03:00
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def doc_namespace(_preposing, matched, _postposing, bind:)
|
|
|
|
retrieve_completion_data(matched, bind: bind, doc_namespace: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_completion_data(input, bind:, doc_namespace:)
|
2000-05-12 13:07:57 +04:00
|
|
|
case input
|
2023-03-06 08:52:41 +03:00
|
|
|
# this regexp only matches the closing character because of irb's Reline.completer_quote_characters setting
|
|
|
|
# details are described in: https://github.com/ruby/irb/pull/523
|
|
|
|
when /^(.*["'`])\.([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# String
|
|
|
|
receiver = $1
|
2023-03-06 08:52:41 +03:00
|
|
|
message = $2
|
2011-06-29 19:08:41 +04:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"String.#{message}"
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
candidates = String.instance_methods.collect{|m| m.to_s}
|
2019-05-26 19:24:03 +03:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
2011-06-29 19:08:41 +04:00
|
|
|
|
2023-03-06 08:52:41 +03:00
|
|
|
# this regexp only matches the closing character because of irb's Reline.completer_quote_characters setting
|
|
|
|
# details are described in: https://github.com/ruby/irb/pull/523
|
|
|
|
when /^(.*\/)\.([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Regexp
|
|
|
|
receiver = $1
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $2
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"Regexp.#{message}"
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
candidates = Regexp.instance_methods.collect{|m| m.to_s}
|
2019-05-26 19:24:03 +03:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
|
|
|
when /^([^\]]*\])\.([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Array
|
|
|
|
receiver = $1
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $2
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"Array.#{message}"
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
candidates = Array.instance_methods.collect{|m| m.to_s}
|
2019-05-26 19:24:03 +03:00
|
|
|
select_message(receiver, message, candidates)
|
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
|
|
|
when /^([^\}]*\})\.([^.]*)$/
|
2023-11-18 20:49:01 +03:00
|
|
|
# Hash or Proc
|
2014-08-09 05:36:49 +04:00
|
|
|
receiver = $1
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $2
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
2023-11-18 20:49:01 +03:00
|
|
|
["Hash.#{message}", "Proc.#{message}"]
|
2019-05-26 19:24:03 +03:00
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
|
2023-11-18 20:49:01 +03:00
|
|
|
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
|
|
|
|
select_message(receiver, message, hash_candidates | proc_candidates)
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2023-03-03 16:41:17 +03:00
|
|
|
when /^(:[^:.]+)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Symbol
|
2022-10-06 13:54:11 +03:00
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
sym = $1
|
|
|
|
candidates = Symbol.all_symbols.collect do |s|
|
2023-06-13 13:46:34 +03:00
|
|
|
s.inspect
|
2022-10-06 13:54:11 +03:00
|
|
|
rescue EncodingError
|
|
|
|
# ignore
|
|
|
|
end
|
|
|
|
candidates.grep(/^#{Regexp.quote(sym)}/)
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2021-10-10 22:30:42 +03:00
|
|
|
when /^::([A-Z][^:\.\(\)]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Absolute Constant or class methods
|
|
|
|
receiver = $1
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2014-08-09 05:36:49 +04:00
|
|
|
candidates = Object.constants.collect{|m| m.to_s}
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
candidates.find { |i| i == receiver }
|
|
|
|
else
|
2023-06-05 21:34:05 +03:00
|
|
|
candidates.grep(/^#{Regexp.quote(receiver)}/).collect{|e| "::" + e}
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2010-04-13 09:01:10 +04:00
|
|
|
when /^([A-Z].*)::([^:.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Constant or class methods
|
|
|
|
receiver = $1
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $2
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"#{receiver}::#{message}"
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
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
|
|
|
|
|
2022-10-06 14:37:37 +03:00
|
|
|
select_message(receiver, message, candidates.sort, "::")
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2011-01-18 12:34:10 +03:00
|
|
|
when /^(:[^:.]+)(\.|::)([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Symbol
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $3
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"Symbol.#{message}"
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
candidates = Symbol.instance_methods.collect{|m| m.to_s}
|
2019-05-26 19:24:03 +03:00
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2020-01-30 06:08:37 +03:00
|
|
|
when /^(?<num>-?(?:0[dbo])?[0-9_]+(?:\.[0-9_]+)?(?:(?:[eE][+-]?[0-9]+)?i?|r)?)(?<sep>\.|::)(?<mes>[^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Numeric
|
2019-05-26 19:24:03 +03:00
|
|
|
receiver = $~[:num]
|
|
|
|
sep = $~[:sep]
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $~[:mes]
|
2014-08-09 05:36:49 +04:00
|
|
|
|
|
|
|
begin
|
2019-05-26 19:24:03 +03:00
|
|
|
instance = eval(receiver, bind)
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
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-09 05:36:49 +04:00
|
|
|
rescue Exception
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
[]
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2006-07-19 18:18:20 +04:00
|
|
|
|
2011-01-18 12:34:10 +03:00
|
|
|
when /^(-?0x[0-9a-fA-F_]+)(\.|::)([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# Numeric(0xFFFF)
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $3
|
2014-08-09 05:36:49 +04:00
|
|
|
|
|
|
|
begin
|
2019-05-26 19:24:03 +03: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-09 05:36:49 +04:00
|
|
|
rescue Exception
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
2022-10-06 13:54:11 +03:00
|
|
|
[]
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
when /^(\$[^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# global var
|
2019-05-26 19:24:03 +03:00
|
|
|
gvar = $1
|
|
|
|
all_gvars = global_variables.collect{|m| m.to_s}
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
all_gvars.find{ |i| i == gvar }
|
|
|
|
else
|
|
|
|
all_gvars.grep(Regexp.new(Regexp.quote(gvar)))
|
|
|
|
end
|
2002-07-09 15:17:17 +04:00
|
|
|
|
2021-09-10 22:30:29 +03:00
|
|
|
when /^([^.:"].*)(\.|::)([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# variable.func or func.func
|
|
|
|
receiver = $1
|
|
|
|
sep = $2
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $3
|
2014-08-09 05:36:49 +04:00
|
|
|
|
2022-10-03 00:55:50 +03:00
|
|
|
gv = bind.eval_global_variables.collect{|m| m.to_s}.push("true", "false", "nil")
|
|
|
|
lv = bind.local_variables.collect{|m| m.to_s}
|
|
|
|
iv = bind.eval_instance_variables.collect{|m| m.to_s}
|
|
|
|
cv = bind.eval_class_constants.collect{|m| m.to_s}
|
2014-08-09 05:36:49 +04:00
|
|
|
|
|
|
|
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 = []
|
|
|
|
end
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
2021-09-03 23:33:03 +03:00
|
|
|
rec_class = rec.is_a?(Module) ? rec : rec.class
|
2024-03-14 18:40:59 +03:00
|
|
|
"#{rec_class.name}#{sep}#{candidates.find{ |i| i == message }}" rescue nil
|
2019-05-26 19:24:03 +03:00
|
|
|
else
|
|
|
|
select_message(receiver, message, candidates, sep)
|
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
|
|
|
when /^\.([^.]*)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
# unknown(maybe String)
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2014-08-09 05:36:49 +04:00
|
|
|
receiver = ""
|
2021-01-06 13:05:46 +03:00
|
|
|
message = $1
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2014-08-09 05:36:49 +04:00
|
|
|
candidates = String.instance_methods(true).collect{|m| m.to_s}
|
2022-10-06 13:54:11 +03:00
|
|
|
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
|
|
|
"String.#{candidates.find{ |i| i == message }}"
|
|
|
|
else
|
2022-07-18 19:45:38 +03:00
|
|
|
select_message(receiver, message, candidates.sort)
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2024-01-03 16:47:47 +03:00
|
|
|
when /^\s*$/
|
|
|
|
# empty input
|
|
|
|
if doc_namespace
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
else
|
2019-05-26 19:24:03 +03:00
|
|
|
if doc_namespace
|
2022-10-03 00:55:50 +03:00
|
|
|
vars = (bind.local_variables | bind.eval_instance_variables).collect{|m| m.to_s}
|
2021-09-02 15:34:53 +03:00
|
|
|
perfect_match_var = vars.find{|m| m.to_s == input}
|
|
|
|
if perfect_match_var
|
2024-03-14 18:40:59 +03:00
|
|
|
eval("#{perfect_match_var}.class.name", bind) rescue nil
|
2021-09-02 15:34:53 +03:00
|
|
|
else
|
2022-10-03 00:55:50 +03:00
|
|
|
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s}
|
2021-09-02 15:34:53 +03:00
|
|
|
candidates |= ReservedWords
|
|
|
|
candidates.find{ |i| i == input }
|
|
|
|
end
|
2019-05-26 19:24:03 +03:00
|
|
|
else
|
2022-10-03 00:55:50 +03:00
|
|
|
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s}
|
2021-09-02 15:34:53 +03:00
|
|
|
candidates |= ReservedWords
|
2022-07-18 19:45:38 +03:00
|
|
|
candidates.grep(/^#{Regexp.quote(input)}/).sort
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
2019-05-26 19:24:03 +03:00
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Set of available operators in Ruby
|
2013-06-04 18:17:17 +04:00
|
|
|
Operators = %w[% & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ ! != !~]
|
2001-04-30 22:25:04 +04:00
|
|
|
|
2023-10-11 20:08:59 +03:00
|
|
|
def select_message(receiver, message, candidates, sep = ".")
|
2021-01-06 13:05:46 +03:00
|
|
|
candidates.grep(/^#{Regexp.quote(message)}/).collect do |e|
|
2014-08-09 05:36:49 +04:00
|
|
|
case e
|
|
|
|
when /^[a-zA-Z_]/
|
|
|
|
receiver + sep + e
|
|
|
|
when /^[0-9]/
|
|
|
|
when *Operators
|
|
|
|
#receiver + " " + e
|
|
|
|
end
|
2001-04-30 22:25:04 +04:00
|
|
|
end
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
2023-10-14 17:12:37 +03:00
|
|
|
|
|
|
|
module InputCompletor
|
|
|
|
class << self
|
|
|
|
private def regexp_completor
|
|
|
|
@regexp_completor ||= RegexpCompletor.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace.binding, doc_namespace: false)
|
|
|
|
regexp_completor.retrieve_completion_data(input, bind: bind, doc_namespace: doc_namespace)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CompletionProc = ->(target, preposing = nil, postposing = nil) {
|
2024-07-05 20:25:06 +03:00
|
|
|
regexp_completor.completion_candidates(preposing || '', target, postposing || '', bind: IRB.conf[:MAIN_CONTEXT].workspace.binding)
|
2023-10-14 17:12:37 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
deprecate_constant :InputCompletor
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|