Skip to content

Commit

Permalink
Fix irb crash on {}. completion (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored Nov 18, 2023
1 parent d3b7940 commit 07e4d54
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,16 @@ def retrieve_completion_data(input, bind:, doc_namespace:)
end

when /^([^\}]*\})\.([^.]*)$/
# Proc or Hash
# Hash or Proc
receiver = $1
message = $2

if doc_namespace
["Proc.#{message}", "Hash.#{message}"]
["Hash.#{message}", "Proc.#{message}"]
else
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, proc_candidates | hash_candidates)
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, hash_candidates | proc_candidates)
end

when /^(:[^:.]+)$/
Expand Down
3 changes: 3 additions & 0 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ def show_doc_dialog_proc
return nil if result.nil? or pointer.nil? or pointer < 0

name = doc_namespace.call(result[pointer])
# Use first one because document dialog does not support multiple namespaces.
name = name.first if name.is_a?(Array)

show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']

options = {}
Expand Down
4 changes: 2 additions & 2 deletions test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_complete_array
def test_complete_hash_and_proc
# hash
assert_include(completion_candidates("{}.an", binding), "{}.any?")
assert_equal(["Proc.any?", "Hash.any?"], doc_namespace("{}.any?", binding))
assert_equal(["Hash.any?", "Proc.any?"], doc_namespace("{}.any?", binding))

# proc
assert_include(completion_candidates("{}.bin", binding), "{}.binding")
assert_equal(["Proc.binding", "Hash.binding"], doc_namespace("{}.binding", binding))
assert_equal(["Hash.binding", "Proc.binding"], doc_namespace("{}.binding", binding))
end

def test_complete_numeric
Expand Down
15 changes: 15 additions & 0 deletions test/irb/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ def test_symbol_with_backtick
EOC
end

def test_autocomplete_with_multiple_doc_namespaces
write_irbrc <<~'LINES'
puts 'start IRB'
LINES
start_terminal(4, 40, %W{ruby -I#{@pwd}/lib #{@pwd}/exe/irb}, startup_message: 'start IRB')
write("{}.__id_")
write("\C-i")
close
assert_screen(<<~EOC)
start IRB
irb(main):001> {}.__id__
}.__id__
EOC
end

def test_autocomplete_with_showdoc_in_gaps_on_narrow_screen_right
rdoc_dir = File.join(@tmpdir, 'rdoc')
system("bundle exec rdoc -r -o #{rdoc_dir}")
Expand Down

0 comments on commit 07e4d54

Please sign in to comment.