Class: Solargraph::Pin::BaseVariable

Inherits:
Base
  • Object
show all
Includes:
ParserGem::NodeMethods
Defined in:
lib/solargraph/pin/base_variable.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#code_object, #location, #name, #path, #source, #type_location

Attributes included from Common

#closure, #context, #location

Instance Method Summary collapse

Methods inherited from Base

#all_rooted?, #best_location, #comments, #deprecated?, #desc, #directives, #docstring, #erase_generics, #filename, #identity, #infer, #inspect, #macros, #maybe_directives?, #nearly?, #presence_certain?, #probed?, #proxied?, #proxy, #realize, #resolve_generics, #resolve_generics_from_context, #to_rbs, #to_s, #transform_types, #typify

Methods included from Documenting

#documentation, normalize_indentation, strip_html_comments

Methods included from Conversions

#completion_item, #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(assignment: nil, return_type: nil, **splat) ⇒ BaseVariable

Returns a new instance of BaseVariable.

Parameters:

  • return_type (ComplexType, nil) (defaults to: nil)
  • assignment (Parser::AST::Node, nil) (defaults to: nil)


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

def initialize assignment: nil, return_type: nil, **splat
  super(**splat)
  @assignment = assignment
  # @type [nil, ::Array(Parser::AST::Node, Integer)]
  @mass_assignment = nil
  @return_type = return_type
end

Instance Attribute Details

#assignmentParser::AST::Node? (readonly)

Returns:

  • (Parser::AST::Node, nil)


10
11
12
# File 'lib/solargraph/pin/base_variable.rb', line 10

def assignment
  @assignment
end

#mass_assignmentObject

Returns the value of attribute mass_assignment.



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

def mass_assignment
  @mass_assignment
end

Instance Method Details

#==(other) ⇒ Object

Parameters:

  • other (Object)


97
98
99
100
# File 'lib/solargraph/pin/base_variable.rb', line 97

def == other
  return false unless super
  assignment == other.assignment
end

#completion_item_kindObject



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

def completion_item_kind
  Solargraph::LanguageServer::CompletionItemKinds::VARIABLE
end

#nil_assignment?Boolean

@sg-ignore

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/solargraph/pin/base_variable.rb', line 38

def nil_assignment?
  # this will always be false - should it be return_type ==
  #   ComplexType::NIL or somesuch?
  return_type.nil?
end

#probe(api_map) ⇒ ComplexType

Parameters:

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solargraph/pin/base_variable.rb', line 74

def probe api_map
  unless @assignment.nil?
    types = return_types_from_node(@assignment, api_map)
    return ComplexType.new(types.uniq) unless types.empty?
  end

  unless @mass_assignment.nil?
    mass_node, index = @mass_assignment
    types = return_types_from_node(mass_node, api_map)
    types.map! do |type|
      if type.tuple?
        type.all_params[index]
      elsif ['::Array', '::Set', '::Enumerable'].include?(type.rooted_name)
        type.all_params.first
      end
    end.compact!
    return ComplexType.new(types.uniq) unless types.empty?
  end

  ComplexType::UNDEFINED
end

#return_typeObject



33
34
35
# File 'lib/solargraph/pin/base_variable.rb', line 33

def return_type
  @return_type ||= generate_complex_type
end

#return_types_from_node(parent_node, api_map) ⇒ ::Array<ComplexType>

Parameters:

  • parent_node (Parser::AST::Node)
  • api_map (ApiMap)

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/solargraph/pin/base_variable.rb', line 51

def return_types_from_node(parent_node, api_map)
  types = []
  value_position_nodes_only(parent_node).each do |node|
    # Nil nodes may not have a location
    if node.nil? || node.type == :NIL || node.type == :nil
      types.push ComplexType::NIL
    else
      rng = Range.from_node(node)
      next if rng.nil?
      pos = rng.ending
      clip = api_map.clip_at(location.filename, pos)
      # Use the return node for inference. The clip might infer from the
      # first node in a method call instead of the entire call.
      chain = Parser.chain(node, nil, nil)
      result = chain.infer(api_map, closure, clip.locals).self_to_type(closure.context)
      types.push result unless result.undefined?
    end
  end
  types
end

#symbol_kindInteger

Returns:

  • (Integer)


29
30
31
# File 'lib/solargraph/pin/base_variable.rb', line 29

def symbol_kind
  Solargraph::LanguageServer::SymbolKinds::VARIABLE
end

#try_merge!(pin) ⇒ Object

Parameters:

  • pin (self)


103
104
105
106
107
108
# File 'lib/solargraph/pin/base_variable.rb', line 103

def try_merge! pin
  return false unless super
  @assignment = pin.assignment
  @return_type = pin.return_type
  true
end

#type_descObject



110
111
112
# File 'lib/solargraph/pin/base_variable.rb', line 110

def type_desc
  "#{super} = #{assignment&.type.inspect}"
end

#variable?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/solargraph/pin/base_variable.rb', line 44

def variable?
  true
end