-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathMarkupTreeDumper.swift
302 lines (260 loc) · 10.6 KB
/
MarkupTreeDumper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
fileprivate extension String {
/// A version of `self` with newline "\n" characters escaped as "\\n".
var newlineEscaped: String {
return replacingOccurrences(of: "\n", with: "\\n")
}
}
/// Options when printing a debug description of a markup tree.
public struct MarkupDumpOptions: OptionSet {
public let rawValue: UInt
public init(rawValue: UInt) {
self.rawValue = rawValue
}
/// Include source locations and ranges of each element in the dump.
public static let printSourceLocations = MarkupDumpOptions(rawValue: 1 << 0)
/// Include internal unique identifiers of each element in the dump.
public static let printUniqueIdentifiers = MarkupDumpOptions(rawValue: 1 << 1)
/// Print all optional information about a markup tree.
public static let printEverything: MarkupDumpOptions = [
.printSourceLocations,
.printUniqueIdentifiers,
]
}
extension Markup {
/// Print a debug representation of the tree.
/// - Parameter options: options to use while printing.
/// - Returns: a description illustrating the hierarchy and contents of each element of the tree.
public func debugDescription(options: MarkupDumpOptions = []) -> String {
var dumper = MarkupTreeDumper(options: options)
dumper.visit(self)
return dumper.result
}
}
/// A `MarkupWalker` that dumps a textual representation of a `Markup` tree for debugging.
///
/// - note: This type is utilized by a public `Markup.dump()` method available on all markup elements.
///
/// For example, this doc comment parsed as markdown would look like the following.
///
/// ```plain
/// Document
/// ├─ Paragraph
/// │ ├─ Text "A "
/// │ ├─ InlineCode `MarkupWalker`
/// │ ├─ Text " that dumps a textual representation of a "
/// │ ├─ InlineCode `Markup`
/// │ └─ Text " tree for debugging."
/// ├─ UnorderedList
/// │ └─ ListItem
/// │ └─ Paragraph
/// │ ├─ Text "note: This type is utilized by a public "
/// │ ├─ InlineCode `Markup.dump()`
/// │ └─ Text " method available on all markup elements."
/// └─ Paragraph
/// └─ Text "For example, this doc comment parsed as markdown would look like the following."
/// ```
struct MarkupTreeDumper: MarkupWalker {
let options: MarkupDumpOptions
init(options: MarkupDumpOptions) {
self.options = options
}
/// The resulting string built up during dumping.
var result = ""
/// The current path in the tree so far, used for printing edges
/// in the dumped tree.
private var path = [Markup]()
private mutating func dump(_ markup: Markup, customDescription: String? = nil) {
indent(markup)
result += "\(type(of: markup))"
if options.contains(.printSourceLocations),
let range = markup.range {
result += " @\(range.diagnosticDescription(includePath: false))"
}
if options.contains(.printUniqueIdentifiers) {
if markup.parent == nil {
result += " Root #\(markup._data.id.rootId)"
}
result += " #\(markup._data.id.childId)"
}
if let customDescription = customDescription {
if !customDescription.starts(with: "\n") {
result += " "
}
result += "\(customDescription)"
}
increasingDepth(markup)
}
mutating func defaultVisit(_ markup: Markup) {
dump(markup)
}
private var lineIndentPrefix: String {
var prefix = ""
for (depth, element) in path.enumerated().reversed() {
guard let lastChildIndex = element.parent?.children.reversed().first?.indexInParent,
lastChildIndex != element.indexInParent else {
if depth > 0 {
prefix.append(" ")
}
continue
}
prefix.append(" │")
}
return String(prefix.reversed())
}
private mutating func indentLiteralBlock(_ string: String, from element: Markup, countLines: Bool = false) -> String {
path.append(element)
let prefix = lineIndentPrefix
let result = string.split(separator: "\n").enumerated().map { (index, line) in
let lineNumber = countLines ? "\(index + 1) " : ""
return "\(prefix)\(lineNumber)\(line)"
}.joined(separator: "\n")
path.removeLast()
return result
}
/**
Add an indentation prefix for a markup element using the current `path`.
- parameter markup: The `Markup` element about to be printed
*/
private mutating func indent(_ markup: Markup) {
if !path.isEmpty {
result.append("\n")
}
result += lineIndentPrefix
guard let lastChildIndex = markup.parent?.children.reversed().first?.indexInParent else {
return
}
let treeMarker = markup.indexInParent == lastChildIndex ? "└─ " : "├─ "
result.append(treeMarker)
}
/**
Push `element` to the current path and descend into the children, popping `element` from the path when returning.
- parameter element: The parent element you're descending into.
*/
private mutating func increasingDepth(_ element: Markup) {
path.append(element)
descendInto(element)
path.removeLast()
}
mutating func visitText(_ text: Text) {
dump(text, customDescription: "\"\(text.string)\"")
}
mutating func visitHTMLBlock(_ html: HTMLBlock) {
dump(html, customDescription: "\n\(indentLiteralBlock(html.rawHTML, from: html))")
}
mutating func visitLink(_ link: Link) {
dump(link, customDescription: link.destination.map { "destination: \"\($0)\"" } ?? "")
}
mutating func visitImage(_ image: Image) {
var description = image.source.map { "source: \"\($0)\"" } ?? ""
if let title = image.title {
description += " title: \"\(title)\""
}
dump(image, customDescription: description)
}
mutating func visitHeading(_ heading: Heading) {
dump(heading, customDescription: "level: \(heading.level)")
}
mutating func visitOrderedList(_ orderedList: OrderedList) {
if orderedList.startIndex != 1 {
dump(orderedList, customDescription: "startIndex: \(orderedList.startIndex)")
} else {
defaultVisit(orderedList)
}
}
mutating func visitCodeBlock(_ codeBlock: CodeBlock) {
let lines = indentLiteralBlock(codeBlock.code, from: codeBlock, countLines: false)
dump(codeBlock, customDescription: "language: \(codeBlock.language ?? "none")\n\(lines)")
}
mutating func visitBlockDirective(_ blockDirective: BlockDirective) {
var argumentsDescription: String
if !blockDirective.argumentText.segments.isEmpty {
var description = "\n"
description += "├─ Argument text segments:\n"
description += blockDirective.argumentText.segments.map { segment -> String in
let range: String
if options.contains(.printSourceLocations) {
range = segment.range.map { "@\($0.diagnosticDescription()): " } ?? ""
} else {
range = ""
}
let segmentText = segment.untrimmedText[segment.parseIndex...].debugDescription
return "| \(range)\(segmentText)"
}.joined(separator: "\n")
argumentsDescription = "\n" + indentLiteralBlock(description, from: blockDirective)
} else {
argumentsDescription = ""
}
dump(blockDirective, customDescription: "name: \(blockDirective.name.debugDescription)\(argumentsDescription)")
}
mutating func visitInlineCode(_ inlineCode: InlineCode) {
dump(inlineCode, customDescription: "`\(inlineCode.code)`")
}
mutating func visitInlineHTML(_ inlineHTML: InlineHTML) {
dump(inlineHTML, customDescription: "\(inlineHTML.rawHTML)")
}
mutating func visitCustomInline(_ customInline: CustomInline) {
dump(customInline, customDescription: "customInline.text")
}
mutating func visitListItem(_ listItem: ListItem) {
let checkboxDescription: String? = listItem.checkbox.map {
switch $0 {
case .checked: return "checkbox: [x]"
case .unchecked: return "checkbox: [ ]"
}
}
dump(listItem, customDescription: checkboxDescription)
}
mutating func visitTable(_ table: Table) {
let alignments = table.columnAlignments.map {
switch $0 {
case nil:
return "-"
case .left:
return "l"
case .right:
return "r"
case .center:
return "c"
}
}.joined(separator: "|")
dump(table, customDescription: "alignments: |\(alignments)|")
}
mutating func visitSymbolLink(_ symbolLink: SymbolLink) {
dump(symbolLink, customDescription: symbolLink.destination.map { "destination: \($0)" })
}
mutating func visitTableCell(_ tableCell: Table.Cell) {
var desc = ""
if tableCell.colspan != 1 {
desc += " colspan: \(tableCell.colspan)"
}
if tableCell.rowspan != 1 {
desc += " rowspan: \(tableCell.rowspan)"
}
desc = desc.trimmingCharacters(in: .whitespaces)
if !desc.isEmpty {
dump(tableCell, customDescription: desc)
} else {
dump(tableCell)
}
}
mutating func visitInlineAttributes(_ attributes: InlineAttributes) -> () {
dump(attributes, customDescription: "attributes: `\(attributes.attributes)`")
}
mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) -> () {
dump(doxygenParam, customDescription: "parameter: \(doxygenParam.name)")
}
mutating func visitFootnoteReference(_ footnoteReference: FootnoteReference) -> () {
dump(footnoteReference, customDescription: "footnoteID: `\(footnoteReference.footnoteID)`")
}
mutating func visitFootnoteDefinition(_ footnoteDefinition: FootnoteDefinition) -> () {
dump(footnoteDefinition, customDescription: "footnoteID: `\(footnoteDefinition.footnoteID)`")
}
}