Class: Solargraph::Pin::Callable
Constant Summary
Constants included
from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS
Instance Attribute Summary collapse
Attributes inherited from Closure
#scope
#node
Attributes inherited from Base
#code_object, #combine_priority, #directives, #docstring, #location, #name, #path, #source, #type_location
Attributes included from Common
#context, #location
Instance Method Summary
collapse
-
#arity ⇒ Array<Array<String>, String, nil>
e.g., [[“T”], “”, “?”, “foo:”] - parameter arity declarations, ignoring positional names.
-
#arity_matches?(arguments, with_block) ⇒ Boolean
-
#block? ⇒ Boolean
-
#blockless_parameters ⇒ Array<Pin::Parameter>
@sg-ignore Need to add nil check here.
-
#choose_parameters(other) ⇒ Array<Pin::Parameter>
-
#combine_blocks(other) ⇒ Pin::Signature?
-
#combine_with(other, attrs = {}) ⇒ self
-
#full_type_arity ⇒ Array<Array, String, nil>
Same as type_arity, but includes return type arity at the front.
-
#generics ⇒ Object
-
#initialize(block: nil, return_type: nil, parameters: [], **splat) ⇒ Callable
constructor
A new instance of Callable.
-
#mandatory_positional_param_count ⇒ Integer
-
#method_name ⇒ String
@sg-ignore Need to add nil check here.
-
#method_namespace ⇒ String
@sg-ignore Need to add nil check here.
-
#parameter_names ⇒ ::Array<String>
-
#parameters_to_rbs ⇒ String
-
#reset_generated! ⇒ Object
-
#resolve_generics_from_context(generics_to_resolve, arg_types = nil, return_type_context = nil, yield_arg_types = nil, yield_return_type_context = nil, resolved_generic_values: {}) ⇒ self
-
#resolve_generics_from_context_until_complete(generics_to_resolve, arg_types = nil, return_type_context = nil, yield_arg_types = nil, yield_return_type_context = nil, resolved_generic_values: {}) ⇒ self
-
#to_rbs ⇒ Object
-
#transform_types {|| ... } ⇒ self
-
#type_arity ⇒ Array<Array, String, nil>
e.g., [[“T”], “1”, “?3”, “foo:5”] - parameter arity declarations, including the number of unique types in each parameter.
-
#typify(api_map) ⇒ Object
Methods inherited from Closure
#context, #generic_defaults, #rbs_generics, #rebind
Methods inherited from Base
#==, #all_location_text, #all_rooted?, #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, #completion_item_kind, #deprecated?, #desc, #dodgy_return_type_source?, #erase_generics, #filename, #gates, #identity, #infer, #inner_desc, #inspect, #macros, #maybe_directives?, #nearly?, #needs_consistent_name?, #prefer_rbs_location, #presence_certain?, #probe, #probed?, #proxied?, #proxy, #rbs_location?, #realize, #resolve_generics, #symbol_kind, #to_s, #type_desc, #variable?
Methods included from Logging
log_level, logger
#documentation, normalize_indentation, strip_html_comments
#completion_item, #completion_item_kind, #deprecated?, #detail, #link_documentation, #probed?, #proxied?, #reset_conversions, #resolve_completion_item, #signature_help, #text_documentation
Methods included from Common
#binder, #closure, #closure=, #comments, #name, #namespace, #path, #source
Constructor Details
#initialize(block: nil, return_type: nil, parameters: [], **splat) ⇒ Callable
Returns a new instance of Callable.
18
19
20
21
22
23
|
# File 'lib/solargraph/pin/callable.rb', line 18
def initialize block: nil, return_type: nil, parameters: [], **splat
super(**splat)
@block = block
@return_type = return_type
@parameters = parameters
end
|
Instance Attribute Details
7
8
9
|
# File 'lib/solargraph/pin/callable.rb', line 7
def block
@block
end
|
#parameters ⇒ Object
Returns the value of attribute parameters.
9
10
11
|
# File 'lib/solargraph/pin/callable.rb', line 9
def parameters
@parameters
end
|
12
13
14
|
# File 'lib/solargraph/pin/callable.rb', line 12
def return_type
@return_type
end
|
Instance Method Details
#arity ⇒ Array<Array<String>, String, nil>
e.g., [[“T”], “”, “?”, “foo:”] - parameter arity declarations,
ignoring positional names. Used to match signatures.
107
108
109
|
# File 'lib/solargraph/pin/callable.rb', line 107
def arity
[generics, blockless_parameters.map(&:arity_decl), block&.arity]
end
|
#arity_matches?(arguments, with_block) ⇒ Boolean
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/solargraph/pin/callable.rb', line 240
def arity_matches? arguments, with_block
argcount = arguments.length
parcount = mandatory_positional_param_count
parcount -= 1 if !parameters.empty? && parameters.last.block?
return false if block? && !with_block
return false if argcount < parcount && !(argcount == parcount - 1 && parameters.last.restarg?)
true
end
|
#block? ⇒ Boolean
266
267
268
|
# File 'lib/solargraph/pin/callable.rb', line 266
def block?
!!@block
end
|
#blockless_parameters ⇒ Array<Pin::Parameter>
@sg-ignore Need to add nil check here
95
96
97
98
99
100
101
|
# File 'lib/solargraph/pin/callable.rb', line 95
def blockless_parameters
if parameters.last&.block?
parameters[0..-2]
else
parameters
end
end
|
#choose_parameters(other) ⇒ Array<Pin::Parameter>
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/solargraph/pin/callable.rb', line 76
def choose_parameters other
if other.arity != arity
raise "Trying to combine two pins with different arities - \nself =#{inspect}, \nother=#{other.inspect}, \n\n self.arity=#{arity}, \nother.arity=#{other.arity}"
end
parameters.zip(other.parameters).map do |param, other_param|
if param.nil? && other_param.block?
other_param
elsif other_param.nil? && param.block?
param
else
param.combine_with(other_param)
end
end
end
|
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/solargraph/pin/callable.rb', line 40
def combine_blocks other
if block.nil?
other.block
elsif other.block.nil?
block
else
choose_pin_attr(other, :block)
end
end
|
#combine_with(other, attrs = {}) ⇒ self
55
56
57
58
59
60
61
62
|
# File 'lib/solargraph/pin/callable.rb', line 55
def combine_with other, attrs = {}
new_attrs = {
block: combine_blocks(other),
return_type: combine_return_type(other)
}.merge(attrs)
new_attrs[:parameters] = choose_parameters(other).clone.freeze unless new_attrs.key?(:parameters)
super(other, new_attrs)
end
|
#full_type_arity ⇒ Array<Array, String, nil>
Same as type_arity, but includes return type arity at the front.
125
126
127
128
|
# File 'lib/solargraph/pin/callable.rb', line 125
def full_type_arity
[return_type ? return_type.items.count.to_s : nil] + type_arity
end
|
#generics ⇒ Object
69
70
71
|
# File 'lib/solargraph/pin/callable.rb', line 69
def generics
[]
end
|
#mandatory_positional_param_count ⇒ Integer
253
254
255
|
# File 'lib/solargraph/pin/callable.rb', line 253
def mandatory_positional_param_count
parameters.count(&:arg?)
end
|
#method_name ⇒ String
@sg-ignore Need to add nil check here
177
178
179
180
181
|
# File 'lib/solargraph/pin/callable.rb', line 177
def method_name
raise "closure was nil in #{inspect}" if closure.nil?
@method_name ||= closure.name
end
|
#method_namespace ⇒ String
@sg-ignore Need to add nil check here
32
33
34
35
|
# File 'lib/solargraph/pin/callable.rb', line 32
def method_namespace
closure.namespace
end
|
#parameter_names ⇒ ::Array<String>
65
66
67
|
# File 'lib/solargraph/pin/callable.rb', line 65
def parameter_names
@parameter_names ||= parameters.map(&:name)
end
|
#parameters_to_rbs ⇒ String
258
259
260
|
# File 'lib/solargraph/pin/callable.rb', line 258
def parameters_to_rbs
"#{rbs_generics}(#{parameters.map(&:to_rbs).join(', ')}) #{"{ #{block.to_rbs} } " unless block.nil?}"
end
|
#reset_generated! ⇒ Object
25
26
27
28
|
# File 'lib/solargraph/pin/callable.rb', line 25
def reset_generated!
parameters.each(&:reset_generated!)
super
end
|
#resolve_generics_from_context(generics_to_resolve, arg_types = nil, return_type_context = nil, yield_arg_types = nil, yield_return_type_context = nil, resolved_generic_values: {}) ⇒ self
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/solargraph/pin/callable.rb', line 138
def resolve_generics_from_context generics_to_resolve,
arg_types = nil,
return_type_context = nil,
yield_arg_types = nil,
yield_return_type_context = nil,
resolved_generic_values: {}
callable = super(generics_to_resolve, return_type_context, resolved_generic_values: resolved_generic_values)
callable.parameters = callable.parameters.each_with_index.map do |param, i|
if arg_types.nil?
param.dup
else
param.resolve_generics_from_context(generics_to_resolve,
arg_types[i],
resolved_generic_values: resolved_generic_values)
end
end
if callable.block?
callable.block = block.resolve_generics_from_context(generics_to_resolve,
yield_arg_types,
yield_return_type_context,
resolved_generic_values: resolved_generic_values)
end
callable
end
|
#resolve_generics_from_context_until_complete(generics_to_resolve, arg_types = nil, return_type_context = nil, yield_arg_types = nil, yield_return_type_context = nil, resolved_generic_values: {}) ⇒ self
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
|
# File 'lib/solargraph/pin/callable.rb', line 191
def resolve_generics_from_context_until_complete generics_to_resolve,
arg_types = nil,
return_type_context = nil,
yield_arg_types = nil,
yield_return_type_context = nil,
resolved_generic_values: {}
return self if generics_to_resolve.empty?
last_resolved_generic_values = resolved_generic_values.dup
new_pin = resolve_generics_from_context(generics_to_resolve,
arg_types,
return_type_context,
yield_arg_types,
yield_return_type_context,
resolved_generic_values: resolved_generic_values)
if last_resolved_generic_values == resolved_generic_values
return new_pin.erase_generics(generics)
end
new_pin.resolve_generics_from_context_until_complete(generics_to_resolve,
arg_types,
return_type_context,
yield_arg_types,
yield_return_type_context,
resolved_generic_values: resolved_generic_values)
end
|
#to_rbs ⇒ Object
262
263
264
|
# File 'lib/solargraph/pin/callable.rb', line 262
def to_rbs
"#{parameters_to_rbs}-> #{return_type&.to_rbs || 'untyped'}"
end
|
227
228
229
230
231
232
233
234
235
|
# File 'lib/solargraph/pin/callable.rb', line 227
def transform_types &transform
callable = super(&transform)
callable.block = block.transform_types(&transform) if block?
callable.parameters = parameters.map do |param|
param.transform_types(&transform)
end
callable
end
|
#type_arity ⇒ Array<Array, String, nil>
e.g., [[“T”], “1”, “?3”, “foo:5”] - parameter arity
declarations, including the number of unique types in each
parameter. Used to determine whether combining two
signatures has lost useful information mapping specific
parameter types to specific return types.
118
119
120
|
# File 'lib/solargraph/pin/callable.rb', line 118
def type_arity
[generics, blockless_parameters.map(&:type_arity_decl), block&.type_arity]
end
|
#typify(api_map) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/solargraph/pin/callable.rb', line 163
def typify api_map
type = super
return type if type.defined?
if method_name.end_with?('?')
logger.debug { "Callable#typify(self=#{self}) => Boolean (? suffix)" }
ComplexType::BOOLEAN
else
logger.debug { "Callable#typify(self=#{self}) => undefined" }
ComplexType::UNDEFINED
end
end
|