Class: Solargraph::Pin::Method
- Inherits:
-
Callable
show all
- Includes:
- ParserGem::NodeMethods
- Defined in:
- lib/solargraph/pin/method.rb
Overview
The base class for method and attribute pins.
Constant Summary
Constants included
from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS
Instance Attribute Summary collapse
Attributes inherited from Callable
#parameters
Attributes inherited from Closure
#scope
Attributes inherited from Base
#code_object, #combine_priority, #directives, #docstring, #location, #name, #source, #type_location
Attributes included from Common
#context, #location
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#all_rooted? ⇒ Boolean
-
#anon_splat? ⇒ Boolean
-
#attribute? ⇒ Boolean
-
#block? ⇒ Boolean
-
#combine_visibility(other) ⇒ ::Symbol
-
#combine_with(other, attrs = {}) ⇒ Object
-
#completion_item_kind ⇒ Object
-
#detail ⇒ String?
-
#explicit? ⇒ Boolean
-
#generate_signature(parameters, return_type) ⇒ Signature
-
#initialize(visibility: :public, explicit: true, block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false, context: nil, **splat) ⇒ Method
constructor
A new instance of Method.
-
#inner_desc ⇒ Object
-
#method_name ⇒ String
-
#nearly?(other) ⇒ Boolean
-
#overloads ⇒ ::Array<Pin::Signature>
-
#path ⇒ Object
-
#probe(api_map) ⇒ Object
-
#proxy_with_signatures(return_type) ⇒ self
-
#reset_generated! ⇒ void
-
#resolve_ref_tag(api_map) ⇒ self
-
#rest_of_stack(api_map) ⇒ Array<Pin::Method>
-
#symbol_kind ⇒ Object
-
#to_rbs ⇒ Object
-
#transform_types(&transform) ⇒ Object
-
#typify(api_map) ⇒ Object
-
#with_single_signature(signature) ⇒ Pin::Method
Methods inherited from Callable
#arity, #arity_matches?, #blockless_parameters, #choose_parameters, #combine_blocks, #full_type_arity, #generics, #mandatory_positional_param_count, #parameter_names, #parameters_to_rbs, #resolve_generics_from_context, #resolve_generics_from_context_until_complete, #type_arity
Methods inherited from Closure
#context, #generic_defaults, #generics, #rbs_generics, #rebind
Methods inherited from Base
#all_location_text, #assert_location_provided, #assert_same, #assert_same_array_content, #assert_same_count, #assert_same_macros, #assert_source_provided, #best_location, #choose, #choose_longer, #choose_node, #choose_pin_attr, #choose_pin_attr_with_same_name, #choose_priority, #closure, #combine_closure, #combine_directives, #combine_name, #combine_return_type, #comments, #deprecated?, #desc, #dodgy_return_type_source?, #erase_generics, #filename, #gates, #identity, #infer, #inspect, #macros, #maybe_directives?, #needs_consistent_name?, #prefer_rbs_location, #presence_certain?, #probed?, #proxied?, #proxy, #rbs_location?, #realize, #resolve_generics, #resolve_generics_from_context, #to_s, #type_desc, #variable?
Methods included from Logging
log_level, logger
normalize_indentation, strip_html_comments
#completion_item, #deprecated?, #link_documentation, #probed?, #proxied?, #reset_conversions, #resolve_completion_item, #text_documentation
Methods included from Common
#binder, #closure, #closure=, #comments, #name, #namespace, #source
Constructor Details
#initialize(visibility: :public, explicit: true, block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false, context: nil, **splat) ⇒ Method
Returns a new instance of Method.
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/solargraph/pin/method.rb', line 27
def initialize visibility: :public, explicit: true, block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false,
context: nil, **splat
super(**splat)
@visibility = visibility
@explicit = explicit
@block = block
@node = node
@attribute = attribute
@signatures = signatures
@anon_splat = anon_splat
@context = context if context
end
|
Instance Attribute Details
@sg-ignore flow sensitive typing needs to remove literal with
this unless block
132
133
134
135
|
# File 'lib/solargraph/pin/method.rb', line 132
def block
return @block unless @block == :undefined
@block = signatures.first&.block
end
|
#documentation ⇒ Object
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# File 'lib/solargraph/pin/method.rb', line 300
def documentation
if @documentation.nil?
method_docs ||= super || ''
param_tags = docstring.tags(:param)
unless param_tags.nil? || param_tags.empty?
method_docs += "\n\n" unless method_docs.empty?
method_docs += "Params:\n"
lines = []
param_tags.each do |p|
l = "* #{p.name}"
l += " [#{escape_brackets(p.types.join(', '))}]" unless p.types.nil? || p.types.empty?
l += " #{p.text}"
lines.push l
end
method_docs += lines.join("\n")
end
yieldparam_tags = docstring.tags(:yieldparam)
unless yieldparam_tags.nil? || yieldparam_tags.empty?
method_docs += "\n\n" unless method_docs.empty?
method_docs += "Block Params:\n"
lines = []
yieldparam_tags.each do |p|
l = "* #{p.name}"
l += " [#{escape_brackets(p.types.join(', '))}]" unless p.types.nil? || p.types.empty?
l += " #{p.text}"
lines.push l
end
method_docs += lines.join("\n")
end
yieldreturn_tags = docstring.tags(:yieldreturn)
unless yieldreturn_tags.empty?
method_docs += "\n\n" unless method_docs.empty?
method_docs += "Block Returns:\n"
lines = []
yieldreturn_tags.each do |r|
l = '*'
l += " [#{escape_brackets(r.types.join(', '))}]" unless r.types.nil? || r.types.empty?
l += " #{r.text}"
lines.push l
end
method_docs += lines.join("\n")
end
return_tags = docstring.tags(:return)
unless return_tags.empty?
method_docs += "\n\n" unless method_docs.empty?
method_docs += "Returns:\n"
lines = []
return_tags.each do |r|
l = '*'
l += " [#{escape_brackets(r.types.join(', '))}]" unless r.types.nil? || r.types.empty?
l += " #{r.text}"
lines.push l
end
method_docs += lines.join("\n")
end
method_docs += "\n\n" unless method_docs.empty?
method_docs += "Visibility: #{visibility}"
@documentation = method_docs
concat_example_tags
end
@documentation.to_s
end
|
#node ⇒ Parser::AST::Node
16
17
18
|
# File 'lib/solargraph/pin/method.rb', line 16
def node
@node
end
|
#return_type ⇒ Object
145
146
147
|
# File 'lib/solargraph/pin/method.rb', line 145
def return_type
@return_type ||= ComplexType.new(signatures.map(&:return_type).flat_map(&:items))
end
|
#signature_help ⇒ ::Array<Hash>
237
238
239
240
241
242
243
244
|
# File 'lib/solargraph/pin/method.rb', line 237
def signature_help
@signature_help ||= signatures.map do |sig|
{
label: "#{name}(#{sig.parameters.map(&:full).join(', ')})",
documentation: documentation
}
end
end
|
#signatures ⇒ ::Array<Signature>
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/solargraph/pin/method.rb', line 190
def signatures
@signatures ||= begin
top_type = generate_complex_type
result = []
result.push generate_signature(parameters, top_type) if top_type.defined?
unless overloads.empty?
result.concat(overloads.map do |meth|
generate_signature(meth.parameters, meth.return_type)
end)
end
result.push generate_signature(parameters, @return_type || ComplexType::UNDEFINED) if result.empty?
result
end
end
|
#visibility ⇒ ::Symbol
Returns :public, :private, or :protected.
11
12
13
|
# File 'lib/solargraph/pin/method.rb', line 11
def visibility
@visibility
end
|
Instance Method Details
#==(other) ⇒ Object
77
78
79
|
# File 'lib/solargraph/pin/method.rb', line 77
def == other
super && other.node == node
end
|
#all_rooted? ⇒ Boolean
104
105
106
|
# File 'lib/solargraph/pin/method.rb', line 104
def all_rooted?
super && parameters.all?(&:all_rooted?) && (!block || block&.all_rooted?) && signatures.all?(&:all_rooted?)
end
|
#anon_splat? ⇒ Boolean
417
418
419
|
# File 'lib/solargraph/pin/method.rb', line 417
def anon_splat?
@anon_splat
end
|
#attribute? ⇒ Boolean
367
368
369
|
# File 'lib/solargraph/pin/method.rb', line 367
def attribute?
@attribute
end
|
#block? ⇒ Boolean
125
126
127
|
# File 'lib/solargraph/pin/method.rb', line 125
def block?
!block.nil?
end
|
#combine_visibility(other) ⇒ ::Symbol
42
43
44
45
46
47
48
49
50
|
# File 'lib/solargraph/pin/method.rb', line 42
def combine_visibility other
if dodgy_visibility_source? && !other.dodgy_visibility_source?
other.visibility
elsif other.dodgy_visibility_source? && !dodgy_visibility_source?
visibility
else
assert_same(other, :visibility)
end
end
|
#combine_with(other, attrs = {}) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/solargraph/pin/method.rb', line 52
def combine_with other, attrs = {}
priority_choice = choose_priority(other)
return priority_choice unless priority_choice.nil?
sigs = combine_signatures(other)
parameters = if sigs.length.positive?
[].freeze
else
choose(other, :parameters).clone.freeze
end
new_attrs = {
visibility: combine_visibility(other),
explicit: explicit? || other.explicit?,
block: combine_blocks(other),
node: choose_node(other, :node),
attribute: prefer_rbs_location(other, :attribute?),
parameters: parameters,
signatures: sigs,
anon_splat: assert_same(other, :anon_splat?),
return_type: nil }.merge(attrs)
super(other, new_attrs)
end
|
#completion_item_kind ⇒ Object
#detail ⇒ String?
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/solargraph/pin/method.rb', line 214
def detail
detail = String.new
detail += if signatures.length > 1
'(*) '
else
"(#{signatures.first.parameters.map(&:full).join(', ')}) " unless signatures.first.parameters.empty?
end.to_s
unless return_type.undefined?
detail += "=#{if probed?
'~'
else
(proxied? ? '^' : '>')
end} #{return_type}"
end
detail.strip!
return nil if detail.empty?
detail
end
|
#explicit? ⇒ Boolean
363
364
365
|
# File 'lib/solargraph/pin/method.rb', line 363
def explicit?
@explicit
end
|
#generate_signature(parameters, return_type) ⇒ Signature
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
|
# File 'lib/solargraph/pin/method.rb', line 152
def generate_signature parameters, return_type
block = nil
yieldparam_tags = docstring.tags(:yieldparam)
yieldreturn_tags = docstring.tags(:yieldreturn)
generics = docstring.tags(:generic).map(&:name)
needs_block_param_signature =
parameters.last&.block? || !yieldreturn_tags.empty? || !yieldparam_tags.empty?
if needs_block_param_signature
yield_parameters = yieldparam_tags.map do |p|
name = p.name
decl = :arg
if name
decl = select_decl(name, false)
name = clean_param(name)
end
Pin::Parameter.new(
location: location,
closure: self,
comments: p.text,
name: name,
decl: decl,
presence: location&.range,
return_type: ComplexType.try_parse(*p.types),
source: source
)
end
yield_return_type = ComplexType.try_parse(*yieldreturn_tags.flat_map(&:types))
block = Signature.new(generics: generics, parameters: yield_parameters, return_type: yield_return_type, source: source,
closure: self, location: location, type_location: type_location)
end
signature = Signature.new(generics: generics, parameters: parameters, return_type: return_type, block: block, closure: self, source: source,
location: location, type_location: type_location)
block.closure = signature if block
signature
end
|
#inner_desc ⇒ Object
246
247
248
249
250
251
252
253
|
# File 'lib/solargraph/pin/method.rb', line 246
def inner_desc
if signatures.length > 1
path + " \n#{to_rbs}\n"
else
super
end
end
|
#method_name ⇒ String
273
274
275
|
# File 'lib/solargraph/pin/method.rb', line 273
def method_name
name
end
|
#nearly?(other) ⇒ Boolean
372
373
374
375
376
377
378
379
380
|
# File 'lib/solargraph/pin/method.rb', line 372
def nearly? other
super &&
parameters == other.parameters &&
scope == other.scope &&
visibility == other.visibility
end
|
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/solargraph/pin/method.rb', line 387
def overloads
@overloads ||= docstring.tags(:overload).select(&:parameters).map do |tag|
Pin::Signature.new(
generics: generics,
parameters: tag.parameters.map do |src|
name, decl = parse_overload_param(src.first)
Pin::Parameter.new(
location: location,
closure: self,
comments: tag.docstring.all.to_s,
name: name,
decl: decl,
presence: location&.range,
return_type: param_type_from_name(tag, src.first),
source: :overloads
)
end,
closure: self,
return_type: ComplexType.try_parse(*tag.docstring.tags(:return).flat_map(&:types)),
source: :overloads
)
end
@overloads
end
|
#path ⇒ Object
268
269
270
|
# File 'lib/solargraph/pin/method.rb', line 268
def path
@path ||= "#{namespace}#{scope == :instance ? '#' : '.'}#{name}"
end
|
#probe(api_map) ⇒ Object
382
383
384
|
# File 'lib/solargraph/pin/method.rb', line 382
def probe api_map
attribute? ? infer_from_iv(api_map) : infer_from_return_nodes(api_map)
end
|
#proxy_with_signatures(return_type) ⇒ self
207
208
209
210
211
|
# File 'lib/solargraph/pin/method.rb', line 207
def proxy_with_signatures return_type
out = proxy return_type
out.signatures = out.signatures.map { |sig| sig.proxy return_type }
out
end
|
#reset_generated! ⇒ void
This method returns an undefined value.
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/solargraph/pin/method.rb', line 93
def reset_generated!
super
unless signatures.empty?
@block = :undefined
[]
end
block&.reset_generated!
@signatures&.each(&:reset_generated!)
nil
end
|
#resolve_ref_tag(api_map) ⇒ self
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
# File 'lib/solargraph/pin/method.rb', line 423
def resolve_ref_tag api_map
return self if @resolved_ref_tag
@resolved_ref_tag = true
return self unless docstring.ref_tags.any?
docstring.ref_tags.each do |tag|
ref = if tag.owner.to_s.start_with?(/[#.]/)
api_map.get_methods(namespace)
.select { |pin| pin.path.end_with?(tag.owner.to_s) }
.first
else
api_map.get_path_pins(tag.owner.to_s).first
end
next unless ref
docstring.add_tag(*ref.docstring.tags(:param))
end
self
end
|
#rest_of_stack(api_map) ⇒ Array<Pin::Method>
446
447
448
|
# File 'lib/solargraph/pin/method.rb', line 446
def rest_of_stack api_map
api_map.get_method_stack(method_namespace, method_name, scope: scope).reject { |pin| pin.path == path }
end
|
#to_rbs ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/solargraph/pin/method.rb', line 255
def to_rbs
return nil if signatures.empty?
rbs = "def #{name}: #{signatures.first.to_rbs}"
signatures[1..].each do |sig|
rbs += "\n"
rbs += (' ' * (4 + name.length))
rbs += "| #{name}: #{sig.to_rbs}"
end
rbs
end
|
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/solargraph/pin/method.rb', line 81
def transform_types &transform
m = super(&transform)
m.signatures = m.signatures.map do |sig|
sig.transform_types(&transform)
end
m.block = block&.transform_types(&transform)
m.reset_generated!
m
end
|
#typify(api_map) ⇒ Object
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/solargraph/pin/method.rb', line 277
def typify api_map
logger.debug do
"Method#typify(self=#{self}, binder=#{binder}, closure=#{closure}, context=#{context.rooted_tags}, return_type=#{return_type.rooted_tags}) - starting"
end
decl = super
unless decl.undefined?
logger.debug do
"Method#typify(self=#{self}, binder=#{binder}, closure=#{closure}, context=#{context}) => #{decl.rooted_tags.inspect} - decl found"
end
return decl
end
type = see_reference(api_map) || typify_from_super(api_map)
logger.debug { "Method#typify(self=#{self}) - type=#{type&.rooted_tags.inspect}" }
unless type.nil?
qualified = type.qualify(api_map, *closure.gates)
logger.debug { "Method#typify(self=#{self}) => #{qualified.rooted_tags.inspect}" }
return qualified
end
super
end
|
#with_single_signature(signature) ⇒ Pin::Method
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/solargraph/pin/method.rb', line 110
def with_single_signature signature
m = proxy signature.return_type
m.reset_generated!
m.parameters = signature.parameters
m.return_type = signature.return_type
m.block = signature.block
m.signatures = [signature]
m
end
|