Add support for local variable references #3558
Open
+390
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Closes #2642
Closes #3111
Implementation
on_local_variable_**_enter
with thePrism::Dispatcher
, and collect references to it while doing so.RubyLsp::Requests::References
now expectsPrism::LocalVariable**Node
and when the target is aRubyIndexer::ReferenceFinder::LocalVariableTarget
, it collects references in the current ruby document and only under the parent of the target. This should limit the scope.RubyLsp::Requests::Rename
andRubyLsp::Requests::PrepareRename
also expectsPrism::LocalVariable**Node
and follows same logic as the Reference request resolver to find and rename the local var.Notes:
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?)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:
Which means something is missing from this PR 👀