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](../../rails_document_link_hover.gif)

The [hover request](microsoft.github.io/language-server-protocol/specification#textDocument_hover) renders a clickable link to the code’s official documentation. It currently only supports Rails’ documentation: when hovering over Rails DSLs/constants under certain paths, like ‘before_save :callback` in `models/post.rb`, it generates a link to `before_save`’s API documentation.

# Example

“‘ruby class Post < ApplicationRecord

before_save :do_something # when hovering on before_save, the link will be rendered

end “‘

Constant Summary collapse

ResponseType =
type_member { { fixed: T.nilable(Interface::Hover) } }
ALLOWED_TARGETS =
T.let(
  [
    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

add_listener, listeners

Methods included from Support::Common

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

Constructor Details

#initialize(emitter, message_queue) ⇒ Hover

Returns a new instance of Hover.



39
40
41
42
43
44
# File 'lib/ruby_lsp/requests/hover.rb', line 39

def initialize(emitter, message_queue)
  super

  @response = T.let(nil, ResponseType)
  emitter.register(self, :on_command, :on_const_path_ref, :on_call)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



36
37
38
# File 'lib/ruby_lsp/requests/hover.rb', line 36

def response
  @response
end

Instance Method Details

#merge_response!(other) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_lsp/requests/hover.rb', line 48

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

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

  self
end

#on_call(node) ⇒ Object



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

def on_call(node)
  message = node.message
  return if message.is_a?(Symbol)

  @response = generate_rails_document_link_hover(message.value, message)
end

#on_command(node) ⇒ Object



62
63
64
65
# File 'lib/ruby_lsp/requests/hover.rb', line 62

def on_command(node)
  message = node.message
  @response = generate_rails_document_link_hover(message.value, message)
end

#on_const_path_ref(node) ⇒ Object



68
69
70
# File 'lib/ruby_lsp/requests/hover.rb', line 68

def on_const_path_ref(node)
  @response = generate_rails_document_link_hover(full_constant_name(node), node)
end