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 are labels added directly in the code that explicitly show the user something that might otherwise just be implied.

Example

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

#merge_external_listeners_responses!, #merge_response!

Methods included from Support::Common

#create_code_lens, #full_constant_name, #range_from_syntax_tree_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

#response ⇒ Object (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
56
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 43

def on_rescue(node)
  exception = node.exception
  return unless exception.nil? || exception.exceptions.nil?

  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