Skip to content

Commit

Permalink
Check target precision for call node in hover and definition
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Nov 18, 2024
1 parent 35ee5f5 commit 06fc55a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def position_outside_target?(position, target)
Prism::InstanceVariableWriteNode

!covers_position?(target.name_loc, position)
when Prism::CallNode
!covers_position?(target.message_loc, position)
else
false
end
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby_lsp/requests/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def position_outside_target?(position, target)
Prism::GlobalVariableOrWriteNode,
Prism::GlobalVariableWriteNode
!covers_position?(target.name_loc, position)
when Prism::CallNode
!covers_position?(target.message_loc, position)
else
false
end
Expand Down
34 changes: 34 additions & 0 deletions test/requests/definition_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,40 @@ def baz
end
end

def test_definition_call_node_precision
source = <<~RUBY
class Foo
def message
"hello!"
end
end
class Bar
def with_foo(foo)
@foo_message = foo.message
end
end
RUBY

with_server(source) do |server, uri|
# On the `foo` receiver, we should not show any results
server.process_message(
id: 1,
method: "textDocument/definition",
params: { textDocument: { uri: uri }, position: { character: 19, line: 8 } },
)
assert_empty(server.pop_response.response)

# On `message`, we should
server.process_message(
id: 2,
method: "textDocument/definition",
params: { textDocument: { uri: uri }, position: { character: 23, line: 8 } },
)
refute_empty(server.pop_response.response)
end
end

private

def create_definition_addon
Expand Down
34 changes: 34 additions & 0 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,40 @@ def foo
end
end

def test_hover_call_node_precision
source = <<~RUBY
class Foo
def message
"hello!"
end
end
class Bar
def with_foo(foo)
@foo_message = foo.message
end
end
RUBY

with_server(source) do |server, uri|
# On the `foo` receiver, we should not show any results
server.process_message(
id: 1,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: { character: 19, line: 8 } },
)
assert_nil(server.pop_response.response)

# On `message`, we should
server.process_message(
id: 2,
method: "textDocument/hover",
params: { textDocument: { uri: uri }, position: { character: 23, line: 8 } },
)
refute_nil(server.pop_response.response)
end
end

private

def create_hover_addon
Expand Down

0 comments on commit 06fc55a

Please sign in to comment.