class Jazzy::DocIndex::Scope
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.
Attributes
children[R]
decl[R]
Public Class Methods
new(decl, children)
click to toggle source
# File lib/jazzy/doc_index.rb, line 16 def initialize(decl, children) @decl = decl @children = children end
new_decl(decl, child_decls)
click to toggle source
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).
# 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)
click to toggle source
# 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
Public Instance Methods
lookup(parts)
click to toggle source
Lookup of a name like ‘Mod.Type.method(arg:)` requires passing an array of name ’parts’ eg. [‘Mod’, ‘Type’, ‘method(arg:)’].
# 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)
click to toggle source
Get an array of scopes matching the name parts.
# File lib/jazzy/doc_index.rb, line 68 def lookup_path(parts) [self] + (children[parts.first]&.lookup_path(parts[1...]) || []) end
merge(new_scope)
click to toggle source
# 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