Class: Jazzy::SymbolGraph::SymNode

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

Overview

A SymNode is a node of the reconstructed syntax tree holding a symbol. It can turn itself into SourceKit and helps decode extensions.

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#children, #parent

Instance Method Summary collapse

Methods inherited from BaseNode

#add_child, #children_to_sourcekit

Constructor Details

#initialize(symbol) ⇒ SymNode

Returns a new instance of SymNode.



45
46
47
48
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 45

def initialize(symbol)
  self.symbol = symbol
  super()
end

Instance Attribute Details

#override=(value) ⇒ Object (writeonly)

Sets the attribute override

Parameters:

  • value

    the value to set the attribute override to.



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

def override=(value)
  @override = value
end

#protocol_requirement=(value) ⇒ Object (writeonly)

Sets the attribute protocol_requirement

Parameters:

  • value

    the value to set the attribute protocol_requirement to.



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

def protocol_requirement=(value)
  @protocol_requirement = value
end

#superclass_nameObject

Returns the value of attribute superclass_name.



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

def superclass_name
  @superclass_name
end

#symbolObject

Returns the value of attribute symbol.



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

def symbol
  @symbol
end

#unlisted=(value) ⇒ Object (writeonly)

Sets the attribute unlisted

Parameters:

  • value

    the value to set the attribute unlisted to.



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

def unlisted=(value)
  @unlisted = value
end

Instance Method Details

#<=>(other) ⇒ Object



149
150
151
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 149

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

#conformance?(protocol) ⇒ Boolean

Messy check whether we need to fabricate an extension for a protocol conformance: don’t bother if it’s already in the type declaration.

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 94

def conformance?(protocol)
  return false unless symbol.declaration =~ /(?<=:).*?(?=(where|$))/
  Regexp.last_match[0] =~ /\b#{protocol}\b/
end

#constraintsObject



62
63
64
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 62

def constraints
  symbol.constraints
end

#full_declarationObject



110
111
112
113
114
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 110

def full_declaration
  symbol.availability
        .append(symbol.declaration + inherits_clause + where_clause)
        .join("\n")
end

#inherits_clauseObject



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

def inherits_clause
  return '' unless superclass_name
  " : #{superclass_name}"
end

#override?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 33

def override?
  @override
end

#parent_qualified_nameObject



54
55
56
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 54

def parent_qualified_name
  symbol.path_components[0...-1].join('.')
end

#protocol?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 58

def protocol?
  symbol.kind.end_with?('protocol')
end

#protocol_requirement?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 37

def protocol_requirement?
  @protocol_requirement
end

#qualified_nameObject



50
51
52
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 50

def qualified_name
  symbol.path_components.join('.')
end

#to_sourcekitObject

rubocop:disable Metrics/MethodLength



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 117

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

  hash = {
    'key.kind' => symbol.kind,
    'key.usr' =>  symbol.usr,
    'key.name' => symbol.name,
    'key.accessibility' => symbol.acl,
    'key.parsed_decl' => declaration,
    'key.annotated_decl' => xml_declaration,
  }
  if docs = symbol.doc_comments
    hash['key.doc.comment'] = docs
    hash['key.doc.full_as_xml'] = ''
  end
  if location = symbol.location
    hash['key.filepath'] = location[:filename]
    hash['key.doc.line'] = location[:line]
    hash['key.doc.column'] = location[:character]
  end
  unless children.empty?
    hash['key.substructure'] = children_to_sourcekit
  end

  hash
end

#top_level_decl?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 41

def top_level_decl?
  !@unlisted && parent.nil?
end

#try_add_child(node, unique_context_constraints) ⇒ Object

Add another SymNode as a member if possible. It must go in an extension if either:

- it has different generic constraints to us; or
- we're a protocol and it's a default impl / ext method


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

def try_add_child(node, unique_context_constraints)
  unless unique_context_constraints.empty? &&
         (!protocol? || node.protocol_requirement?)
    return false
  end
  add_child(node)
  true
end

#unique_context_constraints(context) ⇒ Object

The ‘Constraint`s on this decl that are both:

  1. Unique, ie. not just inherited from its context; and

  2. Constraining the *context’s* gen params rather than our own.



82
83
84
85
86
87
88
89
90
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 82

def unique_context_constraints(context)
  return symbol.constraints unless context

  new_generic_type_params =
    symbol.generic_type_params - context.symbol.generic_type_params

  (symbol.constraints - context.symbol.constraints)
    .select { |con| con.type_names.disjoint?(new_generic_type_params) }
end

#where_clauseObject

Generate the ‘where’ clause for the declaration



100
101
102
103
# File 'lib/jazzy/symbol_graph/sym_node.rb', line 100

def where_clause
  parent_constraints = (parent && parent.constraints) || []
  (constraints - parent_constraints).to_where_clause
end