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.

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#children, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseNode

#add_child, #children_to_sourcekit

Instance Attribute Details

#all_constraintsObject

ExtConstraints



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

def all_constraints
  @all_constraints
end

#conformancesObject

array, can be empty



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

def conformances
  @conformances
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#usrObject

Returns the value of attribute usr.



23
24
25
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 23

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



39
40
41
42
43
44
45
46
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 39

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



30
31
32
33
34
35
36
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 30

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



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

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

#add_conformance(protocol) ⇒ Object



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

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

#constraintsObject



60
61
62
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 60

def constraints
  all_constraints.merged
end

#full_declarationObject



68
69
70
71
72
73
74
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 68

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

#sort_keyObject



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

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

#to_sourcekit(module_name) ⇒ Object



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
# File 'lib/jazzy/symbol_graph/ext_node.rb', line 76

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

  hash = {
    'key.kind' => 'source.lang.swift.decl.extension',
    'key.usr' => usr,
    'key.name' => name,
    'key.modulename' => 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

  unless children.empty?
    hash['key.substructure'] = children_to_sourcekit
  end

  hash
end