Add preferPrimaryDefinition setting for go to definition#4169
Open
sucicfilip wants to merge 1 commit into
Open
Add preferPrimaryDefinition setting for go to definition#4169sucicfilip wants to merge 1 commit into
sucicfilip wants to merge 1 commit into
Conversation
sucicfilip
force-pushed
the
feature/prefer-primary-definition
branch
from
July 20, 2026 11:35
489faf2 to
0c1ebd6
Compare
sucicfilip
marked this pull request as ready for review
July 20, 2026 11:36
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
In apps that reopen classes across many files (packwerk monoliths, engines, etc.), go to definition on a constant always opens a picker listing every file where the class is reopened. Most of the time I just want the file that actually defines the class, and picking it out of the list gets tedious fast.
Implementation
Adds an opt-in
featuresConfiguration.definition.preferPrimaryDefinitionsetting, wired through the existingfeaturesConfigurationmechanism inGlobalState.When the setting is enabled and a constant resolves to multiple namespace entries, the definition listener keeps only the entries whose file path matches the fully qualified constant name following the Zeitwerk convention. For example, in this repository
RubyIndexeris reopened in over 20 files, but onlylib/ruby_indexer/ruby_indexer.rbmatches the convention, so that is the single definition returned. If no entry matches the convention, all entries are returned as before. Methods and plain constants are not affected. The filter only applies to classes and modules.Automated Tests
Added two tests to
DefinitionExpectationsTest: one asserting that only the convention-matching file is returned when the setting is on, and one asserting the fallback to all entries when no file matches.Manual Tests
"rubyLsp.bundleGemfile"or apath:entry in your project's Gemfile)"rubyLsp.featuresConfiguration": { "definition": { "preferPrimaryDefinition": true } }to your settings and restart the Ruby LSPRubyIndexerreference (it is reopened in over 20 files underlib/). You should land directly inlib/ruby_indexer/ruby_indexer.rbinstead of getting a picker