Skip to content

Commit 18d152b

Browse files
Fix UTF-16 autocompletion (#1129)
fixes #52
1 parent e45bb6b commit 18d152b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/irb/completion.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def retrieve_completion_data(input, bind:, doc_namespace:)
275275
nil
276276
else
277277
sym = $1
278-
candidates = Symbol.all_symbols.collect do |s|
278+
candidates = Symbol.all_symbols.filter_map do |s|
279279
s.inspect
280280
rescue EncodingError
281281
# ignore
@@ -453,6 +453,13 @@ def retrieve_completion_data(input, bind:, doc_namespace:)
453453
else
454454
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s}
455455
candidates |= RubyLex::RESERVED_WORDS.map(&:to_s)
456+
457+
target_encoding = Encoding.default_external
458+
candidates = candidates.compact.filter_map do |candidate|
459+
candidate.encoding == target_encoding ? candidate : candidate.encode(target_encoding)
460+
rescue EncodingError
461+
nil
462+
end
456463
candidates.grep(/^#{Regexp.quote(input)}/).sort
457464
end
458465
end

test/irb/test_completion.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_complete_require_with_pathname_in_load_path
110110
FileUtils.remove_entry(temp_dir) if temp_dir
111111
end
112112

113-
def test_complete_require_with_string_convertable_in_load_path
113+
def test_complete_require_with_string_convertible_in_load_path
114114
temp_dir = Dir.mktmpdir
115115
File.write(File.join(temp_dir, "foo.rb"), "test")
116116
object = Object.new
@@ -343,5 +343,24 @@ def test_regexp_completor_handles_encoding_errors_gracefully
343343
Encoding.default_external = original_encoding
344344
end
345345
end
346+
347+
def test_utf16_method_name_does_not_crash
348+
if RUBY_ENGINE == 'truffleruby'
349+
omit "TruffleRuby does not support UTF-16 methods."
350+
end
351+
# Reproduces issue #52: https://github.com/ruby/irb/issues/52
352+
method_name = "test_utf16_method".encode(Encoding::UTF_16)
353+
test_obj = Object.new
354+
test_obj.define_singleton_method(method_name) {}
355+
test_bind = test_obj.instance_eval { binding }
356+
357+
completor = IRB::RegexpCompletor.new
358+
result = nil
359+
assert_nothing_raised do
360+
result = completor.completion_candidates('', 'test', '', bind: test_bind)
361+
end
362+
363+
assert_include result, "test_utf16_method"
364+
end
346365
end
347366
end

0 commit comments

Comments
 (0)