Class: RubyLsp::RailsRoutes::Definition

Inherits:
Object
  • Object
show all
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rails_routes/definition.rb

Overview

When the cursor is on a *_path / *_url route helper call, asks the rails runner process to resolve the named route to its controller action, and adds that action’s source location as a definition candidate. ruby-lsp-rails’ own Definition listener still contributes the routes.rb declaration to the same response builder, so both candidates coexist.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Definition.



12
13
14
15
16
17
18
19
# File 'lib/ruby_lsp/ruby_lsp_rails_routes/definition.rb', line 12

def initialize(client, server_addon_name, response_builder, node_context, dispatcher)
  @client = client
  @server_addon_name = server_addon_name
  @response_builder = response_builder
  @node_context = node_context

  dispatcher.register(self, :on_call_node_enter)
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

Mirrors ruby-lsp-rails’ own detection: self (or implicit) receiver, message ends with _path / _url.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_lsp/ruby_lsp_rails_routes/definition.rb', line 23

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

  message = node.message
  return unless message
  return unless message.end_with?("_path") || message.end_with?("_url")

  result = @client.delegate_request(
    server_addon_name: @server_addon_name,
    request_name: "action_location",
    name: message
  )
  return unless result && result[:location]

  @response_builder << Util.location_from_s(result[:location])
end