From 9c823479ba3bb7e0fa4c6d9778fa0861dfb52672 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Mon, 21 Aug 2023 14:04:16 -0400 Subject: [PATCH] Migrate Hover Request to YARP --- lib/ruby_lsp/event_emitter.rb | 2 +- lib/ruby_lsp/requests/hover.rb | 31 +++++++------------ test/expectations/hover/controller.exp.json | 2 +- test/expectations/hover/job.exp.json | 2 +- test/expectations/hover/model.exp.json | 2 +- .../hover/model_action_text.exp.json | 2 +- .../model_active_record_constant.exp.json | 2 +- .../hover/model_active_storage.exp.json | 2 +- test/expectations/hover/routes.exp.json | 2 +- test/requests/hover_expectations_test.rb | 8 ++--- 10 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/ruby_lsp/event_emitter.rb b/lib/ruby_lsp/event_emitter.rb index 99c91b5b49..4c621383d4 100644 --- a/lib/ruby_lsp/event_emitter.rb +++ b/lib/ruby_lsp/event_emitter.rb @@ -40,7 +40,7 @@ def emit_for_target(node) when YARP::CallNode @listeners[:on_call]&.each { |l| T.unsafe(l).on_call(node) } when YARP::ConstantPathNode - @listeners[:on_constant_path_node]&.each { |l| T.unsafe(l).on_constant_path_node(node) } + @listeners[:on_constant_path]&.each { |l| T.unsafe(l).on_constant_path(node) } when YARP::StringNode @listeners[:on_string_node]&.each { |l| T.unsafe(l).on_string_node(node) } end diff --git a/lib/ruby_lsp/requests/hover.rb b/lib/ruby_lsp/requests/hover.rb index a7c2378ba4..2fc49f7298 100644 --- a/lib/ruby_lsp/requests/hover.rb +++ b/lib/ruby_lsp/requests/hover.rb @@ -25,11 +25,10 @@ class Hover < Listener ALLOWED_TARGETS = T.let( [ - SyntaxTree::Command, - SyntaxTree::CallNode, - SyntaxTree::ConstPathRef, + YARP::CallNode, + YARP::ConstantPathNode, ], - T::Array[T.class_of(SyntaxTree::Node)], + T::Array[T.class_of(YARP::Node)], ) sig { override.returns(ResponseType) } @@ -43,7 +42,7 @@ def initialize(emitter, message_queue) Extension.extensions.filter_map { |ext| ext.create_hover_listener(emitter, message_queue) }, ) @response = T.let(nil, ResponseType) - emitter.register(self, :on_command, :on_const_path_ref, :on_call) + emitter.register(self, :on_constant_path, :on_call) end # Merges responses from other hover listeners @@ -61,34 +60,28 @@ def merge_response!(other) self end - sig { params(node: SyntaxTree::Command).void } - def on_command(node) - message = node.message - @response = generate_rails_document_link_hover(message.value, message) - end - - sig { params(node: SyntaxTree::ConstPathRef).void } - def on_const_path_ref(node) - @response = generate_rails_document_link_hover(full_constant_name(node), node) + sig { params(node: YARP::ConstantPathNode).void } + def on_constant_path(node) + @response = generate_rails_document_link_hover(node.location.slice, node.location) end - sig { params(node: SyntaxTree::CallNode).void } + sig { params(node: YARP::CallNode).void } def on_call(node) message = node.message return if message.is_a?(Symbol) - @response = generate_rails_document_link_hover(message.value, message) + @response = generate_rails_document_link_hover(message, node.message_loc) end private - sig { params(name: String, node: SyntaxTree::Node).returns(T.nilable(Interface::Hover)) } - def generate_rails_document_link_hover(name, node) + sig { params(name: String, location: YARP::Location).returns(T.nilable(Interface::Hover)) } + def generate_rails_document_link_hover(name, location) urls = Support::RailsDocumentClient.generate_rails_document_urls(name) return if urls.empty? contents = Interface::MarkupContent.new(kind: "markdown", value: urls.join("\n\n")) - Interface::Hover.new(range: range_from_syntax_tree_node(node), contents: contents) + Interface::Hover.new(range: range_from_location(location), contents: contents) end end end diff --git a/test/expectations/hover/controller.exp.json b/test/expectations/hover/controller.exp.json index 9031a30326..56fc891f9b 100644 --- a/test/expectations/hover/controller.exp.json +++ b/test/expectations/hover/controller.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 15 + "character": 14 } } }, diff --git a/test/expectations/hover/job.exp.json b/test/expectations/hover/job.exp.json index ec431b3977..10fb82b9a3 100644 --- a/test/expectations/hover/job.exp.json +++ b/test/expectations/hover/job.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 10 + "character": 9 } } }, diff --git a/test/expectations/hover/model.exp.json b/test/expectations/hover/model.exp.json index 7c4d943809..c1ba3329ea 100644 --- a/test/expectations/hover/model.exp.json +++ b/test/expectations/hover/model.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 10 + "character": 9 } } }, diff --git a/test/expectations/hover/model_action_text.exp.json b/test/expectations/hover/model_action_text.exp.json index 648171a932..9238c8c849 100644 --- a/test/expectations/hover/model_action_text.exp.json +++ b/test/expectations/hover/model_action_text.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 15 + "character": 14 } } }, diff --git a/test/expectations/hover/model_active_record_constant.exp.json b/test/expectations/hover/model_active_record_constant.exp.json index 12b917dec4..91b4659fa4 100644 --- a/test/expectations/hover/model_active_record_constant.exp.json +++ b/test/expectations/hover/model_active_record_constant.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 0, - "character": 31 + "character": 30 } } }, diff --git a/test/expectations/hover/model_active_storage.exp.json b/test/expectations/hover/model_active_storage.exp.json index 980c58f174..438de30a6f 100644 --- a/test/expectations/hover/model_active_storage.exp.json +++ b/test/expectations/hover/model_active_storage.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 18 + "character": 17 } } }, diff --git a/test/expectations/hover/routes.exp.json b/test/expectations/hover/routes.exp.json index 8125fccced..1d211924f8 100644 --- a/test/expectations/hover/routes.exp.json +++ b/test/expectations/hover/routes.exp.json @@ -11,7 +11,7 @@ }, "end": { "line": 1, - "character": 6 + "character": 5 } } }, diff --git a/test/requests/hover_expectations_test.rb b/test/requests/hover_expectations_test.rb index 11c9d1b6e4..c74dfdc57a 100644 --- a/test/requests/hover_expectations_test.rb +++ b/test/requests/hover_expectations_test.rb @@ -99,16 +99,16 @@ def create_hover_listener(emitter, message_queue) def initialize(emitter, message_queue) super - emitter.register(self, :on_command) + emitter.register(self, :on_call) end - def on_command(node) + def on_call(node) T.bind(self, RubyLsp::Listener[T.untyped]) contents = RubyLsp::Interface::MarkupContent.new( kind: "markdown", - value: "Method from middleware: #{node.message.value}", + value: "Method from middleware: #{node.message}", ) - @response = RubyLsp::Interface::Hover.new(range: range_from_syntax_tree_node(node), contents: contents) + @response = RubyLsp::Interface::Hover.new(range: range_from_location(node.location), contents: contents) end end