Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def retrieve_completion_data(input, bind:, doc_namespace:)
nil
else
sym = $1
candidates = Symbol.all_symbols.collect do |s|
candidates = Symbol.all_symbols.filter_map do |s|
s.inspect
rescue EncodingError
# ignore
Expand Down Expand Up @@ -453,6 +453,13 @@ def retrieve_completion_data(input, bind:, doc_namespace:)
else
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s}
candidates |= RubyLex::RESERVED_WORDS.map(&:to_s)

target_encoding = Encoding.default_external
candidates = candidates.compact.filter_map do |candidate|
candidate.encoding == target_encoding ? candidate : candidate.encode(target_encoding)
rescue EncodingError
nil
end
candidates.grep(/^#{Regexp.quote(input)}/).sort
end
end
Expand Down
21 changes: 20 additions & 1 deletion test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_complete_require_with_pathname_in_load_path
FileUtils.remove_entry(temp_dir) if temp_dir
end

def test_complete_require_with_string_convertable_in_load_path
def test_complete_require_with_string_convertible_in_load_path
temp_dir = Dir.mktmpdir
File.write(File.join(temp_dir, "foo.rb"), "test")
object = Object.new
Expand Down Expand Up @@ -343,5 +343,24 @@ def test_regexp_completor_handles_encoding_errors_gracefully
Encoding.default_external = original_encoding
end
end

def test_utf16_method_name_does_not_crash
if RUBY_ENGINE == 'truffleruby'
omit "TruffleRuby does not support UTF-16 methods."
end
# Reproduces issue #52: https://github.com/ruby/irb/issues/52
method_name = "test_utf16_method".encode(Encoding::UTF_16)
test_obj = Object.new
test_obj.define_singleton_method(method_name) {}
test_bind = test_obj.instance_eval { binding }

completor = IRB::RegexpCompletor.new
result = nil
assert_nothing_raised do
result = completor.completion_candidates('', 'test', '', bind: test_bind)
end

assert_include result, "test_utf16_method"
end
end
end