Class: Solargraph::Pin::Callable

Inherits:
Closure show all
Defined in:
lib/solargraph/pin/callable.rb

Direct Known Subclasses

Block, Method, Signature

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Closure

#scope

Attributes inherited from CompoundStatement

#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

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

Methods included from Documenting

#documentation, normalize_indentation, strip_html_comments

Methods included from Conversions

#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.

Parameters:



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

#blockSignature

Returns:



7
8
9
# File 'lib/solargraph/pin/callable.rb', line 7

def block
  @block
end

#parametersObject

Returns the value of attribute parameters.



9
10
11
# File 'lib/solargraph/pin/callable.rb', line 9

def parameters
  @parameters
end

#return_typeComplexType? (readonly)

Returns:



12
13
14
# File 'lib/solargraph/pin/callable.rb', line 12

def return_type
  @return_type
end

Instance Method Details

#arityArray<Array<String>, String, nil>

e.g., [[“T”], “”, “?”, “foo:”] - parameter arity declarations,

ignoring positional names.  Used to match signatures.

Returns:

  • (Array<Array<String>, String, nil>)


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

Parameters:

  • arguments (::Array<Chain>)
  • with_block (Boolean)

Returns:

  • (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

Returns:

  • (Boolean)


266
267
268
# File 'lib/solargraph/pin/callable.rb', line 266

def block?
  !!@block
end

#blockless_parametersArray<Pin::Parameter>

@sg-ignore Need to add nil check here

Returns:



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>

Parameters:

  • other (self)

Returns:



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?

Parameters:

  • other (self)

Returns:



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

Parameters:

  • other (self)
  • attrs (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (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_arityArray<Array, String, nil>

Same as type_arity, but includes return type arity at the front.

Returns:

  • (Array<Array, String, nil>)


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

#genericsObject



69
70
71
# File 'lib/solargraph/pin/callable.rb', line 69

def generics
  []
end

#mandatory_positional_param_countInteger

Returns:

  • (Integer)


253
254
255
# File 'lib/solargraph/pin/callable.rb', line 253

def mandatory_positional_param_count
  parameters.count(&:arg?)
end

#method_nameString

@sg-ignore Need to add nil check here

Returns:

  • (String)


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_namespaceString

@sg-ignore Need to add nil check here

Returns:

  • (String)


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>

Returns:

  • (::Array<String>)


65
66
67
# File 'lib/solargraph/pin/callable.rb', line 65

def parameter_names
  @parameter_names ||= parameters.map(&:name)
end

#parameters_to_rbsString

Returns:

  • (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

Parameters:

  • generics_to_resolve (Enumerable<String>)
  • arg_types (Array<ComplexType>, nil) (defaults to: nil)
  • return_type_context (ComplexType, nil) (defaults to: nil)
  • yield_arg_types (Array<ComplexType>, nil) (defaults to: nil)
  • yield_return_type_context (ComplexType, nil) (defaults to: nil)
  • resolved_generic_values (Hash{String => ComplexType}) (defaults to: {})

Returns:

  • (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

Parameters:

  • generics_to_resolve (::Array<String>)
  • arg_types (Array<ComplexType>, nil) (defaults to: nil)
  • return_type_context (ComplexType, nil) (defaults to: nil)
  • yield_arg_types (Array<ComplexType>, nil) (defaults to: nil)
  • yield_return_type_context (ComplexType, nil) (defaults to: nil)
  • resolved_generic_values (Hash{String => ComplexType}) (defaults to: {})

Returns:

  • (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_rbsObject



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

Yield Parameters:

Yield Returns:

Returns:

  • (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_arityArray<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.

Returns:

  • (Array<Array, String, nil>)


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