Skip to content

Support go to definition on method declaration names - #4248

Open
mperice wants to merge 1 commit into
Shopify:mainfrom
mperice:definition-on-method-name
Open

mperice wants to merge 1 commit into
Shopify:mainfrom
mperice:definition-on-method-name

Conversation

@mperice

@mperice mperice commented Oct 22, 2026

Copy link
Copy Markdown

Motivation

Go to definition on the name of a method declaration (def foo) returns nothing. On a class or module name (class Foo) it returns the declaration itself, which editors rely on: when the only definition is the one under the cursor, VS Code runs its alternative command, Go to References by default. So Cmd/Ctrl+click on a class name shows its usages, but on a method name it shows "No definition found", and finding a method's usages from its declaration needs a separate shortcut.

image

Implementation

  • Requests::Definition accepts Prism::DefNode as a target, but only when the cursor is on the method's name (name_loc). The def keyword, parameters and body still return nothing, handled in position_outside_target? like the other targets that use name_loc.
  • Listeners::Definition#on_def_node_enter returns a LocationLink to the method itself, with target_range covering the whole declaration and target_selection_range covering the name. It is skipped on typed: true or higher, as for method calls on self, because Sorbet handles method definitions there.

One thing to watch: with DefNode among the located node types, a cursor inside a method body that previously matched no target now locates the enclosing DefNode first. position_outside_target? then discards it, so those positions still return nothing. A test covers this.

Automated Tests

Added four tests to test/requests/definition_expectations_test.rb:

  • Instance method name.
  • Singleton method name (def self.bar).
  • Positions inside a declaration but outside its name: the keyword, a parameter and the body.
  • typed: true files, where nothing is returned.

The first two fail without the change. RuboCop and srb tc pass.

Manual Tests

  1. Run this branch of the Ruby LSP in VS Code and open a Ruby file with a method declaration.
  2. Cmd/Ctrl+click the method's name on its def line, or put the cursor on it and press F12.
  3. VS Code opens the references peek for the method. Before this change it showed "No definition found".
  4. For comparison, Cmd/Ctrl+click a class name in class Foo, which already behaves this way.

Go to definition on the name of a method declaration returned nothing. Class
and module names already resolve to their own declaration, which lets editors
switch to their alternative action, such as VS Code showing references.
Methods now behave the same way, except on typed true or higher, where Sorbet
handles method definitions.
@mperice
mperice requested a review from a team as a code owner October 22, 2026 15:47
@mperice

mperice commented Oct 23, 2026

Copy link
Copy Markdown
Author

I have signed the CLA!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant