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

Use location links for namespace and method definition response #2206

Merged
merged 4 commits into from
Jun 20, 2024

Conversation

vinistock
Copy link
Member

Motivation

I noticed that we were unable to go to definition for constants that were referenced inside their own declaration. E.g.:

class Foo
  Foo
end

At first, I thought something was wrong with constant resolution, but it was not the case. We were always returning the class' complete location back to the editor (including the body part). This makes the editor consider any attempts to go to definition of the same symbol as a references rather than a definition request.

In reality, we only want the reference request to be invoked if someone CMD clicks the Foo declaration, but not a constant reference like the one in line 1.

Implementation

The spec provides the location link type to handle situations like these.

Location links allow us to return two locations:

  1. targetRange: this is the complete range including the body. This is used to show a preview of the location that you are about to jump to
  2. selectionTargetRange: this is the specific location that you will jump to, which is supposed to be only the identifier of the symbol

By using location links and passing only the identifier locations for namespaces and methods, then the editor does the right thing and allows us to correctly jump to definition for constant references inside their own declarations.

  1. The first commit makes the return type of resolve more specific to avoid any typing issues
  2. The second commit starts indexing name locations only on the entries in which it is relevant to do so. Note that RBS does not provide us with the two locations, so I'm falling back to assigning the same location to both - which should be fine since you cannot write a constant reference inside RBS anyway (you can only write declarations)
  3. Finally, we start using location links for class, module and method definition requests

Automated Tests

I adapted the existing tests, but because this specific scenario requires full interaction with the editor, I could not manage to get a test working properly.

Manual Tests

On main, when you cmd click a constant reference that exists inside its own declaration, the editor will show "no references found for Foo" (because we don't implement the references request yet).

On this branch, you will jump to the right declaration.

@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 Jun 18, 2024
@vinistock vinistock self-assigned this Jun 18, 2024
@vinistock vinistock requested a review from a team as a code owner June 18, 2024 19:00
@vinistock vinistock requested review from andyw8 and st0012 June 18, 2024 19:00
@vinistock vinistock force-pushed the vs/use_location_links branch 2 times, most recently from 58c694f to 4b2a6d6 Compare June 18, 2024 21:14
Copy link
Contributor

@Morriar Morriar left a comment

Choose a reason for hiding this comment

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

You can also replace the selection_range in TypeHierarchySupertypes: https://github.com/Shopify/ruby-lsp/blob/main/lib/ruby_lsp/requests/type_hierarchy_supertypes.rb#L85

@vinistock vinistock force-pushed the vs/use_location_links branch from 4b2a6d6 to 0a04efc Compare June 19, 2024 14:23
@vinistock
Copy link
Member Author

@Morriar done! Good catch.

@vinistock vinistock force-pushed the vs/use_location_links branch from 0a04efc to fdcbe05 Compare June 20, 2024 13:40
@vinistock vinistock added blocked This issue can't move forward until a blocker is resolved and removed blocked This issue can't move forward until a blocker is resolved labels Jun 20, 2024
@vinistock vinistock enabled auto-merge (squash) June 20, 2024 13:48
@vinistock vinistock merged commit 530ceca into main Jun 20, 2024
35 checks passed
@vinistock vinistock deleted the vs/use_location_links branch June 20, 2024 13:55
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.

2 participants