Add preferPrimaryDefinition setting for go to definition - #4169
Add preferPrimaryDefinition setting for go to definition#4169sucicfilip wants to merge 1 commit into
Conversation
489faf2 to
0c1ebd6
Compare
0c1ebd6 to
4da6df0
Compare
|
Thank you for the proposal. While, I understand the point you make in the description and am familiar with Rails'/Zeitwerk's convention, from Ruby's perspective there's no distinction between the different definitions and the file name is irrelevant. Additionally, the current behaviour is also consistent with language servers for other languages. For someone working on multiple Ruby projects, say a few gems and some Rails apps, forgetting that the setting is enabled or working on a workspace where it's configured in It also creates a discrepancy between the features. If we omit other definitions in If it was possible to sort the results and put the matching file name first, that'd be a nicer solution, but the specification doesn't allow for that since you can have multiple language servers connected at the same time and editors wouldn't know how to order the results between them. |
|
Ah, I understand. Thank you for the clarification. I tried out Rubymine and got used to this, but ultimately came back to Zed + Ruby LSP😄 |
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