Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default completor from regexp to type-completor if RUBY_VERSION>=3.4 #1010

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,17 @@ def use_loader=(val)

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}"
Expand All @@ -189,17 +195,17 @@ def use_loader=(val)
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

Expand Down
2 changes: 1 addition & 1 deletion lib/irb/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def IRB.init_config(ap_path)
@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
Expand Down
2 changes: 2 additions & 0 deletions test/irb/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ def test_irb_path_setter
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
Expand Down
10 changes: 7 additions & 3 deletions test/irb/test_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ def test_completor_environment_variable
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__)
Expand All @@ -193,10 +194,12 @@ def test_completor_environment_variable

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]
Expand All @@ -205,6 +208,7 @@ def test_completor_setup_with_argv
assert_equal :regexp, IRB.conf[:COMPLETOR]
ensure
IRB.conf[:COMPLETOR] = orig_completor_conf
ENV['IRB_COMPLETOR'] = orig_completor_env
end

def test_noscript
Expand Down
16 changes: 16 additions & 0 deletions test/irb/test_type_completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def empty_binding
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)
Expand Down
Loading