Skip to content

Commit 98620f6

Browse files
tompngmatzbot
authored andcommitted
[ruby/irb] Change default completor from regexp to auto, try
TypeCompletor and fallback to RegexpCompletor. (ruby/irb#1010) ruby/irb@bb6a99d815
1 parent a6383fb commit 98620f6

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

lib/irb/context.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,17 @@ def use_loader=(val)
176176

177177
private def build_completor
178178
completor_type = IRB.conf[:COMPLETOR]
179+
180+
# Gem repl_type_completor is added to bundled gems in Ruby 3.4.
181+
# Use :type as default completor only in Ruby 3.4 or later.
182+
verbose = !!completor_type
183+
completor_type ||= RUBY_VERSION >= '3.4' ? :type : :regexp
184+
179185
case completor_type
180186
when :regexp
181187
return RegexpCompletor.new
182188
when :type
183-
completor = build_type_completor
189+
completor = build_type_completor(verbose: verbose)
184190
return completor if completor
185191
else
186192
warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}"
@@ -189,17 +195,17 @@ def use_loader=(val)
189195
RegexpCompletor.new
190196
end
191197

192-
private def build_type_completor
198+
private def build_type_completor(verbose:)
193199
if RUBY_ENGINE == 'truffleruby'
194200
# Avoid SyntaxError. truffleruby does not support endless method definition yet.
195-
warn 'TypeCompletor is not supported on TruffleRuby yet'
201+
warn 'TypeCompletor is not supported on TruffleRuby yet' if verbose
196202
return
197203
end
198204

199205
begin
200206
require 'repl_type_completor'
201207
rescue LoadError => e
202-
warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}"
208+
warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}" if verbose
203209
return
204210
end
205211

lib/irb/init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def IRB.init_config(ap_path)
8080
@CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod)
8181
@CONF[:USE_COLORIZE] = (nc = ENV['NO_COLOR']).nil? || nc.empty?
8282
@CONF[:USE_AUTOCOMPLETE] = ENV.fetch("IRB_USE_AUTOCOMPLETE", "true") != "false"
83-
@CONF[:COMPLETOR] = ENV.fetch("IRB_COMPLETOR", "regexp").to_sym
83+
@CONF[:COMPLETOR] = ENV["IRB_COMPLETOR"]&.to_sym
8484
@CONF[:INSPECT_MODE] = true
8585
@CONF[:USE_TRACER] = false
8686
@CONF[:USE_LOADER] = false

test/irb/test_context.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ def test_irb_path_setter
705705
def test_build_completor
706706
verbose, $VERBOSE = $VERBOSE, nil
707707
original_completor = IRB.conf[:COMPLETOR]
708+
IRB.conf[:COMPLETOR] = nil
709+
assert_match /IRB::(Regexp|Type)Completor/, @context.send(:build_completor).class.name
708710
IRB.conf[:COMPLETOR] = :regexp
709711
assert_equal 'IRB::RegexpCompletor', @context.send(:build_completor).class.name
710712
IRB.conf[:COMPLETOR] = :unknown

test/irb/test_init.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ def test_completor_environment_variable
167167
orig_use_autocomplete_env = ENV['IRB_COMPLETOR']
168168
orig_use_autocomplete_conf = IRB.conf[:COMPLETOR]
169169

170+
# Default value is nil: auto-detect
170171
ENV['IRB_COMPLETOR'] = nil
171172
IRB.setup(__FILE__)
172-
assert_equal(:regexp, IRB.conf[:COMPLETOR])
173+
assert_equal(nil, IRB.conf[:COMPLETOR])
173174

174175
ENV['IRB_COMPLETOR'] = 'regexp'
175176
IRB.setup(__FILE__)
@@ -193,10 +194,12 @@ def test_completor_environment_variable
193194

194195
def test_completor_setup_with_argv
195196
orig_completor_conf = IRB.conf[:COMPLETOR]
197+
orig_completor_env = ENV['IRB_COMPLETOR']
198+
ENV['IRB_COMPLETOR'] = nil
196199

197-
# Default is :regexp
200+
# Default value is nil: auto-detect
198201
IRB.setup(__FILE__, argv: [])
199-
assert_equal :regexp, IRB.conf[:COMPLETOR]
202+
assert_equal nil, IRB.conf[:COMPLETOR]
200203

201204
IRB.setup(__FILE__, argv: ['--type-completor'])
202205
assert_equal :type, IRB.conf[:COMPLETOR]
@@ -205,6 +208,7 @@ def test_completor_setup_with_argv
205208
assert_equal :regexp, IRB.conf[:COMPLETOR]
206209
ensure
207210
IRB.conf[:COMPLETOR] = orig_completor_conf
211+
ENV['IRB_COMPLETOR'] = orig_completor_env
208212
end
209213

210214
def test_noscript

test/irb/test_type_completor.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ def empty_binding
2727
binding
2828
end
2929

30+
def test_build_completor
31+
IRB.init_config(nil)
32+
verbose, $VERBOSE = $VERBOSE, nil
33+
original_completor = IRB.conf[:COMPLETOR]
34+
workspace = IRB::WorkSpace.new(Object.new)
35+
@context = IRB::Context.new(nil, workspace, TestInputMethod.new)
36+
IRB.conf[:COMPLETOR] = nil
37+
expected_default_completor = RUBY_VERSION >= '3.4' ? 'IRB::TypeCompletor' : 'IRB::RegexpCompletor'
38+
assert_equal expected_default_completor, @context.send(:build_completor).class.name
39+
IRB.conf[:COMPLETOR] = :type
40+
assert_equal 'IRB::TypeCompletor', @context.send(:build_completor).class.name
41+
ensure
42+
$VERBOSE = verbose
43+
IRB.conf[:COMPLETOR] = original_completor
44+
end
45+
3046
def assert_completion(preposing, target, binding: empty_binding, include: nil, exclude: nil)
3147
raise ArgumentError if include.nil? && exclude.nil?
3248
candidates = @completor.completion_candidates(preposing, target, '', bind: binding)

0 commit comments

Comments
 (0)