Class: RubyLsp::RailsPartial::Definition

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

Overview

When the cursor is on the partial name string of a render call, provides go-to-definition to the matching partial file. When multiple formats match, returns all Locations and lets ruby-lsp present the candidate picker.

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, uri, node_context, resolver, dispatcher) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(response_builder, uri, node_context, resolver, dispatcher)
  @response_builder = response_builder
  @uri = uri
  @node_context = node_context
  @resolver = resolver

  dispatcher.register(self, :on_string_node_enter)
end

Instance Method Details

#on_string_node_enter(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/definition.rb', line 20

def on_string_node_enter(node)
  return unless render_partial_argument?(node)

  reference = node.unescaped
  return if reference.empty?

  # Highlight range of the origin. Pointing at the whole partial name (inside the quotes)
  # makes the entire string a link even for names containing `/` like `admin/areas/form`.
  origin_range = range_from_location(node.content_loc || node.location)

  # The partial body is not parsed, so a leading zero-width range is reused as the target for
  # all candidates. target_selection_range must be contained within target_range, so both are
  # made identical.
  target_range = Interface::Range.new(
    start: Interface::Position.new(line: 0, character: 0),
    end: Interface::Position.new(line: 0, character: 0)
  )

  @resolver.resolve(reference, @uri.to_s).each do |path|
    @response_builder << Interface::LocationLink.new(
      target_uri: URI::Generic.from_path(path:).to_s,
      target_range:,
      target_selection_range: target_range,
      origin_selection_range: origin_range
    )
  end
end