Class: Solargraph::Pin::Block

Inherits:
Callable show all
Includes:
Breakable
Defined in:
lib/solargraph/pin/block.rb

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Callable

#block, #parameters, #return_type

Attributes inherited from Closure

#scope

Attributes inherited from Base

#code_object, #combine_priority, #directives, #docstring, #location, #name, #path, #return_type, #source, #type_location

Attributes included from Common

#context, #location

Instance Method Summary collapse

Methods inherited from Callable

#arity, #arity_matches?, #block?, #blockless_parameters, #choose_parameters, #combine_blocks, #combine_with, #generics, #mandatory_positional_param_count, #method_name, #method_namespace, #parameter_names, #resolve_generics_from_context, #resolve_generics_from_context_until_complete, #to_rbs, #transform_types, #typify

Methods inherited from Closure

#combine_with, #context, #gates, #generic_defaults, #generics, #rbs_generics, #to_rbs

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, #combine_with, #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, #resolve_generics_from_context, #symbol_kind, #to_rbs, #to_s, #transform_types, #type_desc, #typify, #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

#closure, #comments, #name, #namespace, #path, #return_type, #source

Constructor Details

#initialize(receiver: nil, args: [], context: nil, node: nil, **splat) ⇒ Block

Returns a new instance of Block.

Parameters:

  • receiver (Parser::AST::Node, nil) (defaults to: nil)
  • node (Parser::AST::Node, nil) (defaults to: nil)
  • context (ComplexType, nil) (defaults to: nil)
  • args (::Array<Parameter>) (defaults to: [])


18
19
20
21
22
23
24
# File 'lib/solargraph/pin/block.rb', line 18

def initialize receiver: nil, args: [], context: nil, node: nil, **splat
  super(**splat, parameters: args)
  @receiver = receiver
  @context = context
  @return_type = ComplexType.parse('::Proc')
  @node = node
end

Instance Attribute Details

#nodeParser::AST::Node (readonly)

Returns:

  • (Parser::AST::Node)


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

def node
  @node
end

#receiverParser::AST::Node (readonly)

Returns:

  • (Parser::AST::Node)


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

def receiver
  @receiver
end

Instance Method Details

#binderObject



32
33
34
# File 'lib/solargraph/pin/block.rb', line 32

def binder
  @rebind&.defined? ? @rebind : closure.binder
end

#destructure_yield_types(yield_types, parameters) ⇒ ::Array<ComplexType>

Parameters:

Returns:



40
41
42
43
44
45
46
47
# File 'lib/solargraph/pin/block.rb', line 40

def destructure_yield_types(yield_types, parameters)
  # yielding a tuple into a block will destructure the tuple
  if yield_types.length == 1
    yield_type = yield_types.first
    return yield_type.all_params if yield_type.tuple? && yield_type.all_params.length == parameters.length
  end
  parameters.map.with_index { |_, idx| yield_types[idx] || ComplexType::UNDEFINED }
end

#rebind(api_map) ⇒ void

This method returns an undefined value.

Parameters:



28
29
30
# File 'lib/solargraph/pin/block.rb', line 28

def rebind api_map
  @rebind ||= maybe_rebind(api_map)
end

#typify_parameters(api_map) ⇒ ::Array<ComplexType>

Parameters:

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/solargraph/pin/block.rb', line 51

def typify_parameters(api_map)
  chain = Parser.chain(receiver, filename, node)
  clip = api_map.clip_at(location.filename, location.range.start)
  locals = clip.locals - [self]
  meths = chain.define(api_map, closure, locals)
  # @todo Convert logic to use signatures
  meths.each do |meth|
    next if meth.block.nil?

    yield_types = meth.block.parameters.map(&:return_type)
    # 'arguments' is what the method says it will yield to the
    # block; 'parameters' is what the block accepts
    argument_types = destructure_yield_types(yield_types, parameters)
    param_types = argument_types.each_with_index.map do |arg_type, idx|
      param = parameters[idx]
      param_type = chain.base.infer(api_map, param, locals)
      unless arg_type.nil?
        if arg_type.generic? && param_type.defined?
          namespace_pin = api_map.get_namespace_pins(meth.namespace, closure.namespace).first
          arg_type.resolve_generics(namespace_pin, param_type)
        else
          arg_type.self_to_type(chain.base.infer(api_map, self, locals)).qualify(api_map, meth.context.namespace)
        end
      end
    end
    return param_types if param_types.all?(&:defined?)
  end
  parameters.map { ComplexType::UNDEFINED }
end