Skip to content

Commit 8c51b69

Browse files
committed
Different approach for nested parameter docs
Instead of parsing nested parameter documentation differently, which can have source-breaking effects, capture all the unfiltered body nodes when initializing a `DocumentationComment` from markup. This lets us choose whether to use the filtered body (at the top level of a doc comment) or the full list of body nodes (within a parameter's nested documentation).
1 parent 6e55e6c commit 8c51b69

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

Sources/SwiftFormat/Core/DocumentationComment.swift

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public struct DocumentationComment {
7575
/// `Throws:` prefix removed for convenience.
7676
public var `throws`: Paragraph? = nil
7777

78+
/// A collection of _all_ body nodes at the top level of the comment text.
79+
///
80+
/// If a brief summary paragraph was extracted from the comment, it will not be present in this
81+
/// collection. Any special fields extracted (parameters, returns, and throws) from `bodyNodes`
82+
/// will be present in this collection.
83+
internal var allBodyNodes: [Markup] = []
84+
7885
/// Creates a new `DocumentationComment` with information extracted from the leading trivia of the
7986
/// given syntax node.
8087
///
@@ -107,6 +114,9 @@ public struct DocumentationComment {
107114
remainingChildren = markup.children.dropFirst(0)
108115
}
109116

117+
// Capture all the body nodes before filtering out any special fields.
118+
allBodyNodes = remainingChildren.map { $0.detachedFromParent }
119+
110120
for child in remainingChildren {
111121
if var list = child.detachedFromParent as? UnorderedList {
112122
// An unordered list could be one of the following:
@@ -141,24 +151,6 @@ public struct DocumentationComment {
141151
}
142152
}
143153

144-
/// Creates a new `DocumentationComment` from the given `Markup` node, treated as
145-
/// the documentation for a parameter.
146-
///
147-
/// Within the `parameterMarkup` node, only the brief summary is treated separately.
148-
/// All other nodes are treated as body nodes, without special treatment for parameters
149-
/// or return/throws documentation.
150-
private init(parameterMarkup markup: Markup) {
151-
// Extract the first paragraph as the brief summary. It will *not* be included in the body
152-
// nodes.
153-
if let firstParagraph = markup.child(through: [(0, Paragraph.self)]) {
154-
briefSummary = firstParagraph.detachedFromParent as? Paragraph
155-
bodyNodes = markup.children.dropFirst().map { $0.detachedFromParent }
156-
} else {
157-
briefSummary = nil
158-
bodyNodes = markup.children.map { $0.detachedFromParent }
159-
}
160-
}
161-
162154
/// Extracts parameter fields in an outlined parameters list (i.e., `- Parameters:` containing a
163155
/// nested list of parameter fields) from the given unordered list.
164156
///
@@ -245,7 +237,7 @@ public struct DocumentationComment {
245237
let name = rewriter.parameterName
246238
else { return nil }
247239

248-
return Parameter(name: name, comment: DocumentationComment(parameterMarkup: newListItem))
240+
return Parameter(name: name, comment: DocumentationComment(markup: newListItem))
249241
}
250242

251243
/// Extracts simple fields like `- Returns:` and `- Throws:` from the top-level list in the
@@ -494,7 +486,7 @@ extension DocumentationComment.Parameter {
494486
let label = asSingle ? "Parameter \(name):" : "\(name):"
495487
let summaryWithLabel = summary.prefixed(with: label)
496488
return ListItem(
497-
[summaryWithLabel] + comment.bodyNodes.map { $0 as! BlockMarkup }
489+
[summaryWithLabel] + comment.allBodyNodes.map { $0 as! BlockMarkup }
498490
)
499491
}
500492
}

0 commit comments

Comments
 (0)