Class: Solargraph::ComplexType

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType

Returns a new instance of ComplexType.

Parameters:



14
15
16
# File 'lib/solargraph/complex_type.rb', line 14

def initialize types = [UniqueType::UNDEFINED]
  @items = types.uniq(&:to_s)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



55
56
57
58
59
# File 'lib/solargraph/complex_type.rb', line 55

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

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.

Examples:

ComplexType.parse 'String', 'Foo', 'nil' #=> [String, Foo, nil]

Parameters:

  • *strings (Array<String>)

    The type definitions to parse

  • partial (Boolean) (defaults to: false)

    True if the string is part of a another type

Returns:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/solargraph/complex_type.rb', line 117

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
          point_stack -= 1
          subtype_string += char if point_stack == 0
          raise ComplexTypeError, "Invalid close in type #{type_string}" if point_stack < 0
        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

Parameters:

  • strings (Array<String>)

Returns:



197
198
199
200
201
202
# File 'lib/solargraph/complex_type.rb', line 197

def try_parse *strings
  parse *strings
rescue ComplexTypeError => e
  Solargraph.logger.info "Error parsing complex type: #{e.message}"
  ComplexType::UNDEFINED
end

Instance Method Details

#[](index) ⇒ Object



47
48
49
# File 'lib/solargraph/complex_type.rb', line 47

def [](index)
  @items[index]
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/solargraph/complex_type.rb', line 69

def all? &block
  @items.all? &block
end

#each {|| ... } ⇒ Array

Yield Parameters:

Returns:



39
40
41
# File 'lib/solargraph/complex_type.rb', line 39

def each &block
  @items.each &block
end

#firstObject



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

def first
  @items.first
end

#lengthObject



43
44
45
# File 'lib/solargraph/complex_type.rb', line 43

def length
  @items.length
end

#map(&block) ⇒ Object



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

def map &block
  @items.map &block
end

#qualify(api_map, context = '') ⇒ ComplexType

Parameters:

  • api_map (ApiMap)
  • context (String) (defaults to: '')

Returns:



21
22
23
24
25
26
27
# File 'lib/solargraph/complex_type.rb', line 21

def qualify api_map, context = ''
  types = @items.map do |t|
    next t if ['Boolean', 'nil', 'void', 'undefined'].include?(t.name)
    t.qualify api_map, context
  end
  ComplexType.new(types)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/solargraph/complex_type.rb', line 61

def respond_to_missing?(name, include_private = false)
  TypeMethods.public_instance_methods.include?(name) || super
end

#select(&block) ⇒ Object



51
52
53
# File 'lib/solargraph/complex_type.rb', line 51

def select &block
  @items.select &block
end

#self_to(dst) ⇒ ComplexType

Parameters:

  • dst (String)

Returns:



79
80
81
82
83
84
# File 'lib/solargraph/complex_type.rb', line 79

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/solargraph/complex_type.rb', line 73

def selfy?
  @items.any?(&:selfy?)
end

#to_sObject



65
66
67
# File 'lib/solargraph/complex_type.rb', line 65

def to_s
  map(&:tag).join(', ')
end