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.
- checksums.yaml +4 -4
- data/CHANGELOG.txt +77 -0
- data/FAQ_ROADMAP.md +1 -2
- data/Gemfile.lock +226 -70
- data/Guardfile +1 -1
- data/Makefile +38 -13
- data/README.md +77 -7
- data/bin/rdbg +8 -0
- data/bin/run_in_shell +5 -0
- data/exe/ruby_language_server +1 -1
- data/lib/config/initializers/active_record.rb +16 -16
- data/lib/db/schema.rb +5 -1
- data/lib/ruby_language_server/application.rb +3 -4
- data/lib/ruby_language_server/code_file.rb +41 -16
- data/lib/ruby_language_server/completion.rb +92 -13
- data/lib/ruby_language_server/hover.rb +59 -0
- data/lib/ruby_language_server/io.rb +184 -100
- data/lib/ruby_language_server/line_context.rb +14 -0
- data/lib/ruby_language_server/project_manager.rb +330 -131
- data/lib/ruby_language_server/scope_data/base.rb +12 -4
- data/lib/ruby_language_server/scope_data/scope.rb +65 -14
- data/lib/ruby_language_server/scope_parser.rb +399 -203
- data/lib/ruby_language_server/scope_parser_commands/rails_commands.rb +7 -8
- data/lib/ruby_language_server/scope_parser_commands/rake_commands.rb +16 -20
- data/lib/ruby_language_server/scope_parser_commands/rspec_commands.rb +24 -13
- data/lib/ruby_language_server/scope_parser_commands/ruby_commands.rb +16 -42
- data/lib/ruby_language_server/server.rb +52 -29
- data/lib/ruby_language_server/version.rb +1 -1
- data/ruby_language_server.gemspec +7 -3
- metadata +64 -6
- data/lib/ruby_language_server/gem_installer.rb +0 -38
|
@@ -1,255 +1,452 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require '
|
|
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
|
|
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
|
|
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
|
-
|
|
21
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
#
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
|
|
51
|
+
in_singleton_class(false) { super }
|
|
52
|
+
pop_scope
|
|
75
53
|
end
|
|
76
54
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
|
90
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
104
|
-
|
|
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
|
-
(
|
|
107
|
-
|
|
128
|
+
add_variable(name, line, column)
|
|
129
|
+
super
|
|
108
130
|
end
|
|
109
131
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
#
|
|
136
|
-
def
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
148
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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?
|
|
181
|
-
|
|
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}
|
|
376
|
+
RubyLanguageServer.logger.debug("We don't have a #{method_name}")
|
|
184
377
|
end
|
|
378
|
+
end
|
|
185
379
|
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
first_arg = args && args[0] == :args && args[1]
|
|
384
|
+
def command_column(node)
|
|
385
|
+
column_of(node.location)
|
|
386
|
+
end
|
|
203
387
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
|
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
|
-
#
|
|
287
|
-
|
|
288
|
-
|
|
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
|
|
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
|
|
492
|
+
class ScopeParser
|
|
302
493
|
attr_reader :root_scope
|
|
303
494
|
|
|
304
|
-
def initialize(text
|
|
495
|
+
def initialize(text)
|
|
305
496
|
text ||= '' # empty is the same as nil - but it doesn't crash
|
|
306
497
|
begin
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|