Skip to content

refactors: turn RenderBlockContent's per-case data into separate structs #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions Sources/SwiftDocC/Indexing/RenderBlockContent+TextIndexing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,55 @@
extension RenderBlockContent: TextIndexing {
public var headings: [String] {
switch self {
case .heading(_, let text, _):
return [text]
case .heading(let h):
return [h.text]
default:
return []
}
}

public func rawIndexableTextContent(references: [String : RenderReference]) -> String {
switch self {
case let .aside(_, blocks):
return blocks.rawIndexableTextContent(references: references)
case let .orderedList(items):
return items.map {
case let .aside(a):
Copy link
Contributor

@ethan-kusters ethan-kusters Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We don't have a lot of single-letter variable names in the codebase thus far. I think it probably fits in a little better to spell out the name of these types instead? (Throughout this PR)

Suggested change
case let .aside(a):
case let .aside(aside):

return a.content.rawIndexableTextContent(references: references)
case let .orderedList(l):
return l.items.map {
$0.content.rawIndexableTextContent(references: references)
}.joined(separator: " ")
case let .paragraph(blocks):
return blocks.rawIndexableTextContent(references: references)
case let .step(blocks, caption, _, _, _):
return (blocks + caption).rawIndexableTextContent(references: references)
case let .unorderedList(items):
return items.map {
case let .paragraph(p):
return p.inlineContent.rawIndexableTextContent(references: references)
case let .step(s):
return (s.content + s.caption).rawIndexableTextContent(references: references)
case let .unorderedList(l):
return l.items.map {
$0.content.rawIndexableTextContent(references: references)
}.joined(separator: " ")
case .codeListing(_, _, let metadata):
return metadata?.rawIndexableTextContent(references: references) ?? ""
case let .heading(_, text, _):
return text
case let .codeListing(l):
return l.metadata?.rawIndexableTextContent(references: references) ?? ""
case let .heading(h):
return h.text
case .endpointExample:
return ""
case .dictionaryExample(summary: let summary, example: _):
return summary?.rawIndexableTextContent(references: references) ?? ""
case .table(_, let rows, let metadata):
let content = rows.map {
case .dictionaryExample(let e):
return e.summary?.rawIndexableTextContent(references: references) ?? ""
case .table(let t):
let content = t.rows.map {
return $0.cells.map {
return $0.rawIndexableTextContent(references: references)
}.joined(separator: " ")
}.joined(separator: " ")

let meta = metadata?.rawIndexableTextContent(references: references) ?? ""
let meta = t.metadata?.rawIndexableTextContent(references: references) ?? ""

return content + " " + meta
case .termList(let items):
return items.map {
case .termList(let l):
return l.items.map {
let definition = $0.definition.content.rawIndexableTextContent(references: references)
return $0.term.inlineContent.rawIndexableTextContent(references: references)
+ ( definition.isEmpty ? "" : " \(definition)" )
}.joined(separator: " ")
default:
fatalError("unknown RenderBlockContent case in rawIndexableTextContent")
}
}
}
4 changes: 2 additions & 2 deletions Sources/SwiftDocC/Indexing/RenderNode+Indexable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension RenderNode {
return sections
}

return [ContentRenderSection(kind: .content, content: [.paragraph(inlineContent: abstract ?? [])])]
return [ContentRenderSection(kind: .content, content: [.paragraph(.init(inlineContent: abstract ?? []))])]
+ primaryContentSections
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@ extension RenderNode: Indexable {

let summaryParagraph: RenderBlockContent?
if let abstract = self.abstract {
summaryParagraph = RenderBlockContent.paragraph(inlineContent: abstract)
summaryParagraph = RenderBlockContent.paragraph(.init(inlineContent: abstract))
} else if let intro = self.sections.first as? IntroRenderSection, let firstBlock = intro.content.first, case .paragraph = firstBlock {
summaryParagraph = firstBlock
} else {
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ extension Abstracted {
/// - Parameter compiler: The content compiler to render the abstract.
/// - Returns: The rendered abstract, or `nil` of the element doesn't have an abstract.
func renderedAbstract(using compiler: inout RenderContentCompiler) -> LinkDestinationSummary.Abstract? {
guard let abstract = abstract, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(abstract).first else {
guard let abstract = abstract, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(abstract).first else {
return nil
}
return inlineContent
return p.inlineContent
}
}

Expand Down Expand Up @@ -303,10 +303,10 @@ extension LinkDestinationSummary {
let title = symbol.titleVariants[summaryTrait] ?? symbol.title

func renderSymbolAbstract(_ symbolAbstract: Paragraph?) -> Abstract? {
guard let abstractParagraph = symbolAbstract, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(abstractParagraph).first else {
guard let abstractParagraph = symbolAbstract, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(abstractParagraph).first else {
return nil
}
return inlineContent
return p.inlineContent
}

let abstract = renderSymbolAbstract(symbol.abstractVariants[summaryTrait] ?? symbol.abstract)
Expand Down Expand Up @@ -387,8 +387,8 @@ extension LinkDestinationSummary {
let abstract: Abstract?
if let abstracted = landmark as? Abstracted {
abstract = abstracted.renderedAbstract(using: &compiler) ?? []
} else if let paragraph = landmark.markup.children.lazy.compactMap({ $0 as? Paragraph }).first, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(paragraph).first {
abstract = inlineContent
} else if let paragraph = landmark.markup.children.lazy.compactMap({ $0 as? Paragraph }).first, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(paragraph).first {
abstract = p.inlineContent
} else {
abstract = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ extension Collection where Element == RenderBlockContent.ListItem {
/// list items.
private func listWithItems(_ items: [ListableItem]) -> RenderContent {
if let unorderedListItems = items as? [RenderBlockContent.ListItem] {
return RenderBlockContent.unorderedList(items: unorderedListItems)
return RenderBlockContent.unorderedList(.init(items: unorderedListItems))
} else if let termListItems = items as? [RenderBlockContent.TermListItem] {
return RenderBlockContent.termList(items: termListItems)
return RenderBlockContent.termList(.init(items: termListItems))
} else {
fatalError()
}
Expand All @@ -89,7 +89,7 @@ extension RenderBlockContent.TermListItem {
/// given list item is not deemed to be a term list item, this
/// returns `nil`.
init?(_ listItem: RenderBlockContent.ListItem) {
guard case let .paragraph(firstParagraphInlines) = listItem.content.first else {
guard case let .paragraph(firstParagraph) = listItem.content.first else {
// The first child of the list item wasn't a paragraph, so
// don't continue checking to see if this is a term list item.
return nil
Expand All @@ -98,7 +98,7 @@ extension RenderBlockContent.TermListItem {

// Collapse any contiguous text elements before checking
// for term indication
let collapsedFirstParagraphInlines = firstParagraphInlines.collapsingContiguousTextElements()
let collapsedFirstParagraphInlines = firstParagraph.inlineContent.collapsingContiguousTextElements()

let termDefinitionSeparator = ":"
guard let (termInlines, firstDefinitionInlines) = collapsedFirstParagraphInlines.separatedForTermDefinition(separator: termDefinitionSeparator) else {
Expand All @@ -112,8 +112,8 @@ extension RenderBlockContent.TermListItem {
// Use the definition contents from the first paragraph along
// with the subsequent block elements in this list item as the
// complete definition.
let definition = RenderBlockContent.TermListItem.Definition(content: [RenderBlockContent.paragraph(inlineContent: firstDefinitionInlines)] + subsequentBlockContents)
let definition = RenderBlockContent.TermListItem.Definition(content: [RenderBlockContent.paragraph(.init(inlineContent: firstDefinitionInlines))] + subsequentBlockContents)

self = RenderBlockContent.TermListItem(term: term, definition: definition)
}
}
Expand Down
Loading