Skip to content

Commit

Permalink
Change default completor from regexp to auto, try TypeCompletor and f…
Browse files Browse the repository at this point in the history
…allback to RegexpCompletor.
  • Loading branch information
tompng committed Sep 30, 2024
1 parent 04cd231 commit c0d6a80
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def use_loader=(val)
case completor_type
when :regexp
return RegexpCompletor.new
when :type
completor = build_type_completor
when :type, nil
completor = build_type_completor(verbose: !!completor_type)
return completor if completor
else
warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}"
Expand All @@ -189,17 +189,17 @@ def use_loader=(val)
RegexpCompletor.new
end

private def build_type_completor
private def build_type_completor(verbose: false)
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
9 changes: 6 additions & 3 deletions test/irb/test_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_completor_environment_variable

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 +193,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 nil will try type completor and fallbacks to regexp without warning
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 +207,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
15 changes: 15 additions & 0 deletions test/irb/test_type_completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ def empty_binding
binding
end

def test_build_completor
verbose, $VERBOSE = $VERBOSE, nil
original_completor = IRB.conf[:COMPLETOR]
workspace = IRB::WorkSpace.new(Object.new)
@context = IRB::Context.new(nil, workspace, TestInputMethod.new)
# Default (nil, auto) tries to use TypeCompletor
IRB.conf[:COMPLETOR] = nil
assert_equal 'IRB::TypeCompletor', @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

0 comments on commit c0d6a80

Please sign in to comment.