Class: RubyLsp::Requests::InlayHints

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/inlay_hints.rb

Overview

![Inlay hint demo](../../inlay_hints.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

ResponseType =
type_member { { fixed: T::Array[Interface::InlayHint] } }
RESCUE_STRING_LENGTH =
T.let("rescue".length, Integer)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#response

Methods included from Support::Common

#create_code_lens, #markdown_from_index_entries, #range_from_location, #range_from_node, #visible?

Constructor Details

#initialize(range, emitter, message_queue) ⇒ InlayHints

Returns a new instance of InlayHints.



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

def initialize(range, emitter, message_queue)
  super(emitter, message_queue)

  @_response = T.let([], ResponseType)
  @range = range

  emitter.register(self, :on_rescue)
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



30
31
32
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 30

def _response
  @_response
end

Instance Method Details

#on_rescue(node) ⇒ Object



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

def on_rescue(node)
  return unless node.exceptions.empty?

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

  @_response << 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",
  )
end