Class: RubyLsp::Requests::Hover

Inherits:
BaseRequest
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/hover.rb

Overview

![Hover demo](../../misc/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

ALLOWED_TARGETS =
T.let(
  [
    SyntaxTree::Command,
    SyntaxTree::CallNode,
    SyntaxTree::ConstPathRef,
  ],
  T::Array[T.class_of(SyntaxTree::Node)],
)

Instance Method Summary collapse

Methods inherited from BaseRequest

#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(document, position) ⇒ Hover

Returns a new instance of Hover.



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

def initialize(document, position)
  super(document)

  @position = T.let(document.create_scanner.find_char_position(position), Integer)
end

Instance Method Details

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_lsp/requests/hover.rb', line 40

def run
  return unless @document.parsed?

  target, parent = locate(T.must(@document.tree), @position)
  target = parent if !ALLOWED_TARGETS.include?(target.class) && ALLOWED_TARGETS.include?(parent.class)

  case target
  when SyntaxTree::Command
    message = target.message
    generate_rails_document_link_hover(message.value, message)
  when SyntaxTree::CallNode
    return if target.message == :call

    generate_rails_document_link_hover(target.message.value, target.message)
  when SyntaxTree::ConstPathRef
    constant_name = full_constant_name(target)
    generate_rails_document_link_hover(constant_name, target)
  end
end