Class: RubyLsp::Rails::Definition

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rails/definition.rb

Overview

![Definition demo](../../definition.gif)

The [definition request](microsoft.github.io/language-server-protocol/specification#textDocument_definition) jumps to the definition of the symbol under the cursor.

Currently supported targets:

  • Callbacks

  • Named routes (e.g. ‘users_path`)

# Example

“‘ruby before_action :foo # <- Go to definition on this symbol will jump to the method if it is defined in the same class “`

Notes for named routes:

  • It is available only in Rails 7.1 or newer.

  • Route may be defined across multiple files, e.g. using ‘draw`, rather than in `routes.rb`.

  • Routes won’t be found if not defined for the Rails development environment.

  • If using ‘constraints`, the route can only be found if the constraints are met.

  • Changes to routes won’t be picked up until the server is restarted.

Instance Method Summary collapse

Constructor Details

#initialize(client, response_builder, node_context, index, dispatcher) ⇒ Definition

Returns a new instance of Definition.



43
44
45
46
47
48
49
50
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 43

def initialize(client, response_builder, node_context, index, dispatcher)
  @client = client
  @response_builder = response_builder
  @nesting = T.let(node_context.nesting, T::Array[String])
  @index = index

  dispatcher.register(self, :on_call_node_enter)
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 53

def on_call_node_enter(node)
  return unless self_receiver?(node)

  message = node.message

  return unless message

  if Support::Associations::ALL.include?(message)
    handle_association(node)
  elsif Support::Callbacks::ALL.include?(message)
    handle_callback(node)
  elsif message.end_with?("_path") || message.end_with?("_url")
    handle_route(node)
  end
end