Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check target precision for call node in hover and definition #2870

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading