8000 Indicate multiple signatures for Hover and Completion Resolve (#2415) · Shopify/ruby-lsp@b6ba7da · GitHub
[go: up one dir, main page]

Skip to content

Commit b6ba7da

Browse files
authored
Indicate multiple signatures for Hover and Completion Resolve (#2415)
* Indicate multiple signatures for Hover and Completion Resolve * Extract common behaviour
1 parent d92ea6e commit b6ba7da

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

lib/ruby_indexer/lib/ruby_indexer/entry.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,19 @@ def decorated_parameters
342342

343343
"(#{first_signature.format})"
344344
end
345+
346+
sig { returns(String) }
347+
def formatted_signatures
348+
overloads_count = signatures.size
349+
case overloads_count
350+
when 1
351+
""
352+
when 2
353+
"\n(+1 overload)"
354+
else
355+
"\n(+#{overloads_count - 1} overloads)"
356+
end
357+
end
345358
end
346359

347360
class Accessor < Member
@@ -542,6 +555,16 @@ def parameters
542555
def decorated_parameters
543556
@target.decorated_parameters
544557
end
558+
559+
sig { returns(String) }
560+
def formatted_signatures
561+
@target.formatted_signatures
562+
end
563+
564+
sig { returns(T::Array[Signature]) }
565+
def signatures
566+
@target.signatures
567+
end
545568
end
546569

547570
# Ruby doesn't support method overloading, so a method will have only one signature.

lib/ruby_lsp/listeners/hover.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ def handle_method_hover(message, inherited_only: false)
174174
methods = @index.resolve_method(message, type.name, inherited_only: inherited_only)
175175
return unless methods
176176

177-
title = "#{message}#{T.must(methods.first).decorated_parameters}"
177+
first_method = T.must(methods.first)
178+
179+
title = "#{message}#{first_method.decorated_parameters}"
180+
title << first_method.formatted_signatures
178181

179182
if type.is_a?(TypeInferrer::GuessedType)
180183
title << "\n\nGuessed receiver: #{type.name}"

lib/ruby_lsp/requests/completion_resolve.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def perform
6363

6464
if first_entry.is_a?(RubyIndexer::Entry::Member)
6565
label = +"#{label}#{first_entry.decorated_parameters}"
66+
label << first_entry.formatted_signatures
6667
end
6768

6869
guessed_type = @item.dig(:data, :guessed_type)

test/requests/completion_resolve_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ def bar
108108
end
109109
end
110110

111+
def test_indicates_signature_count_in_label_details
112+
source = +<<~RUBY
113+
String.try_convert
114+
RUBY
115+
116+
with_server(source, stub_no_typechecker: true) do |server, _uri|
117+
index = server.instance_variable_get(:@global_state).index
118+
RubyIndexer::RBSIndexer.new(index).index_ruby_core
119+
existing_item = {
120+
label: "try_convert",
121+
kind: RubyLsp::Constant::CompletionItemKind::METHOD,
122+
data: { owner_name: "String::<Class:String>" },
123+
}
124+
125+
server.process_message(id: 1, method: "completionItem/resolve", params: existing_item)
126+
127+
result = server.pop_response.response
128+
assert_match("try_convert(object)", result[:documentation].value)
129+
assert_match("(+2 overloads)", result[:documentation].value)
130+
end
131+
end
132+
111133
def test_completion_documentation_for_guessed_types
112134
source = +<<~RUBY
113135
class User

test/requests/hover_expectations_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,26 @@ def baz
467467
end
468468
end
469469

470+
def test_hover_for_methods_shows_overload_count
471+
source = <<~RUBY
472+
String.try_convert
473+
RUBY
474+
475+
with_server(source) do |server, uri|
476+
index = server.instance_variable_get(:@global_state).index
477+
RubyIndexer::RBSIndexer.new(index).index_ruby_core
478+
server.process_message(
479+
id: 1,
480+
method: "textDocument/hover",
481+
params: { textDocument: { uri: uri }, position: { character: 8, line: 0 } },
482+
)
483+
484+
contents = server.pop_response.response.contents.value
485+
assert_match("try_convert(object)", contents)
486+
assert_match("(+2 overloads)", contents)
487+
end
488+
end
489+
470490
def test_hover_for_singleton_methods
471491
source = <<~RUBY
472492
class Foo

0 commit comments

Comments
 (0)
0