Class: RubyLsp::Requests::InlayHints

Inherits:
BaseRequest
  • Object
show all
Defined in:
lib/ruby_lsp/requests/inlay_hints.rb

Overview

![Inlay hint demo](../../misc/inlay_hint.gif)

[Inlay hints](microsoft.github.io/language-server-protocol/specification#textDocument_inlayHint) are labels added directly in the code that explicitly show the user something that might otherwise just be implied.

# Example

“‘ruby begin

puts "do something that might raise"

rescue # Label “StandardError” goes here as a bare rescue implies rescuing StandardError

puts "handle some rescue"

end “‘

Constant Summary collapse

RESCUE_STRING_LENGTH =
T.let("rescue".length, Integer)

Instance Method Summary collapse

Methods inherited from BaseRequest

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

Constructor Details

#initialize(document, range) ⇒ InlayHints

Returns a new instance of InlayHints.



25
26
27
28
29
30
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 25

def initialize(document, range)
  super(document)

  @hints = T.let([], T::Array[LanguageServer::Protocol::Interface::InlayHint])
  @range = range
end

Instance Method Details

#runObject



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

def run
  visit(@document.tree) if @document.parsed?
  @hints
end

#visit_rescue(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 39

def visit_rescue(node)
  return unless node.exception.nil?

  loc = node.location
  return unless visible?(node, @range)

  @hints << LanguageServer::Protocol::Interface::InlayHint.new(
    position: { line: loc.start_line - 1, character: loc.start_column + RESCUE_STRING_LENGTH },
    label: "StandardError",
    padding_left: true,
    tooltip: "StandardError is implied in a bare rescue",
  )

  super
end