From 67494f4c7acba930f157cde2d607f0838a17bd09 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:09:58 -0400 Subject: [PATCH] Document highlight shouldn't include the block for a method --- lib/ruby_lsp/listeners/document_highlight.rb | 6 +- .../multiple_invocations.exp.json | 75 +++++++++++++++++++ test/fixtures/multiple_invocations.rb | 10 +++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/expectations/document_highlight/multiple_invocations.exp.json create mode 100644 test/fixtures/multiple_invocations.rb diff --git a/lib/ruby_lsp/listeners/document_highlight.rb b/lib/ruby_lsp/listeners/document_highlight.rb index 89ddb59e8..3757bb743 100644 --- a/lib/ruby_lsp/listeners/document_highlight.rb +++ b/lib/ruby_lsp/listeners/document_highlight.rb @@ -180,7 +180,11 @@ def initialize(response_builder, target, parent, dispatcher) def on_call_node_enter(node) return unless matches?(node, [Prism::CallNode, Prism::DefNode]) - add_highlight(Constant::DocumentHighlightKind::READ, node.location) + loc = node.message_loc + # if we have `foo.` it's a call node but there is no message yet. + return unless loc + + add_highlight(Constant::DocumentHighlightKind::READ, loc) end sig { params(node: Prism::DefNode).void } diff --git a/test/expectations/document_highlight/multiple_invocations.exp.json b/test/expectations/document_highlight/multiple_invocations.exp.json new file mode 100644 index 000000000..e7fc3793d --- /dev/null +++ b/test/expectations/document_highlight/multiple_invocations.exp.json @@ -0,0 +1,75 @@ +{ + "result": [ + { + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 3 + } + }, + "kind": 2 + }, + { + "range": { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 2, + "character": 3 + } + }, + "kind": 2 + }, + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 4, + "character": 3 + } + }, + "kind": 2 + }, + { + "range": { + "start": { + "line": 7, + "character": 0 + }, + "end": { + "line": 7, + "character": 3 + } + }, + "kind": 2 + }, + { + "range": { + "start": { + "line": 9, + "character": 0 + }, + "end": { + "line": 9, + "character": 3 + } + }, + "kind": 2 + } + ], + "params": [ + { + "line": 0, + "character": 2 + } + ] +} diff --git a/test/fixtures/multiple_invocations.rb b/test/fixtures/multiple_invocations.rb new file mode 100644 index 000000000..97173d322 --- /dev/null +++ b/test/fixtures/multiple_invocations.rb @@ -0,0 +1,10 @@ +foo() + +foo + +foo do +end + +foo { } + +foo.