Skip to content

Commit 3673ec3

Browse files
refactor: turn RenderBlockContent's per-case data into separate structs (#358)
A collection of refactors to RenderBlockContent that change any associated data in its enum cases to a separate struct. Without this change, any new data being added to these cases would become a source-breaking change to anything that used Swift-DocC as a library. Resolves rdar://97382349
1 parent f730fe5 commit 3673ec3

28 files changed

+540
-346
lines changed

Sources/SwiftDocC/Indexing/RenderBlockContent+TextIndexing.swift

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,55 @@
1111
extension RenderBlockContent: TextIndexing {
1212
public var headings: [String] {
1313
switch self {
14-
case .heading(_, let text, _):
15-
return [text]
14+
case .heading(let h):
15+
return [h.text]
1616
default:
1717
return []
1818
}
1919
}
2020

2121
public func rawIndexableTextContent(references: [String : RenderReference]) -> String {
2222
switch self {
23-
case let .aside(_, blocks):
24-
return blocks.rawIndexableTextContent(references: references)
25-
case let .orderedList(items):
26-
return items.map {
23+
case let .aside(a):
24+
return a.content.rawIndexableTextContent(references: references)
25+
case let .orderedList(l):
26+
return l.items.map {
2727
$0.content.rawIndexableTextContent(references: references)
2828
}.joined(separator: " ")
29-
case let .paragraph(blocks):
30-
return blocks.rawIndexableTextContent(references: references)
31-
case let .step(blocks, caption, _, _, _):
32-
return (blocks + caption).rawIndexableTextContent(references: references)
33-
case let .unorderedList(items):
34-
return items.map {
29+
case let .paragraph(p):
30+
return p.inlineContent.rawIndexableTextContent(references: references)
31+
case let .step(s):
32+
return (s.content + s.caption).rawIndexableTextContent(references: references)
33+
case let .unorderedList(l):
34+
return l.items.map {
3535
$0.content.rawIndexableTextContent(references: references)
3636
}.joined(separator: " ")
37-
case .codeListing(_, _, let metadata):
38-
return metadata?.rawIndexableTextContent(references: references) ?? ""
39-
case let .heading(_, text, _):
40-
return text
37+
case let .codeListing(l):
38+
return l.metadata?.rawIndexableTextContent(references: references) ?? ""
39+
case let .heading(h):
40+
return h.text
4141
case .endpointExample:
4242
return ""
43-
case .dictionaryExample(summary: let summary, example: _):
44-
return summary?.rawIndexableTextContent(references: references) ?? ""
45-
case .table(_, let rows, let metadata):
46-
let content = rows.map {
43+
case .dictionaryExample(let e):
44+
return e.summary?.rawIndexableTextContent(references: references) ?? ""
45+
case .table(let t):
46+
let content = t.rows.map {
4747
return $0.cells.map {
4848
return $0.rawIndexableTextContent(references: references)
4949
}.joined(separator: " ")
5050
}.joined(separator: " ")
5151

52-
let meta = metadata?.rawIndexableTextContent(references: references) ?? ""
52+
let meta = t.metadata?.rawIndexableTextContent(references: references) ?? ""
5353

5454
return content + " " + meta
55-
case .termList(let items):
56-
return items.map {
55+
case .termList(let l):
56+
return l.items.map {
5757
let definition = $0.definition.content.rawIndexableTextContent(references: references)
5858
return $0.term.inlineContent.rawIndexableTextContent(references: references)
5959
+ ( definition.isEmpty ? "" : " \(definition)" )
6060
}.joined(separator: " ")
61+
default:
62+
fatalError("unknown RenderBlockContent case in rawIndexableTextContent")
6163
}
6264
}
6365
}

Sources/SwiftDocC/Indexing/RenderNode+Indexable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension RenderNode {
2828
return sections
2929
}
3030

31-
return [ContentRenderSection(kind: .content, content: [.paragraph(inlineContent: abstract ?? [])])]
31+
return [ContentRenderSection(kind: .content, content: [.paragraph(.init(inlineContent: abstract ?? []))])]
3232
+ primaryContentSections
3333
}
3434
}
@@ -56,7 +56,7 @@ extension RenderNode: Indexable {
5656

5757
let summaryParagraph: RenderBlockContent?
5858
if let abstract = self.abstract {
59-
summaryParagraph = RenderBlockContent.paragraph(inlineContent: abstract)
59+
summaryParagraph = RenderBlockContent.paragraph(.init(inlineContent: abstract))
6060
} else if let intro = self.sections.first as? IntroRenderSection, let firstBlock = intro.content.first, case .paragraph = firstBlock {
6161
summaryParagraph = firstBlock
6262
} else {

Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ extension Abstracted {
256256
/// - Parameter compiler: The content compiler to render the abstract.
257257
/// - Returns: The rendered abstract, or `nil` of the element doesn't have an abstract.
258258
func renderedAbstract(using compiler: inout RenderContentCompiler) -> LinkDestinationSummary.Abstract? {
259-
guard let abstract = abstract, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(abstract).first else {
259+
guard let abstract = abstract, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(abstract).first else {
260260
return nil
261261
}
262-
return inlineContent
262+
return p.inlineContent
263263
}
264264
}
265265

@@ -303,10 +303,10 @@ extension LinkDestinationSummary {
303303
let title = symbol.titleVariants[summaryTrait] ?? symbol.title
304304

305305
func renderSymbolAbstract(_ symbolAbstract: Paragraph?) -> Abstract? {
306-
guard let abstractParagraph = symbolAbstract, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(abstractParagraph).first else {
306+
guard let abstractParagraph = symbolAbstract, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(abstractParagraph).first else {
307307
return nil
308308
}
309-
return inlineContent
309+
return p.inlineContent
310310
}
311311

312312
let abstract = renderSymbolAbstract(symbol.abstractVariants[summaryTrait] ?? symbol.abstract)
@@ -387,8 +387,8 @@ extension LinkDestinationSummary {
387387
let abstract: Abstract?
388388
if let abstracted = landmark as? Abstracted {
389389
abstract = abstracted.renderedAbstract(using: &compiler) ?? []
390-
} else if let paragraph = landmark.markup.children.lazy.compactMap({ $0 as? Paragraph }).first, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(paragraph).first {
391-
abstract = inlineContent
390+
} else if let paragraph = landmark.markup.children.lazy.compactMap({ $0 as? Paragraph }).first, case RenderBlockContent.paragraph(let p)? = compiler.visitParagraph(paragraph).first {
391+
abstract = p.inlineContent
392392
} else {
393393
abstract = nil
394394
}

Sources/SwiftDocC/Model/Rendering/Content/Extensions/RenderTermLists.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ extension Collection where Element == RenderBlockContent.ListItem {
7373
/// list items.
7474
private func listWithItems(_ items: [ListableItem]) -> RenderContent {
7575
if let unorderedListItems = items as? [RenderBlockContent.ListItem] {
76-
return RenderBlockContent.unorderedList(items: unorderedListItems)
76+
return RenderBlockContent.unorderedList(.init(items: unorderedListItems))
7777
} else if let termListItems = items as? [RenderBlockContent.TermListItem] {
78-
return RenderBlockContent.termList(items: termListItems)
78+
return RenderBlockContent.termList(.init(items: termListItems))
7979
} else {
8080
fatalError()
8181
}
@@ -89,7 +89,7 @@ extension RenderBlockContent.TermListItem {
8989
/// given list item is not deemed to be a term list item, this
9090
/// returns `nil`.
9191
init?(_ listItem: RenderBlockContent.ListItem) {
92-
guard case let .paragraph(firstParagraphInlines) = listItem.content.first else {
92+
guard case let .paragraph(firstParagraph) = listItem.content.first else {
9393
// The first child of the list item wasn't a paragraph, so
9494
// don't continue checking to see if this is a term list item.
9595
return nil
@@ -98,7 +98,7 @@ extension RenderBlockContent.TermListItem {
9898

9999
// Collapse any contiguous text elements before checking
100100
// for term indication
101-
let collapsedFirstParagraphInlines = firstParagraphInlines.collapsingContiguousTextElements()
101+
let collapsedFirstParagraphInlines = firstParagraph.inlineContent.collapsingContiguousTextElements()
102102

103103
let termDefinitionSeparator = ":"
104104
guard let (termInlines, firstDefinitionInlines) = collapsedFirstParagraphInlines.separatedForTermDefinition(separator: termDefinitionSeparator) else {
@@ -112,8 +112,8 @@ extension RenderBlockContent.TermListItem {
112112
// Use the definition contents from the first paragraph along
113113
// with the subsequent block elements in this list item as the
114114
// complete definition.
115-
let definition = RenderBlockContent.TermListItem.Definition(content: [RenderBlockContent.paragraph(inlineContent: firstDefinitionInlines)] + subsequentBlockContents)
116-
115+
let definition = RenderBlockContent.TermListItem.Definition(content: [RenderBlockContent.paragraph(.init(inlineContent: firstDefinitionInlines))] + subsequentBlockContents)
116+
117117
self = RenderBlockContent.TermListItem(term: term, definition: definition)
118118
}
119119
}

0 commit comments

Comments
 (0)