Class: Jazzy::SymbolGraph::Symbol

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

Overview

A Symbol is a tidied-up SymbolGraph JSON object

Constant Summary collapse

KIND_MAP =

Mapping SymbolGraph’s declkinds to SourceKit

{
  'class' => 'class',
  'struct' => 'struct',
  'enum' => 'enum',
  'enum.case' => 'enumelement', # intentional
  'protocol' => 'protocol',
  'init' => 'function.constructor',
  'deinit' => 'function.destructor',
  'func.op' => 'function.operator',
  'type.method' => 'function.method.class',
  'static.method' => 'function.method.static',
  'method' => 'function.method.instance',
  'func' => 'function.free',
  'type.property' => 'var.class',
  'static.property' => 'var.static',
  'property' => 'var.instance',
  'var' => 'var.global',
  'subscript' => 'function.subscript',
  'type.subscript' => 'function.subscript',
  'static.subscript' => 'function.subscript',
  'typealias' => 'typealias',
  'associatedtype' => 'associatedtype',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Symbol

Returns a new instance of Symbol.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jazzy/symbol_graph/symbol.rb', line 21

def initialize(hash)
  self.usr = hash[:identifier][:precise]
  self.path_components = hash[:pathComponents]
  raw_decl = hash[:declarationFragments].map { |f| f[:spelling] }.join
  init_kind(hash[:kind][:identifier])
  init_declaration(raw_decl)
  init_acl(hash[:accessLevel])
  if location = hash[:location]
    init_location(location)
  end
  init_constraints(hash, raw_decl)
  if comments_hash = hash[:docComment]
    init_doc_comments(comments_hash)
  end
  init_availability(hash[:availability] || [])
  init_generic_type_params(hash)
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



10
11
12
# File 'lib/jazzy/symbol_graph/symbol.rb', line 10

def acl
  @acl
end

#availabilityObject

array, can be empty



14
15
16
# File 'lib/jazzy/symbol_graph/symbol.rb', line 14

def availability
  @availability
end

#constraintsObject

array, can be empty



12
13
14
# File 'lib/jazzy/symbol_graph/symbol.rb', line 12

def constraints
  @constraints
end

#declarationObject

Returns the value of attribute declaration.



8
9
10
# File 'lib/jazzy/symbol_graph/symbol.rb', line 8

def declaration
  @declaration
end

#doc_commentsObject

can be nil



13
14
15
# File 'lib/jazzy/symbol_graph/symbol.rb', line 13

def doc_comments
  @doc_comments
end

#generic_type_paramsObject

set, can be empty



15
16
17
# File 'lib/jazzy/symbol_graph/symbol.rb', line 15

def generic_type_params
  @generic_type_params
end

#kindObject

Returns the value of attribute kind.



9
10
11
# File 'lib/jazzy/symbol_graph/symbol.rb', line 9

def kind
  @kind
end

#locationObject

can be nil, keys :filename :line :character



11
12
13
# File 'lib/jazzy/symbol_graph/symbol.rb', line 11

def location
  @location
end

#path_componentsObject

Returns the value of attribute path_components.



7
8
9
# File 'lib/jazzy/symbol_graph/symbol.rb', line 7

def path_components
  @path_components
end

#usrObject

Returns the value of attribute usr.



6
7
8
# File 'lib/jazzy/symbol_graph/symbol.rb', line 6

def usr
  @usr
end

Instance Method Details

#<=>(other) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/jazzy/symbol_graph/symbol.rb', line 182

def <=>(other)
  # Things with location: order by file/line/column
  # (pls tell what wheel i am reinventing :/)
  if location && other_loc = other.location
    if location[:filename] == other_loc[:filename]
      if location[:line] == other_loc[:line]
        return location[:character] <=> other_loc[:character]
      end
      return location[:line] <=> other_loc[:line]
    end
    return location[:filename] <=> other_loc[:filename]
  end

  # Things with a location before things without a location
  return +1 if location.nil? && other.location
  return -1 if location && other.location.nil?

  # Things without a location: by name and then USR
  return usr <=> other.usr if name == other.name
  name <=> other.name
end

#adjust_kind_for_declaration(kind) ⇒ Object

We treat ‘static var’ differently to ‘class var’



80
81
82
83
# File 'lib/jazzy/symbol_graph/symbol.rb', line 80

def adjust_kind_for_declaration(kind)
  return kind unless declaration =~ /\bstatic\b/
  kind.gsub(/type/, 'static')
end

#decode_version(hash) ⇒ Object



170
171
172
173
174
175
# File 'lib/jazzy/symbol_graph/symbol.rb', line 170

def decode_version(hash)
  str = hash[:major].to_s
  str += ".#{hash[:minor]}" if hash[:minor]
  str += ".#{hash[:patch]}" if hash[:patch]
  str
end

#init_acl(acl) ⇒ Object

Mapping SymbolGraph’s ACL to SourceKit



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

def init_acl(acl)
  self.acl = 'source.lang.swift.accessibility.' + acl
end

#init_availability(avail_hash_list) ⇒ Object

Availability Re-encode this as Swift. Should really teach Jazzy about these, could maybe then do something smarter here.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jazzy/symbol_graph/symbol.rb', line 146

def init_availability(avail_hash_list)
  self.availability = avail_hash_list.map do |avail|
    str = '@available('
    if avail[:isUnconditionallyDeprecated]
      str += '*, deprecated'
    elsif domain = avail[:domain]
      str += domain
      i[introduced deprecated obsoleted].each do |event|
        if version = avail[event]
          str += ", #{event}: #{decode_version(version)}"
        end
      end
    else
      warn "Found confusing availability: #{avail}"
      next nil
    end

    str += ', message: "' + avail[:message] + '"' if avail[:message]
    str += ', renamed: "' + avail[:renamed] + '"' if avail[:renamed]

    str + ')'
  end.compact
end

#init_constraints(hash, raw_decl) ⇒ Object

Generic constraints: in one or both of two places. There can be duplicates; these are removed by ‘Constraint`.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/jazzy/symbol_graph/symbol.rb', line 109

def init_constraints(hash, raw_decl)
  raw_constraints = i[swiftGenerics swiftExtension].flat_map do |key|
    next [] unless container = hash[key]
    container[:constraints] || []
  end

  constraints =
    Constraint.new_list_for_symbol(raw_constraints, path_components)
  if raw_decl =~ / where (.*)$/
    constraints +=
      Constraint.new_list_from_declaration(Regexp.last_match[1])
  end

  self.constraints = constraints.sort.uniq
end

#init_declaration(raw_decl) ⇒ Object

Repair problems with SymbolGraph’s declprinter



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jazzy/symbol_graph/symbol.rb', line 41

def init_declaration(raw_decl)
  # Too much 'Self.TypeName'; omitted arg labels look odd;
  # duplicated constraints; swift 5.3 vs. master workaround
  self.declaration =
    raw_decl.gsub(/\bSelf\./, '')
            .gsub(/(?<=\(|, )_: /, '_ arg: ')
            .gsub(/ where.*$/, '')
  if kind == 'source.lang.swift.decl.class'
    declaration.sub!(/\s*:.*$/, '')
  end
end

#init_doc_comments(comments_hash) ⇒ Object



137
138
139
140
141
# File 'lib/jazzy/symbol_graph/symbol.rb', line 137

def init_doc_comments(comments_hash)
  self.doc_comments =
    comments_hash[:lines].map { |l| l[:text] }
                         .join("\n")
end

#init_generic_type_params(hash) ⇒ Object

Generic type params



126
127
128
129
130
131
132
133
134
135
# File 'lib/jazzy/symbol_graph/symbol.rb', line 126

def init_generic_type_params(hash)
  self.generic_type_params = Set.new(
    if (generics = hash[:swiftGenerics]) &&
       (parameters = generics[:parameters])
      parameters.map { |p| p[:name] }
    else
      []
    end,
  )
end

#init_kind(kind) ⇒ Object



85
86
87
88
89
90
# File 'lib/jazzy/symbol_graph/symbol.rb', line 85

def init_kind(kind)
  adjusted = adjust_kind_for_declaration(kind)
  sourcekit_kind = KIND_MAP[adjusted.sub('swift.', '')]
  raise "Unknown symbol kind '#{kind}'" unless sourcekit_kind
  self.kind = 'source.lang.swift.decl.' + sourcekit_kind
end

#init_location(loc_hash) ⇒ Object

Symbol location - only available for public+ decls



100
101
102
103
104
105
# File 'lib/jazzy/symbol_graph/symbol.rb', line 100

def init_location(loc_hash)
  self.location = {}
  location[:filename] = loc_hash[:uri].sub(%r{^file://}, '')
  location[:line] = loc_hash[:position][:line]
  location[:character] = loc_hash[:position][:character]
end

#nameObject



17
18
19
# File 'lib/jazzy/symbol_graph/symbol.rb', line 17

def name
  path_components[-1] || '??'
end