Skip to content

Add support for local variable references #3558

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nunosilva800
Copy link

@nunosilva800 nunosilva800 commented May 29, 2025

Motivation

Closes #2642
Closes #3111

Implementation

  1. Made the RubyIndexer::ReferenceFinder register on_local_variable_**_enter with the Prism::Dispatcher, and collect references to it while doing so.
  2. RubyLsp::Requests::References now expects Prism::LocalVariable**Node and when the target is a RubyIndexer::ReferenceFinder::LocalVariableTarget, it collects references in the current ruby document and only under the parent of the target. This should limit the scope.
  3. RubyLsp::Requests::Rename and RubyLsp::Requests::PrepareRename also expects Prism::LocalVariable**Node and follows same logic as the Reference request resolver to find and rename the local var.

Notes:

  1. the usage of Prism::RequiredParameterNode to understand when a local var might be defined in a block argument - there might be better ways of doing this (as well as considering optional parameters?)

  2. since the search for local var references is limited to the parent of target (what is under the cursor), when the var is inside a block, any reference to the var outside the block is not found. This is something that perhaps can be improved.

Automated Tests

Unit tests were added.

Manual Tests

Cannot use it manually... after following https://shopify.github.io/ruby-lsp/contributing.html#live-debugging I still see:

image

Which means something is missing from this PR 👀

Copy link

graphite-app bot commented May 29, 2025

How to use the Graphite Merge Queue

Add the label graphite-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@nunosilva800 nunosilva800 added enhancement New feature or request vscode This pull request should be included in the VS Code extension's release notes server This pull request should be included in the server gem's release notes labels May 29, 2025
@nunosilva800 nunosilva800 self-assigned this Jun 2, 2025
@nunosilva800 nunosilva800 marked this pull request as ready for review June 2, 2025 08:46
@nunosilva800 nunosilva800 requested a review from a team as a code owner June 2, 2025 08:46
@nunosilva800
Copy link
Author

@vinistock any clues on why I cannot test this with live debugging?
Also, what other examples of local var references/rename can you think of that should be tested? The current implementation is quite limited, but having other examples might guide me towards a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes vscode This pull request should be included in the VS Code extension's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement find references and rename for locals Add references support for local variables
1 participant