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.

Configuration

To enable rescue hints, set rubyLsp.featuresConfiguration.inlayHint.implicitRescue to true.

To enable hash value hints, set rubyLsp.featuresConfiguration.inlayHint.implicitHashValue to true.

To enable all hints, set rubyLsp.featuresConfiguration.inlayHint.enableAll to true.

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

Example

var = "foo"
{
  var: var, # Label "var" goes here in cases where the value is omitted
  a: "hello",
}

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, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(range, hints_configuration, dispatcher) ⇒ InlayHints

Returns a new instance of InlayHints.



57
58
59
60
61
62
63
64
65
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 57

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

#_response ⇒ Object (readonly)

Returns the value of attribute _response.



48
49
50
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 48

def _response
  @_response
end

Instance Method Details

#on_implicit_node_enter(node) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 84

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 68

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