[ruby/irb] Change default completor from regexp to auto, try

TypeCompletor and fallback to RegexpCompletor.
(https://github.com/ruby/irb/pull/1010)

https://github.com/ruby/irb/commit/bb6a99d815
This commit is contained in:
tomoya ishida 2024-10-06 20:10:09 +09:00 committed by git
parent a6383fbe16
commit 98620f6c52
5 changed files with 36 additions and 8 deletions

View File

@ -176,11 +176,17 @@ module IRB
private def build_completor
completor_type = IRB.conf[:COMPLETOR]
# Gem repl_type_completor is added to bundled gems in Ruby 3.4.
# Use :type as default completor only in Ruby 3.4 or later.
verbose = !!completor_type
completor_type ||= RUBY_VERSION >= '3.4' ? :type : :regexp
case completor_type
when :regexp
return RegexpCompletor.new
when :type
completor = build_type_completor
completor = build_type_completor(verbose: verbose)
return completor if completor
else
warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}"
@ -189,17 +195,17 @@ module IRB
RegexpCompletor.new
end
private def build_type_completor
private def build_type_completor(verbose:)
if RUBY_ENGINE == 'truffleruby'
# Avoid SyntaxError. truffleruby does not support endless method definition yet.
warn 'TypeCompletor is not supported on TruffleRuby yet'
warn 'TypeCompletor is not supported on TruffleRuby yet' if verbose
return
end
begin
require 'repl_type_completor'
rescue LoadError => e
warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}"
warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}" if verbose
return
end

View File

@ -80,7 +80,7 @@ module IRB # :nodoc:
@CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod)
@CONF[:USE_COLORIZE] = (nc = ENV['NO_COLOR']).nil? || nc.empty?
@CONF[:USE_AUTOCOMPLETE] = ENV.fetch("IRB_USE_AUTOCOMPLETE", "true") != "false"
@CONF[:COMPLETOR] = ENV.fetch("IRB_COMPLETOR", "regexp").to_sym
@CONF[:COMPLETOR] = ENV["IRB_COMPLETOR"]&.to_sym
@CONF[:INSPECT_MODE] = true
@CONF[:USE_TRACER] = false
@CONF[:USE_LOADER] = false

View File

@ -705,6 +705,8 @@ module TestIRB
def test_build_completor
verbose, $VERBOSE = $VERBOSE, nil
original_completor = IRB.conf[:COMPLETOR]
IRB.conf[:COMPLETOR] = nil
assert_match /IRB::(Regexp|Type)Completor/, @context.send(:build_completor).class.name
IRB.conf[:COMPLETOR] = :regexp
assert_equal 'IRB::RegexpCompletor', @context.send(:build_completor).class.name
IRB.conf[:COMPLETOR] = :unknown

View File

@ -167,9 +167,10 @@ module TestIRB
orig_use_autocomplete_env = ENV['IRB_COMPLETOR']
orig_use_autocomplete_conf = IRB.conf[:COMPLETOR]
# Default value is nil: auto-detect
ENV['IRB_COMPLETOR'] = nil
IRB.setup(__FILE__)
assert_equal(:regexp, IRB.conf[:COMPLETOR])
assert_equal(nil, IRB.conf[:COMPLETOR])
ENV['IRB_COMPLETOR'] = 'regexp'
IRB.setup(__FILE__)
@ -193,10 +194,12 @@ module TestIRB
def test_completor_setup_with_argv
orig_completor_conf = IRB.conf[:COMPLETOR]
orig_completor_env = ENV['IRB_COMPLETOR']
ENV['IRB_COMPLETOR'] = nil
# Default is :regexp
# Default value is nil: auto-detect
IRB.setup(__FILE__, argv: [])
assert_equal :regexp, IRB.conf[:COMPLETOR]
assert_equal nil, IRB.conf[:COMPLETOR]
IRB.setup(__FILE__, argv: ['--type-completor'])
assert_equal :type, IRB.conf[:COMPLETOR]
@ -205,6 +208,7 @@ module TestIRB
assert_equal :regexp, IRB.conf[:COMPLETOR]
ensure
IRB.conf[:COMPLETOR] = orig_completor_conf
ENV['IRB_COMPLETOR'] = orig_completor_env
end
def test_noscript

View File

@ -27,6 +27,22 @@ module TestIRB
binding
end
def test_build_completor
IRB.init_config(nil)
verbose, $VERBOSE = $VERBOSE, nil
original_completor = IRB.conf[:COMPLETOR]
workspace = IRB::WorkSpace.new(Object.new)
@context = IRB::Context.new(nil, workspace, TestInputMethod.new)
IRB.conf[:COMPLETOR] = nil
expected_default_completor = RUBY_VERSION >= '3.4' ? 'IRB::TypeCompletor' : 'IRB::RegexpCompletor'
assert_equal expected_default_completor, @context.send(:build_completor).class.name
IRB.conf[:COMPLETOR] = :type
assert_equal 'IRB::TypeCompletor', @context.send(:build_completor).class.name
ensure
$VERBOSE = verbose
IRB.conf[:COMPLETOR] = original_completor
end
def assert_completion(preposing, target, binding: empty_binding, include: nil, exclude: nil)
raise ArgumentError if include.nil? && exclude.nil?
candidates = @completor.completion_candidates(preposing, target, '', bind: binding)