Module: RubyLsp::PinkSpoon::LocalVariableDefinitionFallback

Defined in:
lib/ruby_lsp/pink_spoon/addon.rb

Overview

Prepended to RubyLsp::Requests::Definition to handle local variable go-to-definition. Ruby-lsp excludes LocalVariableReadNode from its node_types list, so when the cursor is on a local variable (including when it is the receiver of a method call), ruby-lsp sets target = nil and calls no listeners. This patch does a second locate pass for that exact case and dispatches to pink-spoon’s DefinitionListener.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.addon_instanceObject

Returns the value of attribute addon_instance.



24
25
26
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 24

def addon_instance
  @addon_instance
end

Instance Method Details

#initialize(document, global_state, position, dispatcher, sorbet_level) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 27

def initialize(document, global_state, position, dispatcher, sorbet_level)
  super

  return if @target
  return unless document.is_a?(RubyLsp::RubyDocument)

  char_position, _ = document.find_index_by_position(position)
  lv_context = RubyLsp::RubyDocument.locate(
    document.ast,
    char_position,
    node_types: [Prism::LocalVariableReadNode],
    code_units_cache: document.code_units_cache,
  )
  return unless lv_context.node.is_a?(Prism::LocalVariableReadNode)

  addon = LocalVariableDefinitionFallback.addon_instance
  return unless addon

  @_lv_dispatcher = Prism::Dispatcher.new
  addon.create_definition_listener(@response_builder, document.uri, lv_context, @_lv_dispatcher)
  @_lv_target = lv_context.node
end

#performObject



50
51
52
53
54
55
56
57
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 50

def perform
  result = super
  if @_lv_target && @_lv_dispatcher && result.empty?
    @_lv_dispatcher.dispatch_once(@_lv_target)
    return @response_builder.response
  end
  result
end