Class: Jazzy::SymbolGraph::ExtNode

Inherits:
BaseNode
  • Object
show all
Includes:
Comparable
Defined in:
lib/jazzy/symbol_graph/ext_node.rb

Overview

An ExtNode is a node of the reconstructed syntax tree representing an extension that we fabricate to resolve certain relationships.

Direct Known Subclasses

ExtSymNode

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#children, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseNode

#add_child, #add_children_to_sourcekit

Instance Attribute Details

#all_constraintsObject

ExtConstraints



28
29
30
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 28

def all_constraints
  @all_constraints
end

#conformancesObject

array, can be empty



29
30
31
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 29

def conformances
  @conformances
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 27

def name
  @name
end

#real_usrObject

Returns the value of attribute real_usr.



26
27
28
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 26

def real_usr
  @real_usr
end

#usrObject

Returns the value of attribute usr.



25
26
27
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 25

def usr
  @usr
end

Class Method Details

.new_for_conformance(type_usr, type_name, protocol, constraints) ⇒ Object

Deduce an extension from a protocol conformance for some type



42
43
44
45
46
47
48
49
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 42

def self.new_for_conformance(type_usr,
                             type_name,
                             protocol,
                             constraints)
  new(type_usr, type_name, constraints).tap do |o|
    o.add_conformance(protocol)
  end
end

.new_for_member(type_usr, member, constraints) ⇒ Object

Deduce an extension from a member of an unknown type or of known type with additional constraints



33
34
35
36
37
38
39
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 33

def self.new_for_member(type_usr,
                        member,
                        constraints)
  new(type_usr,
      member.parent_qualified_name,
      constraints).tap { |o| o.add_child(member) }
end

Instance Method Details

#<=>(other) ⇒ Object



110
111
112
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 110

def <=>(other)
  sort_key <=> other.sort_key
end

#add_conformance(protocol) ⇒ Object



67
68
69
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 67

def add_conformance(protocol)
  conformances.append(protocol).sort!
end

#constraintsObject



63
64
65
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 63

def constraints
  all_constraints.merged
end

#full_declarationObject



71
72
73
74
75
76
77
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 71

def full_declaration
  decl = "extension #{name}"
  unless conformances.empty?
    decl += " : #{conformances.join(', ')}"
  end
  decl + all_constraints.ext.to_where_clause
end

#sort_keyObject



106
107
108
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 106

def sort_key
  name + constraints.map(&:to_swift).join
end

#to_sourcekit(module_name, ext_module_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 79

def to_sourcekit(module_name, ext_module_name)
  declaration = full_declaration
  xml_declaration = "<swift>#{CGI.escapeHTML(declaration)}</swift>"

  hash = {
    'key.kind' => 'source.lang.swift.decl.extension',
    'key.usr' => real_usr || usr,
    'key.name' => name,
    'key.modulename' => ext_module_name,
    'key.parsed_declaration' => declaration,
    'key.annotated_decl' => xml_declaration,
  }

  unless conformances.empty?
    hash['key.inheritedtypes'] = conformances.map do |conformance|
      { 'key.name' => conformance }
    end
  end

  add_children_to_sourcekit(hash, module_name)

  hash
end