Skip to content

Commit 795b975

Browse files
committed
fix: don't crash on utf18 autocompletion
fixes #52
1 parent 3893f18 commit 795b975

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,21 @@ 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+
# Reproduces issue #52: https://github.com/ruby/irb/issues/52
349+
method_name = "test_utf16_method".encode(Encoding::UTF_16)
350+
test_obj = Object.new
351+
test_obj.define_singleton_method(method_name) {}
352+
test_bind = test_obj.instance_eval { binding }
353+
354+
completor = IRB::RegexpCompletor.new
355+
result = nil
356+
assert_nothing_raised do
357+
result = completor.completion_candidates('', 'test', '', bind: test_bind)
358+
end
359+
360+
assert_include result, "test_utf16_method"
361+
end
346362
end
347363
end

0 commit comments

Comments
 (0)