Class: RubyLsp::Requests::Hover

Inherits:
Listener
  • Object
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) } }
ALLOWED_TARGETS =
T.let(
  [
    SyntaxTree::Const,
    SyntaxTree::Command,
    SyntaxTree::CallNode,
    SyntaxTree::ConstPathRef,
  ],
  T::Array[T.class_of(SyntaxTree::Node)],
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#merge_external_listeners_responses!

Methods included from Support::Common

#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(index, nesting, emitter, message_queue) ⇒ Hover

Returns a new instance of Hover.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_lsp/requests/hover.rb', line 43

def initialize(index, nesting, emitter, message_queue)
  super(emitter, message_queue)

  @nesting = nesting
  @index = index
  @external_listeners.concat(
    Extension.extensions.filter_map { |ext| ext.create_hover_listener(emitter, message_queue) },
  )
  @response = T.let(nil, ResponseType)
  emitter.register(self, :on_const_path_ref, :on_const)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



33
34
35
# File 'lib/ruby_lsp/requests/hover.rb', line 33

def response
  @response
end

Instance Method Details

#merge_response!(other) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_lsp/requests/hover.rb', line 57

def merge_response!(other)
  other_response = other.response
  return self unless other_response

  if @response.nil?
    @response = other.response
  else
    @response.contents.value << "\n\n" << other_response.contents.value
  end

  self
end

#on_const(node) ⇒ Object



79
80
81
82
83
# File 'lib/ruby_lsp/requests/hover.rb', line 79

def on_const(node)
  return if DependencyDetector::HAS_TYPECHECKER

  generate_hover(node.value, node)
end

#on_const_path_ref(node) ⇒ Object



71
72
73
74
75
76
# File 'lib/ruby_lsp/requests/hover.rb', line 71

def on_const_path_ref(node)
  return if DependencyDetector::HAS_TYPECHECKER

  name = full_constant_name(node)
  generate_hover(name, node)
end