ruby_language_server 0.4.2 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,255 +1,452 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ripper'
3
+ require 'prism'
4
4
  require_relative 'scope_parser_commands/rake_commands'
5
5
  require_relative 'scope_parser_commands/rspec_commands'
6
6
  require_relative 'scope_parser_commands/ruby_commands'
7
7
  require_relative 'scope_parser_commands/rails_commands'
8
8
 
9
9
  module RubyLanguageServer
10
- # This class is responsible for processing the generated sexp from the ScopeParser below.
10
+ # This class is responsible for processing the AST from Prism.
11
11
  # It builds scopes that amount to heirarchical arrays with information about what
12
12
  # classes, methods, variables, etc - are in each scope.
13
- class SEXPProcessor
13
+ class PrismProcessor < Prism::Visitor
14
14
  include ScopeParserCommands::RakeCommands
15
15
  include ScopeParserCommands::RspecCommands
16
16
  include ScopeParserCommands::RailsCommands
17
17
  include ScopeParserCommands::RubyCommands
18
- attr_reader :sexp, :lines, :current_scope
19
18
 
20
- def initialize(sexp, lines = 1, shallow = false)
21
- @sexp = sexp
19
+ attr_reader :current_scope, :lines
20
+
21
+ def initialize(lines = 1)
22
22
  @lines = lines
23
- @shallow = shallow
24
23
  @root_scope = nil
24
+ @current_scope = nil
25
+ @block_names = {}
25
26
  end
26
27
 
27
28
  def root_scope
28
- return @root_scope unless @root_scope.nil?
29
-
30
- @root_scope = ScopeData::Scope.where(path: nil, class_type: ScopeData::Scope::TYPE_ROOT).first_or_create!
31
- @current_scope = @root_scope
32
- process(@sexp)
33
- @root_scope
34
- end
35
-
36
- def process(sexp)
37
- return if sexp.nil?
38
-
39
- root, args, *rest = sexp
40
- # RubyLanguageServer.logger.error("Doing #{[root, args, rest]}")
41
- case root
42
- when Array
43
- sexp.each { |child| process(child) }
44
- when Symbol
45
- root = root.to_s.gsub(/^@+/, '')
46
- method_name = "on_#{root}"
47
- if respond_to? method_name
48
- send(method_name, args, rest)
49
- else
50
- RubyLanguageServer.logger.debug("We don't have a #{method_name} with #{args}")
51
- process(args)
52
- end
53
- when String
54
- # We really don't do anything with it!
55
- RubyLanguageServer.logger.debug("We don't do Strings like #{root} with #{args}")
56
- when NilClass, FalseClass
57
- process(args)
58
- else
59
- RubyLanguageServer.logger.warn("We don't respond to the likes of #{root} of class #{root.class}")
29
+ @root_scope ||= begin
30
+ scope = ScopeData::Scope.where(path: nil, class_type: ScopeData::Scope::TYPE_ROOT).first_or_create!
31
+ @current_scope = scope
32
+ scope
60
33
  end
61
34
  end
62
35
 
63
- def on_sclass(_args, rest)
64
- process(rest)
65
- end
36
+ # Visit a class node
37
+ def visit_class_node(node)
38
+ name = constant_path_name(node.constant_path)
39
+ line = node.location.start_line
40
+ end_line = node.location.end_line
41
+ column = column_of(node.location)
66
42
 
67
- # foo = bar -- bar is in the vcall. Pretty sure we don't want to remember this.
68
- def on_vcall(_args, rest)
69
- # Seriously - discard args. Maybe process rest?
70
- process(rest)
71
- end
43
+ scope = push_scope(ScopeData::Scope::TYPE_CLASS, name, line, column, end_line, false)
44
+
45
+ # Handle superclass
46
+ if node.superclass
47
+ superclass_name = constant_path_name(node.superclass)
48
+ scope.set_superclass_name(superclass_name) if superclass_name
49
+ end
72
50
 
73
- def on_program(args, _rest)
74
- process(args)
51
+ in_singleton_class(false) { super }
52
+ pop_scope
75
53
  end
76
54
 
