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 “‘

Instance Method Summary collapse

Methods inherited from BaseRequest

#full_constant_name, #locate_node_and_parent, #range_from_syntax_tree_node

Constructor Details

#initialize(document, position) ⇒ Hover

Returns a new instance of Hover.



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

def initialize(document, position)
  super(document)

  @position = T.let(Document::Scanner.new(document.source).find_position(position), Integer)
end

Instance Method Details

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_lsp/requests/hover.rb', line 31

def run
  return unless @document.parsed?

  target, _ = locate_node_and_parent(
    T.must(@document.tree), [SyntaxTree::Command, SyntaxTree::FCall, SyntaxTree::ConstPathRef], @position
  )

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