Class: RubyLsp::Requests::Hover

Inherits:
RubyLsp::Request show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/hover.rb

Overview

![Hover demo](../../hover.gif)

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

# Example

“‘ruby String # -> Hovering over the class reference will show all declaration locations and the documentation “`

Constant Summary collapse

ResponseType =
type_member { { fixed: T.nilable(Interface::Hover) } }

Instance Attribute Summary

Attributes inherited from Message

#method, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLsp::Request

#to_hash

Methods inherited from Message

#to_hash

Constructor Details

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

Returns a new instance of Hover.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_lsp/requests/hover.rb', line 42

def initialize(document, global_state, position, dispatcher, sorbet_level)
  super()
  node_context = document.locate_node(position, node_types: Listeners::Hover::ALLOWED_TARGETS)
  target = node_context.node
  parent = node_context.parent

  if (Listeners::Hover::ALLOWED_TARGETS.include?(parent.class) &&
      !Listeners::Hover::ALLOWED_TARGETS.include?(target.class)) ||
      (parent.is_a?(Prism::ConstantPathNode) && target.is_a?(Prism::ConstantReadNode))
    target = determine_target(
      T.must(target),
      T.must(parent),
      position,
    )
  elsif target.is_a?(Prism::CallNode) && target.name != :require && target.name != :require_relative &&
      !covers_position?(target.message_loc, position)

    target = nil
  end

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

  @target = T.let(target, T.nilable(Prism::Node))
  uri = document.uri
  @response_builder = T.let(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



26
27
28
# File 'lib/ruby_lsp/requests/hover.rb', line 26

def provider
  Interface::HoverOptions.new
end

Instance Method Details

#performObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_lsp/requests/hover.rb', line 77

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