Class: RubyLsp::Listeners::DocumentSymbol

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

Constant Summary collapse

ATTR_ACCESSORS =

: Array

[:attr_reader, :attr_writer, :attr_accessor].freeze

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?

Constructor Details

#initialize(response_builder, uri, dispatcher) ⇒ DocumentSymbol

: (ResponseBuilders::DocumentSymbol response_builder, URI::Generic uri, Prism::Dispatcher dispatcher) -> void



12
13
14
15
16
17
18
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
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 12

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_instance_variable_target_node_enter,
    :on_instance_variable_operator_write_node_enter,
    :on_instance_variable_or_write_node_enter,
    :on_instance_variable_and_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

: (Prism::AliasMethodNode node) -> void



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 310

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: new_name_node.value_loc, #: as !nil
  )
end

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode node) -> void



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 81

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

: (Prism::CallNode node) -> void



95
96
97
98
99
100
101
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 95

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

: (Prism::ClassNode node) -> void



49
50
51
52
53
54
55
56
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 49

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

: (Prism::ClassNode node) -> void



59
60
61
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 59

def on_class_node_leave(node)
  @response_builder.pop
end

#on_class_variable_write_node_enter(node) ⇒ Object

: (Prism::ClassVariableWriteNode node) -> void



250
251
252
253
254
255
256
257
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 250

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

: (Prism::ConstantAndWriteNode node) -> void



164
165
166
167
168
169
170
171
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 164

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

: (Prism::ConstantOperatorWriteNode node) -> void



174
175
176
177
178
179
180
181
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 174

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

: (Prism::ConstantOrWriteNode node) -> void



154
155
156
157
158
159
160
161
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 154

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

: (Prism::ConstantPathAndWriteNode node) -> void



124
125
126
127
128
129
130
131
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 124

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

: (Prism::ConstantPathOperatorWriteNode node) -> void



144
145
146
147
148
149
150
151
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 144

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

: (Prism::ConstantPathOrWriteNode node) -> void



134
135
136
137
138
139
140
141
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 134

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

: (Prism::ConstantPathTargetNode node) -> void



194
195
196
197
198
199
200
201
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 194

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

: (Prism::ConstantPathWriteNode node) -> void



104
105
106
107
108
109
110
111
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 104

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

: (Prism::ConstantTargetNode node) -> void



184
185
186
187
188
189
190
191
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 184

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

: (Prism::ConstantWriteNode node) -> void



114
115
116
117
118
119
120
121
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 114

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

: (Prism::DefNode node) -> void



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

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

: (Prism::DefNode node) -> void



204
205
206
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 204

def on_def_node_leave(node)
  @response_builder.pop
end

#on_instance_variable_and_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableAndWriteNode node) -> void



300
301
302
303
304
305
306
307
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 300

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

#on_instance_variable_operator_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableOperatorWriteNode node) -> void



280
281
282
283
284
285
286
287
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 280

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

#on_instance_variable_or_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableOrWriteNode node) -> void



290
291
292
293
294
295
296
297
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 290

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

#on_instance_variable_target_node_enter(node) ⇒ Object

: (Prism::InstanceVariableTargetNode node) -> void



270
271
272
273
274
275
276
277
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 270

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

#on_instance_variable_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableWriteNode node) -> void



260
261
262
263
264
265
266
267
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 260

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

#on_module_node_enter(node) ⇒ Object

: (Prism::ModuleNode node) -> void



209
210
211
212
213
214
215
216
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 209

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

: (Prism::ModuleNode node) -> void



245
246
247
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 245

def on_module_node_leave(node)
  @response_builder.pop
end

#on_singleton_class_node_enter(node) ⇒ Object

: (Prism::SingletonClassNode node) -> void



64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 64

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

: (Prism::SingletonClassNode node) -> void



76
77
78
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 76

def on_singleton_class_node_leave(node)
  @response_builder.pop
end