Class: Solargraph::ComplexType
- Inherits:
-
Object
- Object
- Solargraph::ComplexType
- Defined in:
- lib/solargraph/complex_type.rb,
lib/solargraph/complex_type/unique_type.rb,
lib/solargraph/complex_type/type_methods.rb
Overview
A container for type data based on YARD type tags.
Defined Under Namespace
Modules: TypeMethods Classes: UniqueType
Constant Summary collapse
- GENERIC_TAG_NAME =
'generic'.freeze
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Class Method Summary collapse
-
.parse(*strings, partial: false) ⇒ ComplexType, ...
Parse type strings into a ComplexType.
- .try_parse(*strings) ⇒ ComplexType
Instance Method Summary collapse
- #[](index) ⇒ Object
- #all?(&block) ⇒ Boolean
- #all_params ⇒ Object
- #any?(&block) ⇒ Boolean
- #each {|| ... } ⇒ Enumerator<UniqueType>
- #each_unique_type {|| ... } ⇒ Enumerator<UniqueType>
- #first ⇒ Object
- #generic? ⇒ Boolean
-
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
constructor
A new instance of ComplexType.
- #length ⇒ Object
- #map(&block) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #namespace ⇒ String
- #namespaces ⇒ Array<String>
- #nullable? ⇒ Boolean
- #qualify(api_map, context = '') ⇒ ComplexType
- #resolve_generics(definitions, context_type) ⇒ ComplexType
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #select(&block) ⇒ Object
- #self_to(dst) ⇒ ComplexType
- #selfy? ⇒ Boolean
- #to_rbs ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
Returns a new instance of ComplexType.
15 16 17 |
# File 'lib/solargraph/complex_type.rb', line 15 def initialize types = [UniqueType::UNDEFINED] @items = types.flat_map(&:items).uniq(&:to_s) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
84 85 86 87 88 |
# File 'lib/solargraph/complex_type.rb', line 84 def method_missing name, *args, &block return if @items.first.nil? return @items.first.send(name, *args, &block) if respond_to_missing?(name) super end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
139 140 141 |
# File 'lib/solargraph/complex_type.rb', line 139 def items @items end |
Class Method Details
.parse(*strings, partial: false) ⇒ ComplexType, ...
Note:
The ‘partial` parameter is used to indicate that the method is receiving a string that will be used inside another ComplexType. It returns arrays of ComplexTypes instead of a single cohesive one. Consumers should not need to use this parameter; it should only be used internally.
Parse type strings into a ComplexType.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/solargraph/complex_type.rb', line 179 def parse *strings, partial: false @cache ||= {} unless partial cached = @cache[strings] return cached unless cached.nil? end types = [] key_types = nil strings.each do |type_string| point_stack = 0 curly_stack = 0 paren_stack = 0 base = String.new subtype_string = String.new type_string&.each_char do |char| if char == '=' #raise ComplexTypeError, "Invalid = in type #{type_string}" unless curly_stack > 0 elsif char == '<' point_stack += 1 elsif char == '>' if subtype_string.end_with?('=') && curly_stack > 0 subtype_string += char elsif base.end_with?('=') raise ComplexTypeError, "Invalid hash thing" unless key_types.nil? # types.push ComplexType.new([UniqueType.new(base[0..-2].strip)]) types.push UniqueType.new(base[0..-2].strip) key_types = types types = [] base.clear subtype_string.clear next else raise ComplexTypeError, "Invalid close in type #{type_string}" if point_stack == 0 point_stack -= 1 subtype_string += char end next elsif char == '{' curly_stack += 1 elsif char == '}' curly_stack -= 1 subtype_string += char raise ComplexTypeError, "Invalid close in type #{type_string}" if curly_stack < 0 next elsif char == '(' paren_stack += 1 elsif char == ')' paren_stack -= 1 subtype_string += char if paren_stack == 0 raise ComplexTypeError, "Invalid close in type #{type_string}" if paren_stack < 0 next elsif char == ',' && point_stack == 0 && curly_stack == 0 && paren_stack == 0 # types.push ComplexType.new([UniqueType.new(base.strip, subtype_string.strip)]) types.push UniqueType.new(base.strip, subtype_string.strip) base.clear subtype_string.clear next end if point_stack == 0 && curly_stack == 0 && paren_stack == 0 base.concat char else subtype_string.concat char end end raise ComplexTypeError, "Unclosed subtype in #{type_string}" if point_stack != 0 || curly_stack != 0 || paren_stack != 0 # types.push ComplexType.new([UniqueType.new(base, subtype_string)]) types.push UniqueType.new(base.strip, subtype_string.strip) end unless key_types.nil? raise ComplexTypeError, "Invalid use of key/value parameters" unless partial return key_types if types.empty? return [key_types, types] end result = partial ? types : ComplexType.new(types) @cache[strings] = result unless partial result end |
.try_parse(*strings) ⇒ ComplexType
259 260 261 262 263 264 |
# File 'lib/solargraph/complex_type.rb', line 259 def try_parse *strings parse *strings rescue ComplexTypeError => e Solargraph.logger.info "Error parsing complex type: #{e.}" ComplexType::UNDEFINED end |
Instance Method Details
#[](index) ⇒ Object
65 66 67 |
# File 'lib/solargraph/complex_type.rb', line 65 def [](index) @items[index] end |
#all?(&block) ⇒ Boolean
98 99 100 |
# File 'lib/solargraph/complex_type.rb', line 98 def all? &block @items.all? &block end |
#all_params ⇒ Object
135 136 137 |
# File 'lib/solargraph/complex_type.rb', line 135 def all_params @items.first.all_params || [] end |
#any?(&block) ⇒ Boolean
102 103 104 |
# File 'lib/solargraph/complex_type.rb', line 102 def any? &block @items.compact.any? &block end |
#each {|| ... } ⇒ Enumerator<UniqueType>
47 48 49 |
# File 'lib/solargraph/complex_type.rb', line 47 def each &block @items.each &block end |
#each_unique_type {|| ... } ⇒ Enumerator<UniqueType>
53 54 55 56 57 58 59 |
# File 'lib/solargraph/complex_type.rb', line 53 def each_unique_type &block return enum_for(__method__) unless block_given? @items.each do |item| item.each_unique_type &block end end |
#first ⇒ Object
31 32 33 |
# File 'lib/solargraph/complex_type.rb', line 31 def first @items.first end |
#generic? ⇒ Boolean
110 111 112 |
# File 'lib/solargraph/complex_type.rb', line 110 def generic? any?(&:generic?) end |
#length ⇒ Object
61 62 63 |
# File 'lib/solargraph/complex_type.rb', line 61 def length @items.length end |
#map(&block) ⇒ Object
41 42 43 |
# File 'lib/solargraph/complex_type.rb', line 41 def map &block @items.map &block end |
#namespace ⇒ String
74 75 76 77 |
# File 'lib/solargraph/complex_type.rb', line 74 def namespace # cache this attr for high frequency call @namespace ||= method_missing(:namespace).to_s end |
#namespaces ⇒ Array<String>
80 81 82 |
# File 'lib/solargraph/complex_type.rb', line 80 def namespaces @items.map(&:namespace) end |
#nullable? ⇒ Boolean
131 132 133 |
# File 'lib/solargraph/complex_type.rb', line 131 def nullable? @items.any?(&:nil_type?) end |
#qualify(api_map, context = '') ⇒ ComplexType
22 23 24 25 26 27 28 29 |
# File 'lib/solargraph/complex_type.rb', line 22 def qualify api_map, context = '' red = reduce_object types = red.items.map do |t| next t if ['Boolean', 'nil', 'void', 'undefined'].include?(t.name) t.qualify api_map, context end ComplexType.new(types).reduce_object end |
#resolve_generics(definitions, context_type) ⇒ ComplexType
117 118 119 120 |
# File 'lib/solargraph/complex_type.rb', line 117 def resolve_generics definitions, context_type result = @items.map { |i| i.resolve_generics(definitions, context_type) } ComplexType.parse(*result.map(&:tag)) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
90 91 92 |
# File 'lib/solargraph/complex_type.rb', line 90 def respond_to_missing?(name, include_private = false) TypeMethods.public_instance_methods.include?(name) || super end |
#select(&block) ⇒ Object
69 70 71 |
# File 'lib/solargraph/complex_type.rb', line 69 def select &block @items.select &block end |
#self_to(dst) ⇒ ComplexType
124 125 126 127 128 129 |
# File 'lib/solargraph/complex_type.rb', line 124 def self_to dst return self unless selfy? red = reduce_class(dst) result = @items.map { |i| i.self_to red } ComplexType.parse(*result.map(&:tag)) end |
#selfy? ⇒ Boolean
106 107 108 |
# File 'lib/solargraph/complex_type.rb', line 106 def selfy? @items.any?(&:selfy?) end |
#to_rbs ⇒ Object
35 36 37 38 39 |
# File 'lib/solargraph/complex_type.rb', line 35 def to_rbs ((@items.length > 1 ? '(' : '') + @items.map do |item| "#{item.namespace}#{item.parameters? ? "[#{item.subtypes.map { |s| s.to_rbs }.join(', ')}]" : ''}" end.join(' | ') + (@items.length > 1 ? ')' : '')).gsub(/undefined/, 'untyped') end |
#to_s ⇒ Object
94 95 96 |
# File 'lib/solargraph/complex_type.rb', line 94 def to_s map(&:tag).join(', ') end |