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



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

def all_constraints
  @all_constraints
end

#conformancesObject

set, can be empty



31
32
33
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 31

def conformances
  @conformances
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#real_usrObject

Returns the value of attribute real_usr.



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

def real_usr
  @real_usr
end

#usrObject

Returns the value of attribute usr.



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

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



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

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



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

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



114
115
116
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 114

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

#add_conformance(protocol) ⇒ Object



69
70
71
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 69

def add_conformance(protocol)
  conformances.add(protocol)
end

#constraintsObject



65
66
67
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 65

def constraints
  all_constraints.merged
end

#full_declarationObject



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

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

#sort_keyObject



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

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

#to_sourcekit(module_name, ext_module_name) ⇒ Object



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

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.sort.map do |conformance|
      { 'key.name' => conformance }
    end
  end

  add_children_to_sourcekit(hash, module_name)

  hash
end