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, #directives, #docstring, #location, #name, #path, #source, #type_location

Attributes included from Common

#closure, #context, #location

Instance Method Summary collapse

Methods inherited from Closure

#binder, #context, #gates, #generic_defaults, #rbs_generics

Methods inherited from Base

#==, #all_location_text, #all_rooted?, #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, #combine_directives, #combine_name, #combine_return_type, #comments, #completion_item_kind, #deprecated?, #desc, #dodgy_return_type_source?, #erase_generics, #filename, #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, #comments, #name, #namespace, #path

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

#arityObject



81
82
83
# File 'lib/solargraph/pin/callable.rb', line 81

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)


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

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)


209
210
211
# File 'lib/solargraph/pin/callable.rb', line 209

def block?
  !!@block
end

#blockless_parametersObject



73
74
75
76
77
78
79
# File 'lib/solargraph/pin/callable.rb', line 73

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

#choose_parameters(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/solargraph/pin/callable.rb', line 60

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
  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) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/solargraph/pin/callable.rb', line 28

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

Parameters:

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

Returns:

  • (self)


42
43
44
45
46
47
48
49
# File 'lib/solargraph/pin/callable.rb', line 42

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



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

def generics
  []
end

#mandatory_positional_param_countObject



200
201
202
# File 'lib/solargraph/pin/callable.rb', line 200

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

#method_nameObject



128
129
130
131
# File 'lib/solargraph/pin/callable.rb', line 128

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

#method_namespaceObject



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

def method_namespace
  closure.namespace
end

#parameter_names::Array<String>

Returns:

  • (::Array<String>)


52
53
54
# File 'lib/solargraph/pin/callable.rb', line 52

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)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/solargraph/pin/callable.rb', line 93

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)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/solargraph/pin/callable.rb', line 141

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_rbsString

Returns:

  • (String)


205
206
207
# File 'lib/solargraph/pin/callable.rb', line 205

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)


178
179
180
181
182
183
184
185
186
# File 'lib/solargraph/pin/callable.rb', line 178

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



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/solargraph/pin/callable.rb', line 116

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