Skip to content

Commit

Permalink
Indicate multiple signatures for Hover and Completion Resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 7, 2024
1 parent 7c4abea commit 307d039
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ def parameters
def decorated_parameters
@target.decorated_parameters
end

sig { returns(T::Array[Signature]) }
def signatures
@target.signatures
end
end

# Ruby doesn't support method overloading, so a method will have only one signature.
Expand Down
10 changes: 9 additions & 1 deletion lib/ruby_lsp/listeners/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ def handle_method_hover(message, inherited_only: false)
methods = @index.resolve_method(message, type.name, inherited_only: inherited_only)
return unless methods

title = "#{message}#{T.must(methods.first).decorated_parameters}"
first_method = T.must(methods.first)

title = "#{message}#{first_method.decorated_parameters}"
overloads_count = first_method.signatures.size
if overloads_count == 2
title << "\n(+1 overload)"
elsif overloads_count > 2
title << "\n(+#{overloads_count - 1} overloads)"
end

if type.is_a?(TypeInferrer::GuessedType)
title << "\n\nGuessed receiver: #{type.name}"
Expand Down
7 changes: 7 additions & 0 deletions lib/ruby_lsp/requests/completion_resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def perform

if first_entry.is_a?(RubyIndexer::Entry::Member)
label = +"#{label}#{first_entry.decorated_parameters}"

overloads_count = first_entry.signatures.size
if overloads_count == 2
label << "\n(+1 overload)"
elsif overloads_count > 2
label << "\n(+#{overloads_count - 1} overloads)"
end
end

guessed_type = @item.dig(:data, :guessed_type)
Expand Down
22 changes: 22 additions & 0 deletions test/requests/completion_resolve_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ def bar
end
end

def test_indicates_signature_count_in_label_details
source = +<<~RUBY
String.try_convert
RUBY

with_server(source, stub_no_typechecker: true) do |server, _uri|
index = server.instance_variable_get(:@global_state).index
RubyIndexer::RBSIndexer.new(index).index_ruby_core
existing_item = {
label: "try_convert",
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
data: { owner_name: "String::<Class:String>" },
}

server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)

result = server.pop_response.response
assert_match("try_convert(object)", result[:documentation].value)
assert_match("(+2 overloads)", result[:documentation].value)
end
end

def test_completion_documentation_for_guessed_types
source = +<<~RUBY
class User
Expand Down
20 changes: 20 additions & 0 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,26 @@ def baz
end
end

def test_hover_for_methods_shows_overload_count
source = <<~RUBY
String.try_convert
RUBY

with_server(source) do |server, uri|
index = server.instance_variable_get(:@global_state).index
RubyIndexer::RBSIndexer.new(index).index_ruby_core
server.process_message(
id: 1,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: { character: 8, line: 0 } },
)

contents = server.pop_response.response.contents.value
assert_match("try_convert(object)", contents)
assert_match("(+2 overloads)", contents)
end
end

def test_hover_for_singleton_methods
source = <<~RUBY
class Foo
Expand Down

0 comments on commit 307d039

Please sign in to comment.