Class: RubyLsp::Rails::Definition

Inherits:
Object
  • Object
show all
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 “`

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, graph, dispatcher) ⇒ Definition

: ( | RunnerClient, | RubyLsp::ResponseBuilders::CollectionResponseBuilder[(Interface::Location | Interface::LocationLink)], | NodeContext, | Rubydex::Graph, | Prism::Dispatcher | ) -> void



40
41
42
43
44
45
46
47
48
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 40

def initialize(client, response_builder, node_context, graph, dispatcher)
  @client = client
  @response_builder = response_builder
  @node_context = node_context
  @nesting = node_context.nesting #: Array[String]
  @graph = graph

  dispatcher.register(self, :on_call_node_enter, :on_symbol_node_enter, :on_string_node_enter)
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode node) -> void



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 61

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

  message = node.message

  return unless message

  if message.end_with?("_path") || message.end_with?("_url")
    handle_route(node)
  end
end

#on_string_node_enter(node) ⇒ Object

: (Prism::StringNode node) -> void



56
57
58
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 56

def on_string_node_enter(node)
  handle_possible_dsl(node)
end

#on_symbol_node_enter(node) ⇒ Object

: (Prism::SymbolNode node) -> void



51
52
53
# File 'lib/ruby_lsp/ruby_lsp_rails/definition.rb', line 51

def on_symbol_node_enter(node)
  handle_possible_dsl(node)
end