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

Discard method call target if position doesn't cover identifier #1981

Merged
merged 1 commit into from Apr 26, 2024

Conversation

vinistock
Copy link
Member

Motivation

With the current experience, clicking to go to the definition of a method (or hovering) over any part of the CallNode will always succeed. This is not what we want, since people should be able to go to definition or hover separately for method arguments.

It also leads to incorrect behaviour. For example:

[].each(&:foo)
#         ^ go to definition here is currently taking you to `each`, which is not correct

Implementation

We still need to locate the method calls based on the entire node, but if we're processing a method call that has no special handling (i.e.: not requires), then we need to ensure that the position being clicked is covered by method's identifier.

Otherwise, it means the user is clicking on the arguments node or on the receiver (neither of which should take you to the method declaration).

Automated Tests

Added tests demonstrating the behaviour.

@vinistock vinistock added bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes labels Apr 26, 2024
@vinistock vinistock self-assigned this Apr 26, 2024
@vinistock vinistock requested a review from a team as a code owner April 26, 2024 16:01
@vinistock vinistock requested review from andyw8 and st0012 April 26, 2024 16:01

# Checks if a given location covers the position requested
sig { params(location: T.nilable(Prism::Location), position: T::Hash[Symbol, T.untyped]).returns(T::Boolean) }
def covers_position?(location, position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like an API that Prism::Location can provide 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kddnewton how would you feel about that? It would mostly be for line + column information since when dealing offsets, checking if an index covers is quite trivial.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think there might be a couple of objects hiding here. The fact that a raw hash is being passed around here makes me really uncomfortable. I think the following objects would be useful instead:

Prism::Position::ByteOffset[:offset, :byte_length]
Prism::Position::LineByteColumn[:start_line, :start_byte_column, :end_line, :end_byte_column]
Prism::Position::LineCharacterColumn[:start_line, :start_character_column, :end_line, :end_character_column]
Prism::Position::LineCodeUnitColumn[:start_line, :start_code_unit_column, :end_line, :end_code_unit_column]

with APIs that create those various positions from a location and can compare against those positions for things like covers?. Unless we're explicit about which kind of column counts we're using, I wouldn't want to make an assumption (because people get burned by this too often with ripper).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, indeed it needs to account for all possible ways that you can refer to a location.

dispatcher,
typechecker_enabled,
)
if target
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) could this be an early return?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, I'm not a fan of early returns in the initialize method. If you feel strongly about it, I can change, but I wouldn't normally.

@paracycle
Copy link
Member

I would like to see how this behaves with clicking foo in the example below, though:

bar(a: foo, 42)

It would be smart to add a test case to make sure that we still jump to foo and not ignore it because it was in the arguments to bar.

@vinistock vinistock force-pushed the vs/make_method_call_targeting_more_precise branch from acb8ff0 to 3f55bbb Compare April 26, 2024 17:45
@vinistock
Copy link
Member Author

I would like to see how this behaves with clicking foo in the example below, though:

Added a test to ensure we don't regress, but that already worked from the implementation of locate_node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants