Class: Jazzy::DocIndex::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/jazzy/doc_index.rb

Overview

A node in the index tree. The root has no decl; its children are per-module indexed by module names. The second level, where each scope is a module, also has no decl; its children are scopes, one for each top-level decl in the module. From the third level onwards the decl is valid.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decl, children) ⇒ Scope

Returns a new instance of Scope.



16
17
18
19
# File 'lib/jazzy/doc_index.rb', line 16

def initialize(decl, children)
  @decl = decl
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

String:Scope



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

def children
  @children
end

#declObject (readonly)

SourceDeclaration



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

def decl
  @decl
end

Class Method Details

.new_decl(decl, child_decls) ⇒ Object

Decl names in a scope are usually unique. The exceptions are (1) methods and (2) typealias+extension, which historically jazzy does not merge. The logic here and in ‘merge()` below preserves the historical ambiguity-resolution of (1) and tries to do the best for (2).



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jazzy/doc_index.rb', line 33

def self.new_decl(decl, child_decls)
  child_scopes = {}
  child_decls.flat_map do |child_decl|
    child_scope = Scope.new_decl(child_decl, child_decl.children)
    child_decl.index_names.map do |name|
      if curr = child_scopes[name]
        curr.merge(child_scope)
      else
        child_scopes[name] = child_scope
      end
    end
  end
  new(decl, child_scopes)
end

.new_root(module_decls) ⇒ Object



21
22
23
24
25
26
# File 'lib/jazzy/doc_index.rb', line 21

def self.new_root(module_decls)
  new(nil,
      module_decls.transform_values do |decls|
        Scope.new_decl(nil, decls)
      end)
end

Instance Method Details

#lookup(parts) ⇒ Object

Lookup of a name like ‘Mod.Type.method(arg:)` requires passing an array of name ’parts’ eg. [‘Mod’, ‘Type’, ‘method(arg:)’].



61
62
63
64
65
# File 'lib/jazzy/doc_index.rb', line 61

def lookup(parts)
  return decl if parts.empty?

  children[parts.first]&.lookup(parts[1...])
end

#lookup_path(parts) ⇒ Object

Get an array of scopes matching the name parts.



68
69
70
71
# File 'lib/jazzy/doc_index.rb', line 68

def lookup_path(parts)
  [self] +
    (children[parts.first]&.lookup_path(parts[1...]) || [])
end

#merge(new_scope) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/jazzy/doc_index.rb', line 48

def merge(new_scope)
  return unless type = decl&.type
  return unless new_type = new_scope.decl&.type

  if type.swift_typealias? && new_type.swift_extension?
    @children = new_scope.children
  elsif type.swift_extension? && new_type.swift_typealias?
    @decl = new_scope.decl
  end
end