77
- def on_var_field(args, rest)
78
- (_, name, (line, column)) = args
79
- return if name.nil?
55
+ # Visit a module node
56
+ def visit_module_node(node)
57
+ name = constant_path_name(node.constant_path)
58
+ line = node.location.start_line
59
+ end_line = node.location.end_line
60
+ column = column_of(node.location)
80
61
 
81
- if name.start_with?('@')
82
- add_ivar(name, line, column)
83
- else
84
- add_variable(name, line, column)
85
- end
86
- process(rest)
62
+ push_scope(ScopeData::Scope::TYPE_MODULE, name, line, column, end_line, false)
63
+ in_singleton_class(false) { super }
64
+ pop_scope
87
65
  end
88
66
 
89
- def on_bodystmt(args, _rest)
90
- process(args)
67
+ # Visit a def node
68
+ def visit_def_node(node)
69
+ name = node.name.to_s
70
+ line = node.location.start_line
71
+ end_line = node.location.end_line
72
+ column = column_of(node.location)
73
+ is_class_method = node.receiver.is_a?(Prism::SelfNode)
74
+
75
+ scope = push_scope(ScopeData::Scope::TYPE_METHOD, name, line, column, end_line, false)
76
+ # def self.name, or a plain def inside class << self
77
+ scope.class_method = is_class_method || (node.receiver.nil? && @in_singleton_class)
78
+ scope.save!
79
+
80
+ # Extract and store parameters
81
+ if node.parameters
82
+ params_data = extract_parameters_data(node.parameters)
83
+ scope.set_parameters(params_data) if params_data.any?
84
+ scope.save!
85
+ end
86
+
87
+ # Process parameters (adds them as variables in scope)
88
+ visit_parameters(node.parameters) if node.parameters
89
+
90
+ super
91
+
92
+ pop_scope
93
+ scope
91
94
  end
92
95
 
93
- def on_module(args, rest)
94
- scope = add_scope(args.last, rest, ScopeData::Scope::TYPE_MODULE)
95
- assign_subclass(scope, rest)
96
+ # Visit a singleton class (class << self)
97
+ def visit_singleton_class_node(node)
98
+ # For class << self, we visit the body but don't create a new scope.
99
+ # The methods defined inside are class methods of the current scope.
100
+ # class << some_object is not about the current class, so it doesn't count.
101
+ in_singleton_class(node.expression.is_a?(Prism::SelfNode)) { super }
96
102
  end
97
103
 
98
- def on_class(args, rest)
99
- scope = add_scope(args.last, rest, ScopeData::Scope::TYPE_CLASS)
100
- assign_subclass(scope, rest)
104
+ # Visit block nodes
105
+ def visit_block_node(node)
106
+ line = node.location.start_line
107
+ end_line = node.location.end_line
108
+ column = column_of(node.location)
109
+
110
+ # Use block-specific name if set by command handler, otherwise use 'block'
111
+ name = @block_names.delete(node.object_id) || 'block'
112
+
113
+ push_scope(ScopeData::Scope::TYPE_BLOCK, name, line, column, end_line, false)
114
+
115
+ # Process block parameters
116
+ visit_block_parameters(node.parameters) if node.parameters
117
+
118
+ super
119
+ pop_scope
101
120
  end
102
121
 
103
- def assign_subclass(scope, sexp)
104
- return unless !sexp[0].nil? && sexp[0][0] == :var_ref
122
+ # Visit local variable write nodes
123
+ def visit_local_variable_write_node(node)
124
+ name = node.name.to_s
125
+ line = node.location.start_line
126
+ column = column_of(node.location)
105
127
 
106
- (_, (_, name)) = sexp[0]
107
- scope.set_superclass_name(name)
128
+ add_variable(name, line, column)
129
+ super
108
130
  end
109
131
 
110
- def on_method_add_block(args, rest)
111
- scope = @current_scope
112
- process(args)
113
- process(rest)
114
- # add_scope(args, rest, ScopeData::Scope::TYPE_BLOCK)
115
- unless @current_scope == scope
116
- scope.bottom_line = [scope&.bottom_line, @current_scope.bottom_line].compact.max
117
- scope.save!
118
- pop_scope
119
- end
132
+ # Visit instance variable write nodes
133
+ def visit_instance_variable_write_node(node)
134
+ name = node.name.to_s
135
+ line = node.location.start_line
136
+ column = column_of(node.location)
137
+
138
+ add_ivar(name, line, column)
139
+ super
120
140
  end
