Class: RubyLsp::Listeners::DocumentSymbol

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/listeners/document_symbol.rb

Constant Summary collapse

ATTR_ACCESSORS =
T.let([:attr_reader, :attr_writer, :attr_accessor].freeze, T::Array[Symbol])

Instance Method Summary collapse

Methods included from Requests::Support::Common

#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?, #sorbet_level_true_or_higher?, #visible?

Constructor Details

#initialize(response_builder, uri, dispatcher) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 19

def initialize(response_builder, uri, dispatcher)
  @response_builder = response_builder
  @uri = uri

  dispatcher.register(
    self,
    :on_class_node_enter,
    :on_class_node_leave,
    :on_call_node_enter,
    :on_call_node_leave,
    :on_constant_path_write_node_enter,
    :on_constant_write_node_enter,
    :on_constant_path_or_write_node_enter,
    :on_constant_path_operator_write_node_enter,
    :on_constant_path_and_write_node_enter,
    :on_constant_or_write_node_enter,
    :on_constant_operator_write_node_enter,
    :on_constant_and_write_node_enter,
    :on_constant_target_node_enter,
    :on_constant_path_target_node_enter,
    :on_def_node_enter,
    :on_def_node_leave,
    :on_module_node_enter,
    :on_module_node_leave,
    :on_instance_variable_write_node_enter,
    :on_class_variable_write_node_enter,
    :on_singleton_class_node_enter,
    :on_singleton_class_node_leave,
    :on_alias_method_node_enter,
  )
end

Instance Method Details

#on_alias_method_node_enter(node) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 273

def on_alias_method_node_enter(node)
  new_name_node = node.new_name
  return unless new_name_node.is_a?(Prism::SymbolNode)

  name = new_name_node.value
  return unless name

  create_document_symbol(
    name: name,
    kind: Constant::SymbolKind::METHOD,
    range_location: new_name_node.location,
    selection_range_location: T.must(new_name_node.value_loc),
  )
end

#on_call_node_enter(node) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 84

def on_call_node_enter(node)
  node_name = node.name
  if ATTR_ACCESSORS.include?(node_name)
    handle_attr_accessor(node)
  elsif node_name == :alias_method
    handle_alias_method(node)
  elsif node_name == :namespace
    handle_rake_namespace(node)
  elsif node_name == :task
    handle_rake_task(node)
  end
end

#on_call_node_leave(node) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 98

def on_call_node_leave(node)
  return unless rake?

  if node.name == :namespace && !node.receiver
    @response_builder.pop
  end
end

#on_class_node_enter(node) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 52

def on_class_node_enter(node)
  @response_builder << create_document_symbol(
    name: node.constant_path.location.slice,
    kind: Constant::SymbolKind::CLASS,
    range_location: node.location,
    selection_range_location: node.constant_path.location,
  )
end

#on_class_node_leave(node) ⇒ Object



62
63
64
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 62

def on_class_node_leave(node)
  @response_builder.pop
end

#on_class_variable_write_node_enter(node) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 263

def on_class_variable_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::VARIABLE,
    range_location: node.name_loc,
    selection_range_location: node.name_loc,
  )
end

#on_constant_and_write_node_enter(node) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 167

def on_constant_and_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )
end

#on_constant_operator_write_node_enter(node) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 177

def on_constant_operator_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )
end

#on_constant_or_write_node_enter(node) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 157

def on_constant_or_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )
end

#on_constant_path_and_write_node_enter(node) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 127

def on_constant_path_and_write_node_enter(node)
  create_document_symbol(
    name: node.target.location.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.target.location,
  )
end

#on_constant_path_operator_write_node_enter(node) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 147

def on_constant_path_operator_write_node_enter(node)
  create_document_symbol(
    name: node.target.location.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.target.location,
  )
end

#on_constant_path_or_write_node_enter(node) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 137

def on_constant_path_or_write_node_enter(node)
  create_document_symbol(
    name: node.target.location.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.target.location,
  )
end

#on_constant_path_target_node_enter(node) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 197

def on_constant_path_target_node_enter(node)
  create_document_symbol(
    name: node.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.location,
  )
end

#on_constant_path_write_node_enter(node) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 107

def on_constant_path_write_node_enter(node)
  create_document_symbol(
    name: node.target.location.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.target.location,
  )
end

#on_constant_target_node_enter(node) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 187

def on_constant_target_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.location,
  )
end

#on_constant_write_node_enter(node) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 117

def on_constant_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )
end

#on_def_node_enter(node) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 222

def on_def_node_enter(node)
  receiver = node.receiver
  previous_symbol = @response_builder.last

  if receiver.is_a?(Prism::SelfNode)
    name = "self.#{node.name}"
    kind = Constant::SymbolKind::FUNCTION
  elsif previous_symbol.is_a?(Interface::DocumentSymbol) && previous_symbol.name.start_with?("<<")
    name = node.name.to_s
    kind = Constant::SymbolKind::FUNCTION
  else
    name = node.name.to_s
    kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD
  end

  symbol = create_document_symbol(
    name: name,
    kind: kind,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )

  @response_builder << symbol
end

#on_def_node_leave(node) ⇒ Object



207
208
209
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 207

def on_def_node_leave(node)
  @response_builder.pop
end

#on_instance_variable_write_node_enter(node) ⇒ Object



253
254
255
256
257
258
259
260
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 253

def on_instance_variable_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::VARIABLE,
    range_location: node.name_loc,
    selection_range_location: node.name_loc,
  )
end

#on_module_node_enter(node) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 212

def on_module_node_enter(node)
  @response_builder << create_document_symbol(
    name: node.constant_path.location.slice,
    kind: Constant::SymbolKind::MODULE,
    range_location: node.location,
    selection_range_location: node.constant_path.location,
  )
end

#on_module_node_leave(node) ⇒ Object



248
249
250
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 248

def on_module_node_leave(node)
  @response_builder.pop
end

#on_singleton_class_node_enter(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 67

def on_singleton_class_node_enter(node)
  expression = node.expression

  @response_builder << create_document_symbol(
    name: "<< #{expression.slice}",
    kind: Constant::SymbolKind::NAMESPACE,
    range_location: node.location,
    selection_range_location: expression.location,
  )
end

#on_singleton_class_node_leave(node) ⇒ Object



79
80
81
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 79

def on_singleton_class_node_leave(node)
  @response_builder.pop
end