From 656e424475b00e050c486affb6cec13822d1b0ed Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 20 Jun 2024 14:23:09 -0400 Subject: [PATCH] Address PR feedback --- DESIGN_AND_ROADMAP.md | 2 +- lib/ruby_lsp/listeners/completion.rb | 5 +++-- lib/ruby_lsp/listeners/hover.rb | 5 ++--- lib/ruby_lsp/listeners/signature_help.rb | 5 ++--- lib/ruby_lsp/requests/completion_resolve.rb | 9 +++++---- lib/ruby_lsp/utils.rb | 1 + test/requests/completion_resolve_test.rb | 4 ++-- test/requests/hover_expectations_test.rb | 2 +- test/requests/signature_help_test.rb | 2 +- 9 files changed, 18 insertions(+), 17 deletions(-) diff --git a/DESIGN_AND_ROADMAP.md b/DESIGN_AND_ROADMAP.md index b4bc4e3eb3..4e6d36630c 100644 --- a/DESIGN_AND_ROADMAP.md +++ b/DESIGN_AND_ROADMAP.md @@ -221,7 +221,7 @@ Interested in contributing? Check out the issues tagged with [help-wanted] or [g ## Guessed types -Guessed types is an experimental features where the Ruby LSP attempts to identify the type of a receiver based on its +Guessed types is an experimental feature where the Ruby LSP attempts to identify the type of a receiver based on its identifier name. For example: ```ruby diff --git a/lib/ruby_lsp/listeners/completion.rb b/lib/ruby_lsp/listeners/completion.rb index 46374dbe04..47e4dab6d4 100644 --- a/lib/ruby_lsp/listeners/completion.rb +++ b/lib/ruby_lsp/listeners/completion.rb @@ -295,10 +295,11 @@ def complete_methods(node, name) ) end - guessed_type = type.is_a?(TypeInferrer::GuessedType) + guessed_type = type.name @index.method_completion_candidates(method_name, type.name).each do |entry| entry_name = entry.name + owner_name = entry.owner&.name label_details = Interface::CompletionItemLabelDetails.new( description: entry.file_name, @@ -311,7 +312,7 @@ def complete_methods(node, name) text_edit: Interface::TextEdit.new(range: range, new_text: entry_name), kind: Constant::CompletionItemKind::METHOD, data: { - owner_name: entry.owner&.name, + owner_name: owner_name, guessed_type: guessed_type, }, ) diff --git a/lib/ruby_lsp/listeners/hover.rb b/lib/ruby_lsp/listeners/hover.rb index 56b6c2fe1f..d51745cada 100644 --- a/lib/ruby_lsp/listeners/hover.rb +++ b/lib/ruby_lsp/listeners/hover.rb @@ -115,9 +115,8 @@ def on_call_node_enter(node) title = +"#{message}#{T.must(methods.first).decorated_parameters}" if type.is_a?(TypeInferrer::GuessedType) - title << " | guessed receiver: #{type.name}" - link = "https://github.com/Shopify/ruby-lsp/blob/main/DESIGN_AND_ROADMAP.md#guessed-types" - @response_builder.push("[Learn more about guessed types](#{link})\n", category: :links) + title << "\n\nGuessed receiver: #{type.name}" + @response_builder.push("[Learn more about guessed types](#{GUESSED_TYPES_URL})\n", category: :links) end categorized_markdown_from_index_entries(title, methods).each do |category, content| diff --git a/lib/ruby_lsp/listeners/signature_help.rb b/lib/ruby_lsp/listeners/signature_help.rb index e2538f9797..7c410ed7dc 100644 --- a/lib/ruby_lsp/listeners/signature_help.rb +++ b/lib/ruby_lsp/listeners/signature_help.rb @@ -64,9 +64,8 @@ def on_call_node_enter(node) title = +"" extra_links = if type.is_a?(TypeInferrer::GuessedType) - title << "guessed receiver: #{type.name}" - link = "https://github.com/Shopify/ruby-lsp/blob/main/DESIGN_AND_ROADMAP.md#guessed-types" - "[Learn more about guessed types](#{link})" + title << "\n\nGuessed receiver: #{type.name}" + "[Learn more about guessed types](#{GUESSED_TYPES_URL})" end signature_help = Interface::SignatureHelp.new( diff --git a/lib/ruby_lsp/requests/completion_resolve.rb b/lib/ruby_lsp/requests/completion_resolve.rb index 282e38a39a..fdb248e6f1 100644 --- a/lib/ruby_lsp/requests/completion_resolve.rb +++ b/lib/ruby_lsp/requests/completion_resolve.rb @@ -63,10 +63,11 @@ def perform label = +"#{label}#{first_entry.decorated_parameters}" end - extra_links = if @item.dig(:data, :guessed_type) - label << " | guessed receiver: #{owner_name}" - link = "https://github.com/Shopify/ruby-lsp/blob/main/DESIGN_AND_ROADMAP.md#guessed-types" - "[Learn more about guessed types](#{link})" + guessed_type = @item.dig(:data, :guessed_type) + + extra_links = if guessed_type + label << "\n\nGuessed receiver: #{guessed_type}" + "[Learn more about guessed types](#{GUESSED_TYPES_URL})" end @item[:documentation] = Interface::MarkupContent.new( diff --git a/lib/ruby_lsp/utils.rb b/lib/ruby_lsp/utils.rb index 8fb6758f15..ecdeea048e 100644 --- a/lib/ruby_lsp/utils.rb +++ b/lib/ruby_lsp/utils.rb @@ -25,6 +25,7 @@ module RubyLsp end, String, ) + GUESSED_TYPES_URL = "https://github.com/Shopify/ruby-lsp/blob/main/DESIGN_AND_ROADMAP.md#guessed-types" # A notification to be sent to the client class Message diff --git a/test/requests/completion_resolve_test.rb b/test/requests/completion_resolve_test.rb index 6de982ab5e..bace349d2e 100644 --- a/test/requests/completion_resolve_test.rb +++ b/test/requests/completion_resolve_test.rb @@ -122,13 +122,13 @@ def foo(a, b, c) existing_item = { label: "foo", kind: RubyLsp::Constant::CompletionItemKind::METHOD, - data: { owner_name: "User", guessed_type: true }, + data: { owner_name: "User", guessed_type: "User" }, } server.process_message(id: 1, method: "completionItem/resolve", params: existing_item) result = server.pop_response.response - assert_match("guessed receiver: User", result[:documentation].value) + assert_match("Guessed receiver: User", result[:documentation].value) assert_match("Learn more about guessed types", result[:documentation].value) end end diff --git a/test/requests/hover_expectations_test.rb b/test/requests/hover_expectations_test.rb index 7f2b9370c4..8342b00a3e 100644 --- a/test/requests/hover_expectations_test.rb +++ b/test/requests/hover_expectations_test.rb @@ -574,7 +574,7 @@ def name; end ) contents = server.pop_response.response.contents.value - assert_match("guessed receiver: User", contents) + assert_match("Guessed receiver: User", contents) assert_match("Learn more about guessed types", contents) end end diff --git a/test/requests/signature_help_test.rb b/test/requests/signature_help_test.rb index b894e2c138..d8718f66d9 100644 --- a/test/requests/signature_help_test.rb +++ b/test/requests/signature_help_test.rb @@ -366,7 +366,7 @@ def subscribe!(news_letter) signature = result.signatures.first assert_equal("subscribe!(news_letter)", signature.label) - assert_match("guessed receiver: User", signature.documentation.value) + assert_match("Guessed receiver: User", signature.documentation.value) end end end