Skip to content

Commit 678a6ed

Browse files
committed
Switchable completors: RegexpCompletor and TypeCompletion::Completor
1 parent c1566ce commit 678a6ed

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

lib/irb.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@
140140
#
141141
# IRB.conf[:USE_AUTOCOMPLETE] = false
142142
#
143+
# To enable enhanced completion using type information, add the following to your +.irbrc+:
144+
#
145+
# IRB.conf[:COMPLETOR] = :type
146+
#
143147
# === History
144148
#
145149
# By default, irb will store the last 1000 commands you used in

lib/irb/context.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ def initialize(irb, workspace = nil, input_method = nil)
8686
when nil
8787
if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
8888
# Both of multiline mode and singleline mode aren't specified.
89-
@io = RelineInputMethod.new
89+
@io = RelineInputMethod.new(build_completor)
9090
else
9191
@io = nil
9292
end
9393
when false
9494
@io = nil
9595
when true
96-
@io = RelineInputMethod.new
96+
@io = RelineInputMethod.new(build_completor)
9797
end
9898
unless @io
9999
case use_singleline?
@@ -149,6 +149,21 @@ def initialize(irb, workspace = nil, input_method = nil)
149149
@command_aliases = IRB.conf[:COMMAND_ALIASES]
150150
end
151151

152+
private def build_completor
153+
# Valid values are :regexp and :type
154+
if IRB.conf[:COMPLETOR] == :type && RUBY_VERSION >= '3.0.0'
155+
begin
156+
require 'prism'
157+
require 'irb/type_completion/completor'
158+
TypeCompletion::Types.preload_in_thread
159+
return TypeCompletion::Completor.new
160+
rescue LoadError
161+
end
162+
end
163+
# Fallback to RegexpCompletor
164+
RegexpCompletor.new
165+
end
166+
152167
def save_history=(val)
153168
IRB.conf[:SAVE_HISTORY] = val
154169
end

lib/irb/init.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def IRB.init_config(ap_path)
4040
@CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod)
4141
@CONF[:USE_COLORIZE] = (nc = ENV['NO_COLOR']).nil? || nc.empty?
4242
@CONF[:USE_AUTOCOMPLETE] = ENV.fetch("IRB_USE_AUTOCOMPLETE", "true") != "false"
43+
@CONF[:COMPLETOR] = :regexp
4344
@CONF[:INSPECT_MODE] = true
4445
@CONF[:USE_TRACER] = false
4546
@CONF[:USE_LOADER] = false

lib/irb/input-method.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ class RelineInputMethod < StdioInputMethod
230230
HISTORY = Reline::HISTORY
231231
include HistorySavingAbility
232232
# Creates a new input method object using Reline
233-
def initialize
233+
def initialize(completor)
234234
IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false)
235235

236-
super
236+
super()
237237

238238
@eof = false
239-
@completor = RegexpCompletor.new
239+
@completor = completor
240240

241241
Reline.basic_word_break_characters = BASIC_WORD_BREAK_CHARACTERS
242242
Reline.completion_append_character = nil

test/irb/test_input_method.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RelineInputMethodTest < InputMethodTest
2424
def test_initialization
2525
Reline.completion_proc = nil
2626
Reline.dig_perfect_match_proc = nil
27-
IRB::RelineInputMethod.new
27+
IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
2828

2929
assert_nil Reline.completion_append_character
3030
assert_equal '', Reline.completer_quote_characters
@@ -40,7 +40,7 @@ def test_initialization_without_use_autocomplete
4040

4141
IRB.conf[:USE_AUTOCOMPLETE] = false
4242

43-
IRB::RelineInputMethod.new
43+
IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
4444

4545
refute Reline.autocompletion
4646
assert_equal empty_proc, Reline.dialog_proc(:show_doc).dialog_proc
@@ -55,7 +55,7 @@ def test_initialization_with_use_autocomplete
5555

5656
IRB.conf[:USE_AUTOCOMPLETE] = true
5757

58-
IRB::RelineInputMethod.new
58+
IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
5959

6060
assert Reline.autocompletion
6161
assert_not_equal empty_proc, Reline.dialog_proc(:show_doc).dialog_proc
@@ -71,7 +71,7 @@ def test_initialization_with_use_autocomplete_but_without_rdoc
7171
IRB.conf[:USE_AUTOCOMPLETE] = true
7272

7373
without_rdoc do
74-
IRB::RelineInputMethod.new
74+
IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
7575
end
7676

7777
assert Reline.autocompletion
@@ -89,7 +89,7 @@ def setup
8989
end
9090

9191
def display_document(target, bind)
92-
input_method = IRB::RelineInputMethod.new
92+
input_method = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
9393
input_method.instance_variable_set(:@completion_params, [target, '', '', bind])
9494
input_method.display_document(target, driver: @driver)
9595
end

0 commit comments

Comments
 (0)