121
141
 
122
- def on_do_block(args, rest)
123
- ((_, ((_, (_, (_, _name, (line, column))))))) = args
124
- push_scope(ScopeData::Scope::TYPE_BLOCK, 'block', line, column, false)
125
- process(args)
126
- process(rest)
127
- pop_scope
142
+ # Visit constant write nodes
143
+ # FOO = 42
144
+ def visit_constant_write_node(node)
145
+ name = node.name.to_s
146
+ line = node.location.start_line
147
+ column = column_of(node.location)
148
+
149
+ add_variable(name, line, column)
150
+ super
128
151
  end
129
152
 
130
- def on_block_var(args, rest)
131
- process(args)
132
- process(rest)
153
+ # Visit multi-write nodes (parallel assignment)
154
+ def visit_multi_write_node(node)
155
+ node.lefts.each do |target|
156
+ case target
157
+ when Prism::LocalVariableTargetNode
158
+ add_variable(target.name.to_s, target.location.start_line, column_of(target.location))
159
+ when Prism::InstanceVariableTargetNode
160
+ add_ivar(target.name.to_s, target.location.start_line, column_of(target.location))
161
+ when Prism::MultiTargetNode
162
+ # Handle nested destructuring like (a, (b, c)) = ...
163
+ visit_multi_target(target)
164
+ end
165
+ end
166
+
167
+ # Handle rest parameter in multi-assignment
168
+ if node.rest.is_a?(Prism::SplatNode) && node.rest.expression
169
+ case node.rest.expression
170
+ when Prism::LocalVariableTargetNode
171
+ add_variable(node.rest.expression.name.to_s, node.rest.expression.location.start_line, column_of(node.rest.expression.location))
172
+ end
173
+ end
174
+
175
+ super
133
176
  end
134
177
 
135
- # Used only to describe subclasses? -- nope
136
- def on_var_ref(_args, _rest)
137
- # [:@const, "Bar", [13, 20]]
138
- # (_, name) = args
139
- # @current_scope.set_superclass_name(name)
178
+ # Helper to handle nested multi-target nodes (for assignments)
179
+ def visit_multi_target(node)
180
+ node.lefts.each do |target|
181
+ case target
182
+ when Prism::LocalVariableTargetNode
183
+ add_variable(target.name.to_s, target.location.start_line, column_of(target.location))
184
+ when Prism::InstanceVariableTargetNode
185
+ add_ivar(target.name.to_s, target.location.start_line, column_of(target.location))
186
+ when Prism::MultiTargetNode
187
+ visit_multi_target(target)
188
+ end
189
+ end
140
190
  end
141
191
 
142
- def on_assign(args, rest)
143
- process(args)
144
- process(rest)
192
+ # Helper to extract variables from multi-target nodes (for parameters)
193
+ # block_method { |(a, b), c| ... } # we need to extract a and b in addition to c
194
+ def extract_multi_target_variables(node)
195
+ node.lefts.each do |target|
196
+ case target
197
+ when Prism::RequiredParameterNode
198
+ add_variable(target.name.to_s, target.location.start_line, column_of(target.location))
199
+ when Prism::MultiTargetNode
200
+ # Recursively handle nested destructuring
201
+ extract_multi_target_variables(target)
202
+ end
203
+ end
204
+
205
+ # Handle rest in multi-target (though rare in practice)
206
+ if node.rest
207
+ case node.rest
208
+ when Prism::SplatNode
209
+ add_variable(node.rest.expression.name.to_s, node.rest.expression.location.start_line, column_of(node.rest.expression.location)) if node.rest.expression.is_a?(Prism::RequiredParameterNode)
210
+ end
211
+ end
212
+
213
+ # Handle rights (elements after rest)
214
+ node.rights.each do |target|
215
+ case target
216
+ when Prism::RequiredParameterNode
217
+ add_variable(target.name.to_s, target.location.start_line, column_of(target.location))
218
+ when Prism::MultiTargetNode
219
+ extract_multi_target_variables(target)
220
+ end
221
+ end
145
222
  end
