Class: Solargraph::Pin::BaseVariable
- Includes:
- ParserGem::NodeMethods
- Defined in:
- lib/solargraph/pin/base_variable.rb
Direct Known Subclasses
ClassVariable, Constant, GlobalVariable, InstanceVariable, LocalVariable
Constant Summary
Constants included from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS
Instance Attribute Summary collapse
- #assignment ⇒ Parser::AST::Node? readonly
-
#mass_assignment ⇒ Object
Returns the value of attribute mass_assignment.
Attributes inherited from Base
#code_object, #directives, #docstring, #location, #name, #path, #source, #type_location
Attributes included from Common
Instance Method Summary collapse
- #==(other) ⇒ Object
- #combine_with(other, attrs = {}) ⇒ Object
- #completion_item_kind ⇒ Object
-
#initialize(assignment: nil, return_type: nil, **splat) ⇒ BaseVariable
constructor
A new instance of BaseVariable.
-
#nil_assignment? ⇒ Boolean
@sg-ignore.
- #probe(api_map) ⇒ ComplexType
- #return_type ⇒ Object
- #return_types_from_node(parent_node, api_map) ⇒ ::Array<ComplexType>
- #symbol_kind ⇒ Integer
- #type_desc ⇒ Object
- #variable? ⇒ Boolean
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, #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?, #probed?, #proxied?, #proxy, #rbs_location?, #realize, #reset_generated!, #resolve_generics, #resolve_generics_from_context, #to_rbs, #to_s, #transform_types, #typify
Methods included from Logging
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.
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
#assignment ⇒ Parser::AST::Node? (readonly)
10 11 12 |
# File 'lib/solargraph/pin/base_variable.rb', line 10 def assignment @assignment end |
#mass_assignment ⇒ Object
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
106 107 108 109 |
# File 'lib/solargraph/pin/base_variable.rb', line 106 def == other return false unless super assignment == other.assignment end |
#combine_with(other, attrs = {}) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/solargraph/pin/base_variable.rb', line 24 def combine_with(other, attrs={}) attrs.merge({ assignment: assert_same(other, :assignment), mass_assignment: assert_same(other, :mass_assignment), return_type: combine_return_type(other), }) super(other, attrs) end |
#completion_item_kind ⇒ Object
33 34 35 |
# File 'lib/solargraph/pin/base_variable.rb', line 33 def completion_item_kind Solargraph::LanguageServer::CompletionItemKinds::VARIABLE end |
#nil_assignment? ⇒ Boolean
@sg-ignore
47 48 49 50 51 |
# File 'lib/solargraph/pin/base_variable.rb', line 47 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/solargraph/pin/base_variable.rb', line 83 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_type ⇒ Object
42 43 44 |
# File 'lib/solargraph/pin/base_variable.rb', line 42 def return_type @return_type ||= generate_complex_type end |
#return_types_from_node(parent_node, api_map) ⇒ ::Array<ComplexType>
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/solargraph/pin/base_variable.rb', line 60 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_kind ⇒ Integer
38 39 40 |
# File 'lib/solargraph/pin/base_variable.rb', line 38 def symbol_kind Solargraph::LanguageServer::SymbolKinds::VARIABLE end |
#type_desc ⇒ Object
111 112 113 |
# File 'lib/solargraph/pin/base_variable.rb', line 111 def type_desc "#{super} = #{assignment&.type.inspect}" end |
#variable? ⇒ Boolean
53 54 55 |
# File 'lib/solargraph/pin/base_variable.rb', line 53 def variable? true end |