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

#binder, #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_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, #reset_generated!, #resolve_generics, #symbol_kind, #to_s, #type_desc, #variable?

Methods included from Logging

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, #comments, #name, #namespace, #path, #source

Constructor Details

#initialize(block: nil, return_type: nil, parameters: [], **splat) ⇒ Callable

Returns a new instance of Callable.

Parameters:



17
18
19
20
21
22
# File 'lib/solargraph/pin/callable.rb', line 17

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

Returns:

  • (Array)


93
94
95
# File 'lib/solargraph/pin/callable.rb', line 93

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)


204
205
206
207
208
209
210
211
# File 'lib/solargraph/pin/callable.rb', line 204

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

Returns:

  • (Boolean)


222
223
224
# File 'lib/solargraph/pin/callable.rb', line 222

def block?
  !!@block
end

#blockless_parametersArray<Pin::Parameter>

Returns:



84
85
86
87
88
89
90
# File 'lib/solargraph/pin/callable.rb', line 84

def blockless_parameters
  if parameters.last&.block?
    parameters[0..-2]
  else
    parameters
  end
end

#choose_parameters(other) ⇒ Array<Pin::Parameter>

Parameters:

  • other (self)

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/solargraph/pin/callable.rb', line 68

def choose_parameters(other)
  raise "Trying to combine two pins with different arities - \nself =#{inspect}, \nother=#{other.inspect}, \n\n self.arity=#{self.arity}, \nother.arity=#{other.arity}" if other.arity != arity
  # @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:



32
33
34
35
36
37
38
39
40
41
# File 'lib/solargraph/pin/callable.rb', line 32

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)


47
48
49
50
51
52
53
54
# File 'lib/solargraph/pin/callable.rb', line 47

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

#genericsObject



61
62
63
# File 'lib/solargraph/pin/callable.rb', line 61

def generics
  []
end

#mandatory_positional_param_countInteger

Returns:

  • (Integer)


214
215
216
# File 'lib/solargraph/pin/callable.rb', line 214

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

#method_nameString

Returns:

  • (String)


141
142
143
144
# File 'lib/solargraph/pin/callable.rb', line 141

def method_name
  raise "closure was nil in #{self.inspect}" if closure.nil?
  @method_name ||= closure.name
end

#method_namespaceString

Returns:

  • (String)


25
26
27
# File 'lib/solargraph/pin/callable.rb', line 25

def method_namespace
  closure.namespace
end

#parameter_names::Array<String>

Returns:

  • (::Array<String>)


57
58
59
# File 'lib/solargraph/pin/callable.rb', line 57

def parameter_names
  @parameter_names ||= parameters.map(&:name)
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)
  • context (ComplexType, nil)
  • resolved_generic_values (Hash{String => ComplexType}) (defaults to: {})

Returns:

  • (self)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/solargraph/pin/callable.rb', line 105

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
  callable.block = block.resolve_generics_from_context(generics_to_resolve,
                                                        yield_arg_types,
                                                        yield_return_type_context,
                                                        resolved_generic_values: resolved_generic_values) if callable.block?
  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)
  • context (ComplexType, nil)
  • resolved_generic_values (Hash{String => ComplexType}) (defaults to: {})

Returns:

  • (self)


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
# File 'lib/solargraph/pin/callable.rb', line 154

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(self.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



218
219
220
# File 'lib/solargraph/pin/callable.rb', line 218

def to_rbs
  rbs_generics + '(' + parameters.map { |param| param.to_rbs }.join(', ') + ') ' + (block.nil? ? '' : '{ ' + block.to_rbs + ' } ') + '-> ' + return_type.to_rbs
end

#transform_types {|| ... } ⇒ Array<String>, self

Yield Parameters:

Yield Returns:

Returns:

  • (Array<String>)
  • (self)


191
192
193
194
195
196
197
198
199
# File 'lib/solargraph/pin/callable.rb', line 191

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

#typify(api_map) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/solargraph/pin/callable.rb', line 128

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