class Jazzy::SymbolGraph::SymNode

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

Attributes

override[W]
protocol_requirement[W]
superclass_name[RW]
symbol[RW]
unlisted[W]

Public Class Methods

new(symbol) click to toggle source
Calls superclass method Jazzy::SymbolGraph::BaseNode::new
# File lib/jazzy/symbol_graph/sym_node.rb, line 50
def initialize(symbol)
  self.symbol = symbol
  super()
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 159
def <=>(other)
  symbol <=> other.symbol
end
actor?() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 67
def actor?
  symbol.kind.end_with?('actor')
end
async?() click to toggle source

approximately…

# File lib/jazzy/symbol_graph/sym_node.rb, line 123
def async?
  symbol.declaration =~ /\basync\b[^)]*$/
end
conformance?(protocol) click to toggle source

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

# File lib/jazzy/symbol_graph/sym_node.rb, line 104
def conformance?(protocol)
  return false unless symbol.declaration =~ /(?<=:).*?(?=(where|$))/

  Regexp.last_match[0] =~ /\b#{protocol}\b/
end
constraints() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 71
def constraints
  symbol.constraints
end
full_declaration() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 127
def full_declaration
  symbol.attributes
    .append(symbol.declaration + inherits_clause + where_clause)
    .join("\n")
end
inherits_clause() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 116
def inherits_clause
  return '' unless superclass_name

  " : #{superclass_name}"
end
override?() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 38
def override?
  @override
end
parent_qualified_name() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 59
def parent_qualified_name
  symbol.path_components[0...-1].join('.')
end
protocol?() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 63
def protocol?
  symbol.kind.end_with?('protocol')
end
protocol_requirement?() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 42
def protocol_requirement?
  @protocol_requirement
end
qualified_name() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 55
def qualified_name
  symbol.path_components.join('.')
end
to_sourcekit(module_name) click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 133
def to_sourcekit(module_name)
  declaration = full_declaration
  xml_declaration = "<swift>#{CGI.escapeHTML(declaration)}</swift>"

  hash = {
    'key.kind' => symbol.kind,
    'key.usr' => symbol.usr,
    'key.name' => symbol.name,
    'key.modulename' => module_name,
    'key.parsed_declaration' => declaration,
    'key.annotated_decl' => xml_declaration,
    'key.symgraph_async' => async?,
  }
  if params = symbol.parameter_names
    hash['key.doc.parameters'] =
      params.map { |name| { 'name' => name } }
  end
  hash['key.symgraph_spi'] = true if symbol.spi

  add_children_to_sourcekit(hash, module_name)
  symbol.add_to_sourcekit(hash)
end
top_level_decl?() click to toggle source
# File lib/jazzy/symbol_graph/sym_node.rb, line 46
def top_level_decl?
  !@unlisted && parent.nil?
end
try_add_child(node, unique_context_constraints) click to toggle source

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
# File lib/jazzy/symbol_graph/sym_node.rb, line 79
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) click to toggle source

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.

# File lib/jazzy/symbol_graph/sym_node.rb, line 92
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_clause() click to toggle source

Generate the ‘where’ clause for the declaration

# File lib/jazzy/symbol_graph/sym_node.rb, line 111
def where_clause
  parent_constraints = parent&.constraints || []
  (constraints - parent_constraints).to_where_clause
end