Class: SourcekittenDependenciesGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcekitten/sourcekitten_dependencies_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_kitten_json) ⇒ SourcekittenDependenciesGenerator

Returns a new instance of SourcekittenDependenciesGenerator.



39
40
41
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 39

def initialize(source_kitten_json)
  @source_kitten_json = source_kitten_json
end

Instance Method Details

#generate_dependenciesDependencyTree

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 43

def generate_dependencies

  @tree = DependencyTree.new
  @context = []

  file = File.read(@source_kitten_json)
  parsed_files = JSON.parse(file)

  parsed_files.each do |parsed_file|
    parsed_file.each do |_path, contents|
      substructures = contents[SKKey::SUBSTRUCTURE] || next
      substructures.each { |substructure| parse_structure(substructure) }
    end
  end

  @tree
end

#generic_parameters(element) ⇒ Array<String>

Returns array of generic names for element.

Returns:

  • (Array<String>)

    array of generic names for element



191
192
193
194
195
196
197
198
199
200
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 191

def generic_parameters(element)
  fully_annotated_decl = element[SKKey::FULLY_ANNOTATED_DECLARATION]
  return [] if fully_annotated_decl.nil?
  generics = []
  doc = REXML::Document.new(fully_annotated_decl)
  doc.each_element('//decl.generic_type_param.name') do |el|
    generics.push(el.text.to_s)
  end
  generics
end

#in_context(name) ⇒ Object



61
62
63
64
65
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 61

def in_context(name)
  @context.push(name)
  yield
  @context.pop
end

#is_typealias(element, name) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 202

def is_typealias(element, name)
  fully_annotated_decl = element[SKKey::FULLY_ANNOTATED_DECLARATION]
  return false if fully_annotated_decl.nil?
  doc = REXML::Document.new(fully_annotated_decl)
  doc.each_element('//ref.typealias') do |el|
    return true if el.text.to_s == name
  end

  false
end


139
140
141
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 139

def link_items_from_context(item_name)
  @context.each { |el_name| @tree.add(el_name, item_name) }
end

#parse_structure(element) ⇒ Object

Parameters:

  • element (Hash)

    SourceKitten source



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
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 83

def parse_structure(element)

  kind = element[SKKey::KIND]
  item_name = element[SKKey::NAME]

  case kind
  when SKDeclarationType::SWIFT_EXTENSION
    process_element(element, DependencyItemType::UNKNOWN)

  when SKDeclarationType::SWIFT_PROTOCOL, SKDeclarationType::OBJC_PROTOCOL
    process_element(element, DependencyItemType::PROTOCOL)

  when SKDeclarationType::SWIFT_STRUCT, SKDeclarationType::OBJC_STRUCT
    process_element(element, DependencyItemType::STRUCTURE)

  when SKDeclarationType::SWIFT_CLASS, SKDeclarationType::OBJC_CLASS
    process_element(element, DependencyItemType::CLASS)
    register_types_from_annotated_declaration(element)

  when SKDeclarationType::INSTANCE_VARIABLE, SKDeclarationType::INSTANCE_METHOD, SKDeclarationType::STATIC_VARIABLE, SKDeclarationType::CLASS_METHOD
    @context.each do |el_name|
      register_types_from_annotated_declaration(element, el_name)
    end
    parse_substructures(element)

  when SKDeclarationType::CALL
    if item_name
      object_names = potential_object_names(item_name)
      object_names.each { |on| @context.each { |el_name| @tree.add(el_name, on, DependencyLinkType::CALL)} }
    end
    parse_substructures(element)

  when SKDeclarationType::ARGUMENT, SKDeclarationType::DICTIONARY, SKDeclarationType::ARRAY
    parse_substructures(element)

  else
    # do nothing
  end
end

#parse_substructures(element) ⇒ Object



134
135
136
137
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 134

def parse_substructures(element)
  sub_structures = element[SKKey::SUBSTRUCTURE] || return
  sub_structures.each { |it| parse_structure(it) }
end

#potential_object_names(call_string) ⇒ Object



223
224
225
226
227
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 223

def potential_object_names(call_string)
  (call_string.scan(/^[A-Z_][A-Za-z0-9_]+/) + call_string.scan(/\s[A-Z_][A-Za-z0-9_]+/))
    .select { |t| !t.empty? }
    .select { |t| !is_primitive_swift_type?(t) }
end

#process_element(element, type) ⇒ Object

Parameters:

  • item (Hash)

    array of sourcekitten substructure of class, protocol, whatever

  • type (DependencyItemType)

    type of items to register



125
126
127
128
129
130
131
132
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 125

def process_element(element, type)
  register_item(element, type)
  item_name = element[SKKey::NAME] || return
  link_items_from_context(item_name)
  in_context(item_name) do
    parse_substructures(element)
  end
end

#register_item(item, type) ⇒ Object

Parameters:

  • item (Hash)

    array of sourcekitten substructure of class, protocol, whatever

  • type (DependencyItemType)

    type of items to register



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 69

def register_item(item, type)
  item_name = item[SKKey::NAME]
  @tree.register(item_name, type)

  inherited_types = item[SKKey::INHERITED_TYPES] || return

  inherited_types.each do |inherited_type|
    inherited_type_name = inherited_type[SKKey::NAME]
    @tree.add(item_name, inherited_type_name)
  end

end

#register_types_from_annotated_declaration(element, name = nil) ⇒ Object



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
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 143

def register_types_from_annotated_declaration(element, name = nil)
  item_name = name || element[SKKey::NAME] || return
  annotated_decl = element[SKKey::ANNOTATED_DECLARATION] || return

  doc = REXML::Document.new(annotated_decl)

  has_typealiases = false
  doc.each_element('//Declaration/Type') do |el|
    dependency_type = el.text.to_s

    # get el type
    attribute_el = el.attribute('usr') || next

    if is_typealias(element, dependency_type)
      has_typealiases = true
      next
    end

    attribute = attribute_el.to_s
    if attribute.start_with? 's:P'
      @tree.add(item_name, dependency_type)
      @tree.register(dependency_type, DependencyItemType::PROTOCOL)
    elsif attribute.start_with? 'c:objc(pl)'
      @tree.add(item_name, dependency_type)
      @tree.register(dependency_type, DependencyItemType::PROTOCOL)
    elsif attribute.start_with? 'c:objc(cs)'
      @tree.add(item_name, dependency_type)
      @tree.register(dependency_type, DependencyItemType::CLASS)
    elsif attribute.start_with? 's:C'
      @tree.add(item_name, dependency_type)
      @tree.register(dependency_type, DependencyItemType::CLASS)
    end
  end

  register_types_from_type_name(element, item_name) if has_typealiases
end

#register_types_from_type_name(element, item_name) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 180

def register_types_from_type_name(element, item_name)
  type_name_string = element[SKKey::TYPE_NAME] || return

  generics = generic_parameters(element)
  type_names(type_name_string)
    .select { |type_name| !generics.include?(type_name) }
    .each { |type_name| @tree.add(item_name, type_name) }

end

#type_names(type_name_string) ⇒ Array<String>

Returns an array of strings, which represents type names

Parameters:

  • type_name_string (String)

    sourcekitten type name

Returns:

  • (Array<String>)

    array of types, found in this type



216
217
218
219
220
221
# File 'lib/sourcekitten/sourcekitten_dependencies_generator.rb', line 216

def type_names(type_name_string)
  type_name_string
    .split(/\W+/)
    .select { |t| !t.empty? }
    .select { |t| !is_primitive_swift_type?(t) }
end