81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/solargraph/source_map/mapper.rb', line 81
def process node, tree = [], visibility = :public, scope = :instance, fqn = '', stack = []
return unless node.is_a?(AST::Node)
return if node.type == :str
stack.push node
if node.kind_of?(AST::Node)
@node_stack.unshift node
if node.type == :class or node.type == :module
visibility = :public
if node.children[0].kind_of?(AST::Node) and node.children[0].children[0].kind_of?(AST::Node) and node.children[0].children[0].type == :cbase
tree = pack_name(node.children[0])
tree.shift if tree.first.empty?
else
tree = tree + pack_name(node.children[0])
end
fqn = tree.join('::')
sc = nil
if node.type == :class and !node.children[1].nil?
sc = unpack_name(node.children[1])
end
pins.push Solargraph::Pin::Namespace.new(get_node_location(node), tree[0..-2].join('::') || '', pack_name(node.children[0]).last.to_s, (node), node.type, visibility)
pins.push Pin::Reference::Superclass.new(pins.last.location, pins.last.path, sc) unless sc.nil?
end
file = filename
node.children.each do |c|
if c.kind_of?(AST::Node)
if c.type == :ivasgn
here = get_node_start_position(c)
named_path = get_named_path_pin(here)
if c.children[1].nil?
ora = find_parent(stack, :or_asgn)
unless ora.nil?
u = c.updated(:ivasgn, c.children + ora.children[1..-1], nil)
pins.push Solargraph::Pin::InstanceVariable.new(get_node_location(u), fqn || '', c.children[0].to_s, (u), u.children[1], infer_literal_node_type(u.children[1]), named_path.context)
if visibility == :module_function and named_path.kind == Pin::METHOD
other = ComplexType.parse("Module<#{named_path.context.namespace}>")
pins.push Solargraph::Pin::InstanceVariable.new(get_node_location(u), fqn || '', c.children[0].to_s, (u), u.children[1], infer_literal_node_type(u.children[1]), other) end
end
else
pins.push Solargraph::Pin::InstanceVariable.new(get_node_location(c), fqn || '',c.children[0].to_s, (c), c.children[1], infer_literal_node_type(c.children[1]), named_path.context)
if visibility == :module_function and named_path.kind == Pin::METHOD
other = ComplexType.parse("Module<#{named_path.context.namespace}>")
pins.push Solargraph::Pin::InstanceVariable.new(get_node_location(c), fqn || '',c.children[0].to_s, (c), c.children[1], infer_literal_node_type(c.children[1]), other)
end
end
elsif c.type == :cvasgn
here = get_node_start_position(c)
context = get_named_path_pin(here)
if c.children[1].nil?
ora = find_parent(stack, :or_asgn)
unless ora.nil?
u = c.updated(:cvasgn, c.children + ora.children[1..-1], nil)
pins.push Solargraph::Pin::ClassVariable.new(get_node_location(u), fqn || '', c.children[0].to_s, (u), u.children[1], infer_literal_node_type(u.children[1]), context.context)
end
else
pins.push Solargraph::Pin::ClassVariable.new(get_node_location(c), fqn || '', c.children[0].to_s, (c), c.children[1], infer_literal_node_type(c.children[1]), context.context)
end
elsif c.type == :lvasgn
here = get_node_start_position(c)
context = get_named_path_pin(here)
block = get_block_pin(here)
presence = Range.new(here, block.location.range.ending)
if c.children[1].nil?
ora = find_parent(stack, :or_asgn)
unless ora.nil?
u = c.updated(:lvasgn, c.children + ora.children[1..-1], nil)
@locals.push Solargraph::Pin::LocalVariable.new(get_node_location(u), fqn, u.children[0].to_s, (ora), u.children[1], infer_literal_node_type(c.children[1]), context.context, block, presence)
end
else
@locals.push Solargraph::Pin::LocalVariable.new(get_node_location(c), fqn, c.children[0].to_s, (c), c.children[1], infer_literal_node_type(c.children[1]), context.context, block, presence)
end
elsif c.type == :gvasgn
if c.children[1].nil?
ora = find_parent(stack, :or_asgn)
unless ora.nil?
u = c.updated(:gvasgn, c.children + ora.children[1..-1], nil)
pins.push Solargraph::Pin::GlobalVariable.new(get_node_location(c), fqn, u.children[0].to_s, (c), u.children[1], infer_literal_node_type(c.children[1]), @pins.first.context)
end
else
pins.push Solargraph::Pin::GlobalVariable.new(get_node_location(c), fqn, c.children[0].to_s, (c), c.children[1], infer_literal_node_type(c.children[1]), @pins.first.context)
end
elsif c.type == :sym
pins.push Solargraph::Pin::Symbol.new(get_node_location(c), ":#{c.children[0]}")
elsif c.type == :casgn
here = get_node_start_position(c)
block = get_block_pin(here)
pins.push Solargraph::Pin::Constant.new(get_node_location(c), fqn, c.children[1].to_s, (c), c.children[2], infer_literal_node_type(c.children[2]), block.context, :public)
elsif c.type == :def
methpin = Solargraph::Pin::Method.new(get_node_location(c), fqn || '', c.children[(c.type == :def ? 0 : 1)].to_s, (c), scope, visibility, get_method_args(c))
if methpin.name == 'initialize' and methpin.scope == :instance
pins.push Solargraph::Pin::Method.new(methpin.location, methpin.namespace, 'new', methpin., :class, :public, methpin.parameters)
pins.last.instance_variable_set(:@return_complex_type, ComplexType.parse(methpin.namespace))
pins.push Solargraph::Pin::Method.new(methpin.location, methpin.namespace, methpin.name, methpin., methpin.scope, :private, methpin.parameters)
elsif visibility == :module_function
pins.push Solargraph::Pin::Method.new(methpin.location, methpin.namespace, methpin.name, methpin., :class, :public, methpin.parameters)
pins.push Solargraph::Pin::Method.new(methpin.location, methpin.namespace, methpin.name, methpin., :instance, :private, methpin.parameters)
else
pins.push methpin
end
elsif c.type == :defs
s_visi = visibility
s_visi = :public if s_visi == :module_function or scope != :class
if c.children[0].is_a?(AST::Node) and c.children[0].type == :self
dfqn = fqn || ''
else
dfqn = unpack_name(c.children[0])
end
unless dfqn.nil?
pins.push Solargraph::Pin::Method.new(get_node_location(c), dfqn, "#{c.children[(node.type == :def ? 0 : 1)]}", (c), :class, s_visi, get_method_args(c))
process c, tree, scope, :class, dfqn, stack
end
next
elsif c.type == :send and [:public, :protected, :private].include?(c.children[1])
visibility = c.children[1]
elsif c.type == :send and [:private_class_method].include?(c.children[1]) and c.children[2].kind_of?(AST::Node)
if c.children[2].type == :sym or c.children[2].type == :str
ref = pins.select{|p| p.namespace == (fqn || '') and p.name == c.children[2].children[0].to_s}.first
unless ref.nil?
pins.delete ref
pins.push Solargraph::Pin::Method.new(ref.location, ref.namespace, ref.name, ref., ref.scope, :private, ref.parameters)
end
else
process c, tree, :private, :class, fqn, stack
next
end
elsif c.type == :send and [:private_constant].include?(c.children[1]) and c.children[2].kind_of?(AST::Node)
if c.children[2].type == :sym or c.children[2].type == :str
cn = c.children[2].children[0].to_s
ref = pins.select{|p| p.namespace == (fqn || '') and p.name == cn}.first
unless ref.nil?
pins.delete ref
if ref.kind == Pin::CONSTANT
pins.push ref.class.new(ref.location, ref.namespace, ref.name, ref., ref.signature, ref.return_type, ref.context, :private)
else
pins.push ref.class.new(ref.location, ref.namespace, ref.name, ref., ref.type, :private)
end
end
end
next
elsif c.type == :send and c.children[1] == :module_function
if c.children[2].nil?
visibility = :module_function
elsif c.children[2].type == :sym or c.children[2].type == :str
c.children[2..-1].each do |x|
cn = x.children[0].to_s
ref = pins.select{|p| p.namespace == (fqn || '') and p.name == cn}.first
unless ref.nil?
pins.delete ref
mm = Solargraph::Pin::Method.new(ref.location, ref.namespace, ref.name, ref., :class, :public, ref.parameters)
cm = Solargraph::Pin::Method.new(ref.location, ref.namespace, ref.name, ref., :instance, :private, ref.parameters)
pins.push mm, cm
pins.select{|pin| pin.kind == Pin::INSTANCE_VARIABLE and pin.context == ref.context}.each do |ivar|
pins.delete ivar
pins.push Solargraph::Pin::InstanceVariable.new(ivar.location, ivar.namespace, ivar.name, ivar., ivar.signature, ivar.instance_variable_get(:@literal), mm)
pins.push Solargraph::Pin::InstanceVariable.new(ivar.location, ivar.namespace, ivar.name, ivar., ivar.signature, ivar.instance_variable_get(:@literal), cm)
end
end
end
elsif c.children[2].type == :def
process c, tree, :module_function, :class, fqn, stack
next
end
elsif c.type == :send and c.children[1] == :include and c.children[0].nil?
last_node = get_last_in_stack_not_begin(stack)
if last_node.nil? or last_node.type == :class or last_node.type == :module or last_node.type == :source
if c.children[2].kind_of?(AST::Node) and c.children[2].type == :const
c.children[2..-1].each do |i|
nspin = @pins.select{|pin| pin.kind == Pin::NAMESPACE and pin.path == fqn}.last
unless nspin.nil?
pins.push Pin::Reference::Include.new(get_node_location(c), nspin.path, unpack_name(i))
end
end
end
end
elsif c.type == :send and c.children[1] == :extend and c.children[0].nil?
last_node = get_last_in_stack_not_begin(stack)
if last_node.nil? or last_node.type == :class or last_node.type == :module or last_node.type == :source
c.children[2..-1].each do |i|
nspin = @pins.select{|pin| pin.kind == Pin::NAMESPACE and pin.path == fqn}.last
unless nspin.nil?
ref = nil
if i.type == :self
ref = Pin::Reference::Extend.new(get_node_location(c), nspin.path, nspin.path)
elsif i.type == :const
ref = Pin::Reference::Extend.new(get_node_location(c), nspin.path, unpack_name(i))
end
pins.push ref unless ref.nil?
end
end
end
elsif c.type == :send && [:attr_reader, :attr_writer, :attr_accessor].include?(c.children[1])
c.children[2..-1].each do |a|
if c.children[1] == :attr_reader or c.children[1] == :attr_accessor
pins.push Solargraph::Pin::Attribute.new(get_node_location(c), fqn || '', "#{a.children[0]}", (c), :reader, scope, visibility)
end
if c.children[1] == :attr_writer or c.children[1] == :attr_accessor
pins.push Solargraph::Pin::Attribute.new(get_node_location(c), fqn || '', "#{a.children[0]}=", (c), :writer, scope, visibility)
end
end
elsif c.type == :alias
pin = pins.select{|p| p.name == c.children[1].children[0].to_s && p.namespace == fqn && p.scope == scope}.first
unless pin.nil?
if pin.is_a?(Solargraph::Pin::Method)
pins.push Solargraph::Pin::Method.new(get_node_location(c), pin.namespace, c.children[0].children[0].to_s, (c) || pin., pin.scope, pin.visibility, pin.parameters)
elsif pin.is_a?(Solargraph::Pin::Attribute)
pins.push Solargraph::Pin::Attribute.new(get_node_location(c), pin.namespace, c.children[0].children[0].to_s, (c) || pin., pin.access, pin.scope, pin.visibility)
end
end
elsif c.type == :send && c.children[1] == :alias_method && c.children[2] && c.children[2] && c.children[2].type == :sym && c.children[3] && c.children[3].type == :sym
pin = pins.select{|p| p.name == c.children[3].children[0].to_s && p.namespace == fqn && p.scope == scope}.first
unless pin.nil?
if pin.is_a?(Solargraph::Pin::Method)
pins.push Solargraph::Pin::Method.new(get_node_location(c), pin.namespace, c.children[2].children[0].to_s, (c) || pin., pin.scope, pin.visibility, pin.parameters)
elsif pin.is_a?(Solargraph::Pin::Attribute)
pins.push Solargraph::Pin::Attribute.new(get_node_location(c), pin.namespace, c.children[2].children[0].to_s, (c) || pin., pin.access, pin.scope, pin.visibility)
end
end
elsif c.type == :sclass && c.children[0].type == :self
process c, tree, :public, :class, fqn || '', stack
next
elsif c.type == :send && c.children[1] == :require
if c.children[2].kind_of?(AST::Node) and c.children[2].type == :str
pins.push Pin::Reference::Require.new(get_node_location(c), c.children[2].children[0].to_s)
end
elsif c.type == :args
if @node_stack.first.type == :block
pi = 0
c.children.each do |u|
here = get_node_start_position(c)
blk = get_block_pin(here)
@locals.push Solargraph::Pin::BlockParameter.new(get_node_location(u), fqn || '', "#{u.children[0]}", (c), blk)
blk.parameters.push @locals.push.last
pi += 1
end
else
c.children.each do |u|
here = get_node_start_position(u)
context = get_named_path_pin(here)
block = get_block_pin(here)
presence = Range.new(here, block.location.range.ending)
@locals.push Solargraph::Pin::MethodParameter.new(get_node_location(u), fqn, u.children[0].to_s, (c), resolve_node_signature(u.children[1]), infer_literal_node_type(u.children[1]), context, block, presence)
end
end
elsif c.type == :block
here = get_node_start_position(c)
named_path = get_named_path_pin(here)
@pins.push Solargraph::Pin::Block.new(get_node_location(c), fqn || '', '', (c), c.children[0], named_path.context)
end
process c, tree, visibility, scope, fqn, stack
end
end
@node_stack.shift
end
stack.pop
end
|