Class: Solargraph::Pin::Callable
- Inherits:
-
Closure
- Object
- Pin::Base
- CompoundStatement
- Closure
- Solargraph::Pin::Callable
- Defined in:
- lib/solargraph/pin/callable.rb
Instance Attribute Summary collapse
- #block ⇒ Signature readonly
-
#parameters ⇒ Object
Returns the value of attribute parameters.
- #return_type ⇒ ComplexType? readonly
Attributes inherited from Closure
Attributes inherited from CompoundStatement
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
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
#parameters ⇒ Object
Returns the value of attribute parameters.
9 10 11 |
# File 'lib/solargraph/pin/callable.rb', line 9 def parameters @parameters end |
#return_type ⇒ ComplexType? (readonly)
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 # @todo this and its caller should be changed so that this can # look at the kwargs provided and check names against what # we acccept 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 # @param param [Pin::Parameter] # @param other_param [Pin::Parameter] 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 |
#combine_blocks(other) ⇒ Pin::Signature?
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 # @type [Pin::Signature, nil] 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 # @sg-ignore flow sensitive typing needs to handle attrs [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? # @sg-ignore Need to add nil check here @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 # @sg-ignore Need to add nil check here 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: {} # See # https://github.com/soutaro/steep/tree/master/lib/steep/type_inference # and # https://github.com/sorbet/sorbet/blob/master/infer/inference.cc # for other implementations 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 # erase anything unresolved 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 |
#transform_types {|| ... } ⇒ self
227 228 229 230 231 232 233 234 235 |
# File 'lib/solargraph/pin/callable.rb', line 227 def transform_types &transform # @todo 'super' alone should work here I think, but doesn't typecheck at level typed 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 = return_type return type.qualify(api_map, *gates) 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 |