146
223
 
147
- def on_def(args, rest)
148
- add_scope(args, rest, ScopeData::Scope::TYPE_METHOD)
224
+ # Visit call nodes (method calls)
225
+ def visit_call_node(node)
226
+ name = node.name.to_s
227
+
228
+ # Handle special commands like attr_accessor, private, etc.
229
+ # Only handle if there's no receiver (i.e., it's a method call in current scope)
230
+ if node.receiver.nil?
231
+ block_node = node.block if node.block.is_a?(Prism::BlockNode)
232
+ handle_command(name, node, block_node)
233
+
234
+ # If the command pushed a scope for a block, pop it after visiting children
235
+ should_pop = @command_pushed_scope
236
+ @command_pushed_scope = false
237
+
238
+ super
239
+
240
+ pop_scope if should_pop
241
+ else
242
+ super
243
+ end
149
244
  end
150
245
 
151
- # def self.something(par)...
152
- # [:var_ref, [:@kw, "self", [28, 14]]], [[:@period, ".", [28, 18]], [:@ident, "something", [28, 19]], [:paren, [:params, [[:@ident, "par", [28, 23]]], nil, nil, nil, nil, nil, nil]], [:bodystmt, [[:assign, [:var_field, [:@ident, "pax", [29, 12]]], [:var_ref, [:@ident, "par", [29, 18]]]]], nil, nil, nil]]
153
- def on_defs(args, rest)
154
- on_def(rest[1], rest[2..]) if args[1][1] == 'self' && rest[0][1] == '.'
246
+ private
247
+
248
+ # LSP character offsets count UTF-16 code units. Prism's start_column counts bytes,
249
+ # which differ from code units on any line with non-ASCII text before the node.
250
+ def column_of(location)
251
+ location.start_code_units_column(Encoding::UTF_16LE)
155
252
  end
156
253
 
157
- # Multiple left hand side
158
- # (foo, bar) = somethingg...
159
- def on_mlhs(args, rest)
160
- process(args)
161
- process(rest)
254
+ def extract_parameters_data(params_node)
255
+ return [] unless params_node
256
+
257
+ params = []
258
+
259
+ # Required parameters
260
+ params_node.requireds.each do |param|
261
+ case param
262
+ when Prism::RequiredParameterNode
263
+ params << { name: param.name.to_s, type: 'required' }
264
+ when Prism::MultiTargetNode
265
+ # Handle destructuring - simplified as single param
266
+ params << { name: 'args', type: 'required' }
267
+ end
268
+ end
269
+
270
+ # Optional parameters
271
+ params_node.optionals.each do |param|
272
+ params << { name: param.name.to_s, type: 'optional' }
273
+ end
274
+
275
+ # Rest parameter
276
+ params << { name: "*#{params_node.rest.name}", type: 'rest' } if params_node.rest&.name
277
+
278
+ # Keyword parameters
279
+ params_node.keywords.each do |param|
280
+ name = param.name.to_s
281
+ params << { name: "#{name}:", type: 'keyword' }
282
+ end
283
+
284
+ # Keyword rest parameter
285
+ params << { name: "**#{params_node.keyword_rest.name}", type: 'keyword_rest' } if params_node.keyword_rest.is_a?(Prism::KeywordRestParameterNode) && params_node.keyword_rest.name
286
+
287
+ # Block parameter
288
+ params << { name: "&#{params_node.block.name}", type: 'block' } if params_node.block&.name
289
+
290
+ params
162
291
  end
163
292
 
