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

Constant Summary collapse

VOID =
ComplexType.parse('void')
UNDEFINED =
ComplexType.parse('undefined')
SYMBOL =
ComplexType.parse('Symbol')
ROOT =
ComplexType.parse('Class<>')
NIL =
ComplexType.parse('nil')
SELF =
ComplexType.parse('self')
BOOLEAN =
ComplexType.parse('Boolean')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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


12
13
14
# File 'lib/solargraph/complex_type.rb', line 12

def initialize types = [UniqueType::UNDEFINED]
  @items = types
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


52
53
54
55
56
# File 'lib/solargraph/complex_type.rb', line 52

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]

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/solargraph/complex_type.rb', line 96

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 = ''
    subtype_string = ''
    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 = ''
          subtype_string = ''
          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 = ''
        subtype_string = ''
        next
      end
      if point_stack == 0 && curly_stack == 0 && paren_stack == 0
        base += char 
      else
        subtype_string += char
      end
    end
    base.strip!
    subtype_string.strip!
    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, subtype_string)
  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


178
179
180
181
182
183
# File 'lib/solargraph/complex_type.rb', line 178

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


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

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

#all?(&block) ⇒ Boolean


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

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

#each {|| ... } ⇒ Array

Yield Parameters:


36
37
38
# File 'lib/solargraph/complex_type.rb', line 36

def each &block
  @items.each &block
end

#firstObject


26
27
28
# File 'lib/solargraph/complex_type.rb', line 26

def first
  @items.first
end

#lengthObject


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

def length
  @items.length
end

#map(&block) ⇒ Object


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

def map &block
  @items.map &block
end

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


19
20
21
22
23
24
# File 'lib/solargraph/complex_type.rb', line 19

def qualify api_map, context = ''
  types = @items.map do |t|
    t.qualify api_map, context
  end
  ComplexType.new(types)
end

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


58
59
60
# File 'lib/solargraph/complex_type.rb', line 58

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

#select(&block) ⇒ Object


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

def select &block
  @items.select &block
end

#self_to(dst) ⇒ Object


74
75
76
77
78
# File 'lib/solargraph/complex_type.rb', line 74

def self_to dst
  return self unless selfy?
  result = @items.map { |i| i.self_to dst }
  ComplexType.parse(*result.map(&:tag))
end

#selfy?Boolean


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

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

#to_sObject


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

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