Class: Solargraph::ComplexType

Inherits:
Array
  • 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<>')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ComplexType.

Parameters:

  • types (Array<ComplexType>) (defaults to: [ComplexType::UNDEFINED])


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

def initialize types = [ComplexType::UNDEFINED]
  super()
  concat types
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing name, *args, &block
  return if first.nil?
  return 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:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
# File 'lib/solargraph/complex_type.rb', line 60

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)])
          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)])
        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)])
  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

Instance Method Details

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

Parameters:

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

Returns:



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

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

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

Returns:

  • (Boolean)


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

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

#to_sObject



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

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