Find references from constant definitions - #4218
Draft
SlavaEremenko wants to merge 2 commits into
Draft
SlavaEremenko wants to merge 2 commits into
SlavaEremenko wants to merge 2 commits into
Conversation
`ReferenceFinder` registered `on_constant_or_write_node_enter` with the dispatcher twice, so every `CONST ||= value` was collected as two identical references.
Invoking find references on the name in `CONST = value` returned nothing, because the request only located constant reads and paths. `ReferenceFinder` already collects constant writes and multi-write targets as references, so the request now accepts the same node types as starting points and resolves them through the current nesting.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Find references returns nothing when invoked on a constant's definition, for example with the cursor on
STATUSEShere:Invoking it on any usage (
STATUSES,Invoice::STATUSES) works and even lists the definition, so only the declaration itself is a dead end. Class and module names are unaffected becauseclass Fooholds aConstantReadNode.Implementation
The request only located
ConstantReadNode,ConstantPathNodeandConstantPathTargetNode. OnSTATUSES = ..., aConstantWriteNode,locatefell back to the program node and the request returned early.ReferenceFinderalready collectsConstantWriteNode,ConstantOrWriteNode,ConstantAndWriteNode,ConstantOperatorWriteNodeand multi-writeConstantTargetNodes, so the request now accepts the same nodes as starting points and resolvesnode.namethrough the current nesting. The resolve-to-ConstTargetstep is extracted so both constant branches share it.While testing the
||=case I foundReferenceFinderregisteredon_constant_or_write_node_enterwith the dispatcher twice, which returned everyCONST ||= valueas two identical locations no matter where the request started. That's fixed in its own commit.Two things I noticed but left out to keep this focused:
Foo::BAR = 1is also reported twice, because bothon_constant_path_write_node_enterandon_constant_path_node_enterfire for the write's target. Those handlers came with rename support, so I didn't want to change them without knowing the intent.Renamelocates the same three node types, so renaming from a constant's definition has the same gap.Happy to follow up on either.
Automated Tests
Added request tests for plain,
||=,+=and multi-write definitions, plus aReferenceFinderregression test for the duplicate or-write.bundle exec rake,bundle exec srb tcandbin/rubocoppass locally.Manual Tests
class Foo; BAR = 1; endto one file andFoo::BARto another.BARon its definition line and pick Find All References.Foo::BARare listed.