164
- # ident is something that gets processed at parameters to a function or block
165
- def on_ident(name, ((line, column)))
166
- add_variable(name, line, column)
293
+ def visit_parameters(params_node)
294
+ return unless params_node
295
+
296
+ # Required parameters
297
+ params_node.requireds.each do |param|
298
+ case param
299
+ when Prism::RequiredParameterNode
300
+ add_variable(param.name.to_s, param.location.start_line, column_of(param.location))
301
+ when Prism::MultiTargetNode
302
+ # Handle destructuring in parameters like |(a, b), c|
303
+ extract_multi_target_variables(param)
304
+ end
305
+ end
306
+
307
+ # Optional parameters
308
+ params_node.optionals.each do |param|
309
+ add_variable(param.name.to_s, param.location.start_line, column_of(param.location))
310
+ end
311
+
312
+ # Rest parameter
313
+ add_variable(params_node.rest.name.to_s, params_node.rest.location.start_line, column_of(params_node.rest.location)) if params_node.rest&.name
314
+
315
+ # Keyword parameters
316
+ params_node.keywords.each do |param|
317
+ name = param.name.to_s
318
+ add_variable(name, param.location.start_line, column_of(param.location))
319
+ end
320
+
321
+ # Keyword rest parameter
322
+ add_variable(params_node.keyword_rest.name.to_s, params_node.keyword_rest.location.start_line, column_of(params_node.keyword_rest.location)) if params_node.keyword_rest.is_a?(Prism::KeywordRestParameterNode) && params_node.keyword_rest.name
323
+
324
+ # Block parameter
325
+ add_variable(params_node.block.name.to_s, params_node.block.location.start_line, column_of(params_node.block.location)) if params_node.block&.name
167
326
  end
168
327
 
169
- def on_params(args, rest)
170
- process(args)
171
- process(rest)
328
+ def visit_block_parameters(params_node)
329
+ return unless params_node
330
+
331
+ visit_parameters(params_node.parameters) if params_node.parameters
172
332
  end
173
333
 
174
- # The on_command function idea is stolen from RipperTags https://github.com/tmm1/ripper-tags/blob/master/lib/ripper-tags/parser.rb
175
- def on_command(args, rest)
176
- # [:@ident, "public", [6, 8]]
177
- (_, name, (line, _column)) = args
334
+ def constant_path_name(node)
335
+ case node
336
+ when Prism::ConstantReadNode
337
+ node.name.to_s
338
+ when Prism::ConstantPathNode
339
+ # For Some::Class, we need to recursively build the path
340
+ parts = []
341
+
342
+ # Add the rightmost name (e.g., "Class" in Some::Class)
343
+ parts << node.name.to_s
344
+
345
+ # Traverse the parent chain
346
+ current = node.parent
347
+ while current
348
+ case current
349
+ when Prism::ConstantReadNode
350
+ parts.unshift(current.name.to_s)
351
+ current = nil
352
+ when Prism::ConstantPathNode
353
+ parts.unshift(current.name.to_s)
354
+ current = current.parent
355
+ else
356
+ current = nil
357
+ end
358
+ end
359
+
360
+ parts.join('::')
361
+ else
362
+ node.to_s if node.respond_to?(:to_s)
363
+ end
364
+ end
178
365
 
366
+ # Calls on_<name>_command(node) from ScopeParserCommands, if there is one, with
367
+ # @current_block_node set to the call's block. Handlers read the call with
368
+ # command_line, command_column and command_arguments.
369
+ def handle_command(name, node, block_node = nil)
179
370
  method_name = "on_#{name}_command"
180
- if respond_to? method_name
181
- return send(method_name, line, args, rest)
371
+ if respond_to?(method_name)
372
+ @current_block_node = block_node
373
+ send(method_name, node)
374
+ @current_block_node = nil
182
375
  else
183
- RubyLanguageServer.logger.debug("We don't have a #{method_name} with #{args}")
376
+ RubyLanguageServer.logger.debug("We don't have a #{method_name}")
184
377
  end
378
+ end
185
379
 
186
- case name
187
- when 'public', 'private', 'protected'
188
- # FIXME: access control...
189
- process(rest)
190
- when 'delegate'
191
- # on_delegate(*args[0][1..-1])
192
- when 'def_delegator', 'def_instance_delegator'
193
- # on_def_delegator(*args[0][1..-1])
194
- when 'def_delegators', 'def_instance_delegators'
195
- # on_def_delegators(*args[0][1..-1])
196
- end
380
+ def command_line(node)
381
+ node.location.start_line
197
382
  end
198
383
 
