Repository navigation
Support finding references from constant definition sites - #4264
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
When invoking Find References on a constant definition (such as ConstantWriteNode, ConstantOrWriteNode, ConstantAndWriteNode, ConstantOperatorWriteNode, or ConstantTargetNode), Ruby LSP previously returned empty results because locate was only matching read nodes and constant path nodes. This commit expands RubyDocument.locate to locate constant write/target nodes, resolves their fully qualified name using index.resolve, and guards against invoking references when the cursor is in the value position of the assignment. Additionally, this removes a duplicate :on_constant_or_write_node_enter dispatcher listener in RubyIndexer::ReferenceFinder that caused or-write constants (CONST ||= 1) to be emitted twice. Signed-off-by: Reginald Alfret <reginaldalfret@gmail.com>
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
Currently, triggering
textDocument/references("Find All References" orShift+F12in VS Code) while the cursor is placed on a constant definition site (such asFOO = 1,BAR ||= 2, orA, B = 1, 2) yields "No references found".This happens because
RubyLsp::Requests::References#performdelegates node selection toRubyDocument#locate, which was configured to only accept:Prism::ConstantReadNodePrism::ConstantPathNodePrism::ConstantPathTargetNodeConsequently, constant writes (
ConstantWriteNode,ConstantOrWriteNode,ConstantAndWriteNode,ConstantOperatorWriteNode, andConstantTargetNode) were excluded during node location, making it impossible to search for references directly from the constant's definition or assignment site.Additionally, in
RubyIndexer::ReferenceFinder#initialize,:on_constant_or_write_node_enterwas accidentally registered twice with the dispatcher, causing conditional assignments likeCONST ||= 1to emit duplicated references.Implementation
Include Constant Write and Target Nodes in References Locate:
Prism::ConstantWriteNode,Prism::ConstantOrWriteNode,Prism::ConstantAndWriteNode,Prism::ConstantOperatorWriteNode, andPrism::ConstantTargetNodeto the node selection inRubyDocument#locate.Handle Definition Site Resolution:
resolve_const_reference_targetto look up index entries usingindex.resolvewithnode_context.nesting.target_node.name.to_sinto aRubyIndexer::ReferenceFinder::ConstTarget.Guard Against Value-Position Cursoring:
position_outside_target?ensuring that references are only triggered when the cursor actually covers the constant's name location (target.name_loc), returning empty locations if the cursor is resting inside the RHS expression/value.Deduplicate Dispatcher Registration in ReferenceFinder:
:on_constant_or_write_node_enterentry fromdispatcher.registerinRubyIndexer::ReferenceFinder.Automated Tests
test/requests/references_test.rb:test_finds_references_from_constant_write_definitiontest_finds_references_from_constant_or_write_definitiontest_finds_references_from_constant_operator_write_definitiontest_finds_references_from_constant_target_multi_writetest_returns_empty_when_cursor_is_on_constant_assignment_valuelib/ruby_indexer/test/reference_finder_test.rb:test_does_not_duplicate_references_for_or_write_constantsspoom srb tc.Manual Tests
BARinBAR = 123(line 2).Shift+F12/References: Find All References).123on line 2 and execute "Find All References".