class Jazzy::DocIndex

This class stores an index of symbol names for doing name lookup when resolving custom categories and autolinks.

Attributes

root_scope[R]

Public Class Methods

new(all_decls) click to toggle source
# File lib/jazzy/doc_index.rb, line 76
def initialize(all_decls)
  @root_scope = Scope.new_root(all_decls.group_by(&:module_name))
end

Public Instance Methods

lookup(name, context = nil) click to toggle source

Look up a name and return the matching SourceDeclaration or nil.

‘context` is an optional SourceDeclaration indicating where the text was found, affects name resolution - see `lookup_context()` below.

# File lib/jazzy/doc_index.rb, line 84
def lookup(name, context = nil)
  lookup_name = LookupName.new(name)

  return lookup_fully_qualified(lookup_name) if lookup_name.fully_qualified?
  return lookup_guess(lookup_name) if context.nil?

  lookup_context(lookup_name, context)
end

Private Instance Methods

lookup_context(lookup_name, context) click to toggle source

Look up a name from a declaration context, approximately how Swift resolves names.

1 - try and resolve with a common prefix, eg. ‘B’ from ‘T.A’

can match 'T.B', or 'R' from 'S.T.A' can match 'S.R'.

2 - try and resolve as a top-level symbol from a different module 3 - (affordance for docs writers) resolve as a child of the context,

eg. 'B' from 'T.A' can match 'T.A.B' *only if* (1,2) fail.
Currently disabled for Swift for back-compatibility.
# File lib/jazzy/doc_index.rb, line 121
def lookup_context(lookup_name, context)
  context_scope_path =
    root_scope.lookup_path(context.fully_qualified_module_name_parts)

  context_scope = context_scope_path.pop
  context_scope_path.reverse.each do |scope|
    if decl = scope.lookup(lookup_name.parts)
      return decl
    end
  end

  lookup_guess(lookup_name) ||
    (lookup_name.objc? && context_scope.lookup(lookup_name.parts))
end
lookup_fully_qualified(lookup_name) click to toggle source

Look up a fully-qualified name, ie. it starts with the module name.

# File lib/jazzy/doc_index.rb, line 96
def lookup_fully_qualified(lookup_name)
  root_scope.lookup(lookup_name.parts)
end
lookup_guess(lookup_name) click to toggle source

Look up a top-level name best-effort, searching for a module that has it before trying the first name-part as a module name.

# File lib/jazzy/doc_index.rb, line 102
def lookup_guess(lookup_name)
  root_scope.children.each_value do |module_scope|
    if result = module_scope.lookup(lookup_name.parts)
      return result
    end
  end

  lookup_fully_qualified(lookup_name)
end