199
- # The on_method_add_arg function is downright stolen from RipperTags https://github.com/tmm1/ripper-tags/blob/master/lib/ripper-tags/parser.rb
200
- def on_method_add_arg(call, args)
201
- call_name = call && call[0]
202
- first_arg = args && args[0] == :args && args[1]
384
+ def command_column(node)
385
+ column_of(node.location)
386
+ end
203
387
 
204
- if call_name == :call && first_arg
205
- if args.length == 2
206
- # augment call if a single argument was used
207
- call = call.dup
208
- call[3] = args[1]
209
- end
210
- call
211
- elsif call_name == :fcall && first_arg
212
- name, line = call[1]
213
- case name
214
- when 'alias_method' # this is an fcall
215
- [:alias, args[1][0], args[2][0], line] if args[1] && args[2]
216
- when 'define_method' # this is an fcall
217
- [:def, args[1][0], line]
218
- when 'public_class_method', 'private_class_method', 'private', 'public', 'protected'
219
- access = name.sub('_class_method', '')
220
-
221
- if args[1][1] == 'self'
222
- klass = 'self'
223
- method_name = args[1][2]
224
- else
225
- klass = nil
226
- method_name = args[1][1]
227
- end
388
+ # The last line of the command's block, or its own line if it has none.
389
+ def command_end_line(node)
390
+ @current_block_node ? @current_block_node.location.end_line : command_line(node)
391
+ end
228
392
 
229
- [:def_with_access, klass, method_name, access, line]
393
+ # The command's arguments that name things, as strings, in order:
394
+ # symbols (attr_reader :foo), strings (describe 'thing'), constants
395
+ # (describe Some::Class) and symbol keys of a keyword hash with a trailing colon
396
+ # (task something: [] becomes "something:").
397
+ def command_arguments(node)
398
+ return [] unless node.arguments
399
+
400
+ args = []
401
+ node.arguments.arguments.each do |arg|
402
+ case arg
403
+ when Prism::SymbolNode
404
+ # Extract symbol name
405
+ args << arg.value.to_s if arg.value
406
+ when Prism::StringNode
407
+ args << arg.unescaped
408
+ when Prism::ConstantReadNode
409
+ # Handle constant arguments like: describe SomeClass
410
+ args << arg.name.to_s
411
+ when Prism::ConstantPathNode
412
+ # Handle constant path arguments like: describe Some::Class
413
+ args << constant_path_name(arg)
414
+ when Prism::KeywordHashNode
415
+ # Handle keyword arguments like: task something: [] do
416
+ # Extract the keys which are the task names
417
+ arg.elements.each do |element|
418
+ if element.is_a?(Prism::AssocNode) && element.key.is_a?(Prism::SymbolNode)
419
+ # Add the colon suffix to match Rake syntax expectations
420
+ args << "#{element.key.value}:"
421
+ end
422
+ end
230
423
  end
231
424
  end
425
+ args
232
426
  end
233
427
 
234
- private
235
-
428
+ # Records the first assignment of each name in a scope; later ones are skipped.
429
+ # The names already recorded are kept in memory, so this is one INSERT per new
430
+ # variable rather than a SELECT per assignment.
236
431
  def add_variable(name, line, column, scope = @current_scope)
237
- return if @shallow
238
-
239
- newvar = scope.variables.where(name:).first_or_create!(
240
- line:,
241
- column:,
242
- code_file: scope.code_file
243
- )
244
- if scope.top_line.blank?
245
- scope.top_line = line
246
- scope.save!
247
- end
248
- newvar
432
+ return if scope.nil?
433
+ return unless variable_names_in(scope).add?(name)
434
+
435
+ scope.variables.create!(name:, line:, column:, code_file_id: scope.code_file_id)
436
+ end
437
+
438
+ # Names of the variables recorded in scope. Scopes this parse created start empty.
439
+ # Only the root can already have rows (ScopeParser reuses an existing root), so its
440
+ # names are read from the database once.
441
+ def variable_names_in(scope)
442
+ @variable_names ||= {}
443
+ @variable_names[scope.id] ||= scope.root_scope? ? Set.new(scope.variables.pluck(:name)) : Set.new
249
444
  end
250
445
 
251
446
  def add_ivar(name, line, column)
252
447
  scope = @current_scope
448
+ return if scope.nil?
449
+
253
450
  unless scope == root_scope
