Module: RubyLsp::PinkSpoon::LocalVariableHoverFallback

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

Overview

Same pattern as LocalVariableDefinitionFallback but for hover. Hover#perform returns nil when target is nil; here we intercept, do a second locate for LocalVariableReadNode, and return hover content.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.addon_instanceObject

Returns the value of attribute addon_instance.



65
66
67
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 65

def addon_instance
  @addon_instance
end

Instance Method Details

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 68

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 = LocalVariableHoverFallback.addon_instance
  return unless addon

  @_lv_hover_builder = RubyLsp::ResponseBuilders::Hover.new
  @_lv_hover_dispatcher = Prism::Dispatcher.new
  addon.create_hover_listener(@_lv_hover_builder, lv_context, @_lv_hover_dispatcher)
  @_lv_hover_target = lv_context.node
end

#performObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_lsp/pink_spoon/addon.rb', line 92

def perform
  result = super
  return result if result

  return nil unless @_lv_hover_target && @_lv_hover_dispatcher

  @_lv_hover_dispatcher.dispatch_once(@_lv_hover_target)
  return nil if @_lv_hover_builder.empty?

  RubyLsp::Interface::Hover.new(
    contents: RubyLsp::Interface::MarkupContent.new(
      kind: "markdown",
      value: @_lv_hover_builder.response,
    ),
  )
end