Class: RubyLsp::Listeners::InlayHints

Inherits:
RubyLsp::Listener show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/listeners/inlay_hints.rb

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 RubyLsp::Listener

#response

Methods included from Requests::Support::Common

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

Constructor Details

#initialize(range, hints_configuration, dispatcher) ⇒ InlayHints

Returns a new instance of InlayHints.



24
25
26
27
28
29
30
31
32
# File 'lib/ruby_lsp/listeners/inlay_hints.rb', line 24

def initialize(range, hints_configuration, dispatcher)
  super(dispatcher)

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

  dispatcher.register(self, :on_rescue_node_enter, :on_implicit_node_enter)
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



15
16
17
# File 'lib/ruby_lsp/listeners/inlay_hints.rb', line 15

def _response
  @_response
end

Instance Method Details

#on_implicit_node_enter(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_lsp/listeners/inlay_hints.rb', line 51

def on_implicit_node_enter(node)
  return unless @hints_configuration.enabled?(:implicitHashValue)
  return unless visible?(node, @range)

  node_value = node.value
  loc = node.location
  tooltip = ""
  node_name = ""
  case node_value
  when Prism::CallNode
    node_name = node_value.name
    tooltip = "This is a method call. Method name: #{node_name}"
  when Prism::ConstantReadNode
    node_name = node_value.name
    tooltip = "This is a constant: #{node_name}"
  when Prism::LocalVariableReadNode
    node_name = node_value.name
    tooltip = "This is a local variable: #{node_name}"
  end

  @_response << Interface::InlayHint.new(
    position: { line: loc.start_line - 1, character: loc.start_column + node_name.length + 1 },
    label: node_name,
    padding_left: true,
    tooltip: tooltip,
  )
end

#on_rescue_node_enter(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_lsp/listeners/inlay_hints.rb', line 35

def on_rescue_node_enter(node)
  return unless @hints_configuration.enabled?(:implicitRescue)
  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