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) ⇒ Array<UniqueType>
Parse type strings into a ComplexType.
- .try_parse(*strings) ⇒ ComplexType
Instance Method Summary collapse
- #[](index) ⇒ UniqueType
- #all?(&block) ⇒ Boolean
- #all_params ⇒ Array<ComplexType>
- #any?(&block) ⇒ Boolean
- #each {|| ... } ⇒ Enumerable<UniqueType>
- #each_unique_type ⇒ Enumerator<UniqueType>
- #first ⇒ UniqueType
- #generic? ⇒ Boolean
-
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
constructor
A new instance of ComplexType.
- #length ⇒ Integer
- #map {|| ... } ⇒ Array
- #method_missing(name, *args, &block) ⇒ Object?
- #namespace ⇒ String
- #namespaces ⇒ Array<String>
- #nullable? ⇒ Boolean
- #qualify(api_map, context = '') ⇒ ComplexType
- #resolve_generics(definitions, context_type) ⇒ ComplexType
- #resolve_generics_from_context(generics_to_resolve, context_type, resolved_generic_values: {}) ⇒ self
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #select(&block) ⇒ Array<UniqueType>
- #self_to(dst) ⇒ ComplexType
- #selfy? ⇒ Boolean
- #to_a ⇒ Array<UniqueType>
- #to_rbs ⇒ String
- #to_s ⇒ Object
- #transform(new_name = nil) {|t| ... } ⇒ ComplexType
Constructor Details
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
Returns a new instance of ComplexType.
15 16 17 18 19 |
# File 'lib/solargraph/complex_type.rb', line 15 def initialize types = [UniqueType::UNDEFINED] # @todo @items here should not need an annotation # @type [Array<UniqueType>] @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?
113 114 115 116 117 |
# File 'lib/solargraph/complex_type.rb', line 113 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.
179 180 181 |
# File 'lib/solargraph/complex_type.rb', line 179 def items @items end |
Class Method Details
.parse(*strings, partial: false) ⇒ Array<UniqueType>
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.
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/solargraph/complex_type.rb', line 227 def parse *strings, partial: false # @type [Hash{Array<String> => ComplexType}] @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) # @todo this should either expand key_type's type # automatically or complain about not being # compatible with key_type's type in type checking 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 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
311 312 313 314 315 316 |
# File 'lib/solargraph/complex_type.rb', line 311 def try_parse *strings parse *strings rescue ComplexTypeError => e Solargraph.logger.info "Error parsing complex type: #{e.}" ComplexType::UNDEFINED end |
Instance Method Details
#[](index) ⇒ UniqueType
91 92 93 |
# File 'lib/solargraph/complex_type.rb', line 91 def [](index) @items[index] end |
#all?(&block) ⇒ Boolean
129 130 131 |
# File 'lib/solargraph/complex_type.rb', line 129 def all? &block @items.all? &block end |
#all_params ⇒ Array<ComplexType>
175 176 177 |
# File 'lib/solargraph/complex_type.rb', line 175 def all_params @items.first.all_params || [] end |
#any?(&block) ⇒ Boolean
133 134 135 |
# File 'lib/solargraph/complex_type.rb', line 133 def any? &block @items.compact.any? &block end |
#each {|| ... } ⇒ Enumerable<UniqueType>
63 64 65 |
# File 'lib/solargraph/complex_type.rb', line 63 def each &block @items.each &block end |
#each_unique_type ⇒ Enumerator<UniqueType>
This method returns an undefined value.
71 72 73 74 75 76 77 |
# File 'lib/solargraph/complex_type.rb', line 71 def each_unique_type &block return enum_for(__method__) unless block_given? @items.each do |item| item.each_unique_type &block end end |
#first ⇒ UniqueType
44 45 46 |
# File 'lib/solargraph/complex_type.rb', line 44 def first @items.first end |
#generic? ⇒ Boolean
141 142 143 |
# File 'lib/solargraph/complex_type.rb', line 141 def generic? any?(&:generic?) end |
#length ⇒ Integer
80 81 82 |
# File 'lib/solargraph/complex_type.rb', line 80 def length @items.length end |
#map {|| ... } ⇒ Array
57 58 59 |
# File 'lib/solargraph/complex_type.rb', line 57 def map &block @items.map &block end |
#namespace ⇒ String
101 102 103 104 |
# File 'lib/solargraph/complex_type.rb', line 101 def namespace # cache this attr for high frequency call @namespace ||= method_missing(:namespace).to_s end |
#namespaces ⇒ Array<String>
107 108 109 |
# File 'lib/solargraph/complex_type.rb', line 107 def namespaces @items.map(&:namespace) end |
#nullable? ⇒ Boolean
170 171 172 |
# File 'lib/solargraph/complex_type.rb', line 170 def nullable? @items.any?(&:nil_type?) end |
#qualify(api_map, context = '') ⇒ ComplexType
24 25 26 27 28 29 30 31 |
# File 'lib/solargraph/complex_type.rb', line 24 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
156 157 158 159 |
# File 'lib/solargraph/complex_type.rb', line 156 def resolve_generics definitions, context_type result = @items.map { |i| i.resolve_generics(definitions, context_type) } ComplexType.parse(*result.map(&:tag)) end |
#resolve_generics_from_context(generics_to_resolve, context_type, resolved_generic_values: {}) ⇒ self
37 38 39 40 41 |
# File 'lib/solargraph/complex_type.rb', line 37 def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {} return self unless generic? ComplexType.new(@items.map { |i| i.resolve_generics_from_context(generics_to_resolve, context_type, resolved_generic_values: resolved_generic_values) }) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
121 122 123 |
# File 'lib/solargraph/complex_type.rb', line 121 def respond_to_missing?(name, include_private = false) TypeMethods.public_instance_methods.include?(name) || super end |
#select(&block) ⇒ Array<UniqueType>
96 97 98 |
# File 'lib/solargraph/complex_type.rb', line 96 def select &block @items.select &block end |
#self_to(dst) ⇒ ComplexType
163 164 165 166 167 168 |
# File 'lib/solargraph/complex_type.rb', line 163 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
137 138 139 |
# File 'lib/solargraph/complex_type.rb', line 137 def selfy? @items.any?(&:selfy?) end |
#to_a ⇒ Array<UniqueType>
85 86 87 |
# File 'lib/solargraph/complex_type.rb', line 85 def to_a @items end |
#to_rbs ⇒ String
49 50 51 52 53 |
# File 'lib/solargraph/complex_type.rb', line 49 def to_rbs ((@items.length > 1 ? '(' : '') + @items.map(&:to_rbs).join(' | ') + (@items.length > 1 ? ')' : '')) end |
#to_s ⇒ Object
125 126 127 |
# File 'lib/solargraph/complex_type.rb', line 125 def to_s map(&:tag).join(', ') end |
#transform(new_name = nil) {|t| ... } ⇒ ComplexType
149 150 151 |
# File 'lib/solargraph/complex_type.rb', line 149 def transform(new_name = nil, &transform_type) ComplexType.new(map { |ut| ut.transform(new_name, &transform_type) }) end |