Class: RubyLsp::Requests::Hover

Inherits:
RubyLsp::Request show all
Defined in:
lib/ruby_lsp/requests/hover.rb

Overview

The [hover request](microsoft.github.io/language-server-protocol/specification#textDocument_hover) displays the documentation for the symbol currently under the cursor. : [ResponseType = Interface::Hover?]

Instance Attribute Summary

Attributes inherited from Message

#method, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLsp::Request

register_watched_files, #to_hash

Methods inherited from Message

#to_hash

Constructor Details

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

: ((RubyDocument | ERBDocument) document, GlobalState global_state, Hash[Symbol, untyped] position, Prism::Dispatcher dispatcher, SorbetLevel sorbet_level) -> void



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
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_lsp/requests/hover.rb', line 20

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

  char_position, _ = document.find_index_by_position(position)
  delegate_request_if_needed!(global_state, document, char_position)

  node_context = RubyDocument.locate(
    document.ast,
    char_position,
    node_types: Listeners::Hover::ALLOWED_TARGETS,
    code_units_cache: document.code_units_cache,
  )
  target = node_context.node
  parent = node_context.parent

  if should_refine_target?(parent, target)
    target = determine_target(
      target, #: as !nil
      parent, #: as !nil
      position,
    )
  elsif position_outside_target?(position, target)
    target = nil
  end

  # Don't need to instantiate any listeners if there's no target
  return unless target

  @target = target #: Prism::Node?
  uri = document.uri
  @response_builder = ResponseBuilders::Hover.new #: ResponseBuilders::Hover
  Listeners::Hover.new(@response_builder, global_state, uri, node_context, dispatcher, sorbet_level)
  Addon.addons.each do |addon|
    addon.create_hover_listener(@response_builder, node_context, dispatcher)
  end

  @dispatcher = dispatcher
end

Class Method Details

.providerObject

: -> Interface::HoverOptions



14
15
16
# File 'lib/ruby_lsp/requests/hover.rb', line 14

def provider
  Interface::HoverOptions.new
end

Instance Method Details

#performObject

: -> ResponseType



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_lsp/requests/hover.rb', line 61

def perform
  return unless @target

  @dispatcher.dispatch_once(@target)

  return if @response_builder.empty?

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