254
451
  ivar_scope_types = [ScopeData::Base::TYPE_CLASS, ScopeData::Base::TYPE_MODULE]
255
452
  while !ivar_scope_types.include?(scope.class_type) && !scope.parent.nil?
@@ -259,35 +456,29 @@ module RubyLanguageServer
259
456
  add_variable(name, line, column, scope)
260
457
  end
261
458
 
262
- def add_scope(args, rest, type)
263
- (_, name, (line, column)) = args
264
- scope = push_scope(type, name, line, column)
265
- if type == ScopeData::Scope::TYPE_METHOD && @shallow
266
- process([])
267
- else
268
- process(rest)
269
- end
270
- pop_scope
271
- scope
459
+ # Runs the block with @in_singleton_class set, restoring it afterwards. A class or
460
+ # module nested inside class << self starts fresh.
461
+ def in_singleton_class(value)
462
+ saved = @in_singleton_class
463
+ @in_singleton_class = value
464
+ yield
465
+ ensure
466
+ @in_singleton_class = saved
272
467
  end
273
468
 
274
469
  def type_is_class_or_module(type)
275
470
  [RubyLanguageServer::ScopeData::Base::TYPE_CLASS, RubyLanguageServer::ScopeData::Base::TYPE_MODULE].include?(type)
276
471
  end
277
472
 
278
- def push_scope(type, name, top_line, column, close_siblings = true)
279
- close_sibling_scopes(top_line) if close_siblings
280
- new_scope = ScopeData::Scope.build(@current_scope, type, name, top_line, column)
281
- new_scope.bottom_line = @lines
282
- new_scope.save!
473
+ def push_scope(type, name, top_line, column, end_line, close_siblings = true) # rubocop:disable Metrics/ParameterLists
474
+ close_sibling_scopes if close_siblings
475
+ new_scope = ScopeData::Scope.build(@current_scope, type, name, top_line, column, end_line)
283
476
  @current_scope = new_scope
284
477
  end
285
478
 
286
- # This is a very poor man's "end" handler because there is no end handler.
287
- # The notion is that when you start the next scope, all the previous peers and unclosed descendents of the previous peer should be closed.
288
- def close_sibling_scopes(line)
289
- parent_scope = @current_scope
290
- parent_scope&.descendants&.each { |scope| scope.close(line) }
479
+ # Clean up any empty block scopes when starting a new sibling scope.
480
+ def close_sibling_scopes
481
+ @current_scope&.descendants&.each(&:close)
291
482
  end
292
483
 
293
484
  def pop_scope
@@ -295,21 +486,26 @@ module RubyLanguageServer
295
486
  end
296
487
  end
297
488
 
298
- # This class builds on Ripper's sexp processor to add ruby and rails magic.
489
+ # This class builds on Prism's AST processor to add ruby and rails magic.
299
490
  # Specifically it knows about things like alias, attr_*, has_one/many, etc.
300
491
  # It adds the appropriate definitions for those magic words.
301
- class ScopeParser < Ripper
492
+ class ScopeParser
302
493
  attr_reader :root_scope
303
494
 
304
- def initialize(text, shallow = false)
495
+ def initialize(text)
305
496
  text ||= '' # empty is the same as nil - but it doesn't crash
306
497
  begin
307
- sexp = self.class.sexp(text)
308
- rescue TypeError => e
309
- RubyLanguageServer.logger.error("Exception in sexp: #{e} for text: #{text}")
498
+ result = Prism.parse(text)
499
+ processor = PrismProcessor.new(text.split("\n").length)
500
+ processor.root_scope # Initialize root scope
501
+ result.value.accept(processor)
502
+ @root_scope = processor.root_scope
503
+ rescue StandardError => e
504
+ RubyLanguageServer.logger.error("Exception in prism parsing: #{e} for text: #{text}")
505
+ # Create an empty root scope on error
506
+ processor = PrismProcessor.new(text.split("\n").length)
507
+ @root_scope = processor.root_scope
310
508
  end
311
- processor = SEXPProcessor.new(sexp, text.split("\n").length, shallow)
312
- @root_scope = processor.root_scope
313
509
  end
314
510
  end
315
511
  end