Class: Solargraph::Pin::BaseVariable

Inherits:
Base
  • Object
show all
Includes:
(rubyvm? ? Rubyvm::NodeMethods : Legacy(rubyvm? ? Rubyvm::NodeMethods : Legacy::NodeMethods)
Defined in:
lib/solargraph/pin/base_variable.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#code_object, #location, #name, #path

Attributes included from Common

#closure, #location

Instance Method Summary collapse

Methods inherited from Base

#comments, #deprecated?, #directives, #docstring, #filename, #identity, #infer, #inspect, #macros, #maybe_directives?, #nearly?, #probed?, #proxied?, #proxy, #realize, #to_s, #typify

Methods included from Documenting

#documentation

Methods included from Conversions

#completion_item, #detail, #link_documentation, #reset_conversions, #resolve_completion_item, #signature_help, #text_documentation

Methods included from Common

#binder, #comments, #context, #name, #namespace, #path

Constructor Details

#initialize(assignment: nil, **splat) ⇒ BaseVariable

Returns a new instance of BaseVariable.

Parameters:

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


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

def initialize assignment: nil, **splat
  super(**splat)
  @assignment = assignment
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

Instance Method Details

#==(other) ⇒ Object



62
63
64
65
# File 'lib/solargraph/pin/base_variable.rb', line 62

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

#completion_item_kindObject



18
19
20
# File 'lib/solargraph/pin/base_variable.rb', line 18

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

#nil_assignment?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/solargraph/pin/base_variable.rb', line 31

def nil_assignment?
  return_type.nil?
end

#probe(api_map) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/solargraph/pin/base_variable.rb', line 39

def probe api_map
  return ComplexType::UNDEFINED if @assignment.nil?
  types = []
  returns_from(@assignment).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, clip.in_block?)
      result = chain.infer(api_map, closure, clip.locals)
      types.push result unless result.undefined?
    end
  end
  return ComplexType::UNDEFINED if types.empty?
  ComplexType.try_parse(*types.map(&:tag))
end

#return_typeObject



27
28
29
# File 'lib/solargraph/pin/base_variable.rb', line 27

def return_type
  @return_type ||= generate_complex_type
end

#symbol_kindInteger

Returns:

  • (Integer)


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

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

#try_merge!(pin) ⇒ Object



67
68
69
70
71
72
# File 'lib/solargraph/pin/base_variable.rb', line 67

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

#variable?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/solargraph/pin/base_variable.rb', line 35

def variable?
  true
end