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 “‘

# Example

“‘ruby 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, dispatcher, message_queue) ⇒ InlayHints

Returns a new instance of InlayHints.



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

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

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

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

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



40
41
42
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 40

def _response
  @_response
end

Instance Method Details

#on_implicit_node_enter(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_lsp/requests/inlay_hints.rb', line 68

def on_implicit_node_enter(node)
  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



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

def on_rescue_node_enter(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