Skip to content

Go To Definition Lookup for String Literal #3892

@mario-amazing

Description

@mario-amazing

I have checked that this feature is not already implemented

  • This feature does not exist

Use case

In Ruby, class names are often represented as strings in mappings and configurations:

WORKERS_MAPPING = {
  ....Worker => 'Another::Worker',
  #.  ...
}

'User', "Go to Definition" works on direct class references (User) but not on string literals ('User'). When invoking "Go to Definition" on 'User', it should navigate to the User class definition.

Image Image

Description

Ruby LSP should recognize string literals containing class/module names and allow navigating to their definitions. When a user invokes "Go to Definition" on a string literal:

  1. LSP checks if the string content is a valid class/module name
  2. Searches for that class definition in the workspace
  3. If found, navigates to the definition
  4. If not found, shows standard string behavior

Implementation

In lib/ruby_lsp/requests/definition.rb:

  1. On definition request, check the AST node type
  2. If it's a string literal, extract its value
  3. Check if the value is a valid constant name (e.g., User, Communication::User)
  4. If yes, resolve the class/module using Object.const_get or Ruby LSP's index
  5. Return the position of the found class definition

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions