Skip to content

Commit 0b80433

Browse files
authored
Merge pull request #1849 from Matejkob/node-inits-doc-comment
Generate doc-comment for syntax node initializers
2 parents d7be9b6 + b37bff1 commit 0b80433

File tree

10 files changed

+894
-2
lines changed

10 files changed

+894
-2
lines changed

CodeGeneration/Sources/SyntaxSupport/Utils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public func lowercaseFirstWord(name: String) -> String {
4040

4141
/// Give a (possibly multi-line) string, prepends `///` to each line and creates
4242
/// a `.docLineComment` trivia piece for each line.
43-
func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
43+
public func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
4444
guard let string else {
4545
return []
4646
}

CodeGeneration/Sources/Utils/Utils.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public func flattened(indentedDocumentation: String) -> String {
2929
.trimmingCharacters(in: .whitespacesAndNewlines)
3030
}
3131

32+
/// Removes all empty lines from a multi-line string.
33+
public func removedEmptyLines(string: String) -> String {
34+
string.split(whereSeparator: \.isNewline)
35+
.filter { !$0.allSatisfy(\.isWhitespace) }
36+
.joined(separator: "\n")
37+
}
38+
3239
public extension Collection {
3340
/// If the collection contains a single element, return it, otherwise `nil`.
3441
var only: Element? {

CodeGeneration/Sources/generate-swiftsyntax/LayoutNode+Extensions.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import SwiftSyntax
1414
import SwiftSyntaxBuilder
1515
import SyntaxSupport
16+
import Utils
1617

1718
extension LayoutNode {
1819
func generateInitializerDeclHeader(useDeprecatedChildName: Bool = false) -> PartialSyntaxNodeString {
@@ -74,6 +75,31 @@ extension LayoutNode {
7475
"""
7576
}
7677

78+
func generateInitializerDocComment() -> SwiftSyntax.Trivia {
79+
func generateParamDocComment(for child: Child) -> String? {
80+
guard let documentation = child.documentation,
81+
let firstLine = documentation.split(whereSeparator: \.isNewline).first
82+
else {
83+
return nil
84+
}
85+
86+
return " - \(child.varName): \(firstLine)"
87+
}
88+
89+
let formattedParams = removedEmptyLines(
90+
string: """
91+
- Parameters:
92+
- leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. \
93+
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
94+
\(children.compactMap(generateParamDocComment).joined(separator: "\n"))
95+
- trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. \
96+
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
97+
"""
98+
)
99+
100+
return docCommentTrivia(from: formattedParams)
101+
}
102+
77103
/// Create a builder-based convenience initializer, if needed.
78104
func createConvenienceBuilerInitializer(useDeprecatedChildName: Bool = false) throws -> InitializerDeclSyntax? {
79105
// Only create the convenience initializer if at least one parameter

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
7878
"""
7979
)
8080

81-
try! InitializerDeclSyntax("\(node.generateInitializerDeclHeader())") {
81+
try! InitializerDeclSyntax(
82+
"""
83+
\(raw: node.generateInitializerDocComment())
84+
\(node.generateInitializerDeclHeader())
85+
"""
86+
) {
8287
let parameters = ClosureParameterListSyntax {
8388
for child in node.children {
8489
ClosureParameterSyntax(firstName: .identifier(child.varName.backtickedIfNeeded))

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift

Lines changed: 145 additions & 0 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift

Lines changed: 158 additions & 0 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 425 additions & 0 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxPatternNodes.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public struct ExpressionPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
3737
self._syntaxNode = Syntax(data)
3838
}
3939

40+
/// - Parameters:
41+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
42+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
4043
public init(
4144
leadingTrivia: Trivia? = nil,
4245
_ unexpectedBeforeExpression: UnexpectedNodesSyntax? = nil,
@@ -119,6 +122,9 @@ public struct IdentifierPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
119122
self._syntaxNode = Syntax(data)
120123
}
121124

125+
/// - Parameters:
126+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
127+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
122128
public init(
123129
leadingTrivia: Trivia? = nil,
124130
_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil,
@@ -202,6 +208,9 @@ public struct IsTypePatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
202208
self._syntaxNode = Syntax(data)
203209
}
204210

211+
/// - Parameters:
212+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
213+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
205214
public init(
206215
leadingTrivia: Trivia? = nil,
207216
_ unexpectedBeforeIsKeyword: UnexpectedNodesSyntax? = nil,
@@ -322,6 +331,10 @@ public struct MissingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
322331
self._syntaxNode = Syntax(data)
323332
}
324333

334+
/// - Parameters:
335+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
336+
/// - placeholder: A placeholder, i.e. `<#pattern#>` that can be inserted into the source code to represent the missing pattern.
337+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
325338
public init(
326339
leadingTrivia: Trivia? = nil,
327340
_ unexpectedBeforePlaceholder: UnexpectedNodesSyntax? = nil,
@@ -407,6 +420,9 @@ public struct TuplePatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
407420
self._syntaxNode = Syntax(data)
408421
}
409422

423+
/// - Parameters:
424+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
425+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
410426
public init(
411427
leadingTrivia: Trivia? = nil,
412428
_ unexpectedBeforeLeftParen: UnexpectedNodesSyntax? = nil,
@@ -578,6 +594,9 @@ public struct ValueBindingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
578594
self._syntaxNode = Syntax(data)
579595
}
580596

597+
/// - Parameters:
598+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
599+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
581600
public init(
582601
leadingTrivia: Trivia? = nil,
583602
_ unexpectedBeforeBindingKeyword: UnexpectedNodesSyntax? = nil,
@@ -699,6 +718,9 @@ public struct WildcardPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
699718
self._syntaxNode = Syntax(data)
700719
}
701720

721+
/// - Parameters:
722+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
723+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
702724
public init(
703725
leadingTrivia: Trivia? = nil,
704726
_ unexpectedBeforeWildcard: UnexpectedNodesSyntax? = nil,

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public struct BreakStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
3838
self._syntaxNode = Syntax(data)
3939
}
4040

41+
/// - Parameters:
42+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
43+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
4144
public init(
4245
leadingTrivia: Trivia? = nil,
4346
_ unexpectedBeforeBreakKeyword: UnexpectedNodesSyntax? = nil,
@@ -159,6 +162,9 @@ public struct ContinueStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
159162
self._syntaxNode = Syntax(data)
160163
}
161164

165+
/// - Parameters:
166+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
167+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
162168
public init(
163169
leadingTrivia: Trivia? = nil,
164170
_ unexpectedBeforeContinueKeyword: UnexpectedNodesSyntax? = nil,
@@ -280,6 +286,9 @@ public struct DeferStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
280286
self._syntaxNode = Syntax(data)
281287
}
282288

289+
/// - Parameters:
290+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
291+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
283292
public init(
284293
leadingTrivia: Trivia? = nil,
285294
_ unexpectedBeforeDeferKeyword: UnexpectedNodesSyntax? = nil,
@@ -401,6 +410,9 @@ public struct DiscardStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
401410
self._syntaxNode = Syntax(data)
402411
}
403412

413+
/// - Parameters:
414+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
415+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
404416
public init(
405417
leadingTrivia: Trivia? = nil,
406418
_ unexpectedBeforeDiscardKeyword: UnexpectedNodesSyntax? = nil,
@@ -523,6 +535,9 @@ public struct DoStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
523535
self._syntaxNode = Syntax(data)
524536
}
525537

538+
/// - Parameters:
539+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
540+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
526541
public init(
527542
leadingTrivia: Trivia? = nil,
528543
_ unexpectedBeforeDoKeyword: UnexpectedNodesSyntax? = nil,
@@ -693,6 +708,9 @@ public struct ExpressionStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
693708
self._syntaxNode = Syntax(data)
694709
}
695710

711+
/// - Parameters:
712+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
713+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
696714
public init(
697715
leadingTrivia: Trivia? = nil,
698716
_ unexpectedBeforeExpression: UnexpectedNodesSyntax? = nil,
@@ -775,6 +793,9 @@ public struct FallthroughStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
775793
self._syntaxNode = Syntax(data)
776794
}
777795

796+
/// - Parameters:
797+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
798+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
778799
public init(
779800
leadingTrivia: Trivia? = nil,
780801
_ unexpectedBeforeFallthroughKeyword: UnexpectedNodesSyntax? = nil,
@@ -866,6 +887,9 @@ public struct ForInStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
866887
self._syntaxNode = Syntax(data)
867888
}
868889

890+
/// - Parameters:
891+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
892+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
869893
public init(
870894
leadingTrivia: Trivia? = nil,
871895
_ unexpectedBeforeForKeyword: UnexpectedNodesSyntax? = nil,
@@ -1197,6 +1221,9 @@ public struct GuardStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
11971221
self._syntaxNode = Syntax(data)
11981222
}
11991223

1224+
/// - Parameters:
1225+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1226+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
12001227
public init(
12011228
leadingTrivia: Trivia? = nil,
12021229
_ unexpectedBeforeGuardKeyword: UnexpectedNodesSyntax? = nil,
@@ -1395,6 +1422,9 @@ public struct LabeledStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
13951422
self._syntaxNode = Syntax(data)
13961423
}
13971424

1425+
/// - Parameters:
1426+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1427+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
13981428
public init(
13991429
leadingTrivia: Trivia? = nil,
14001430
_ unexpectedBeforeLabel: UnexpectedNodesSyntax? = nil,
@@ -1541,6 +1571,10 @@ public struct MissingStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
15411571
self._syntaxNode = Syntax(data)
15421572
}
15431573

1574+
/// - Parameters:
1575+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1576+
/// - placeholder: A placeholder, i.e. `<#statement#>` that can be inserted into the source code to represent the missing pattern.
1577+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
15441578
public init(
15451579
leadingTrivia: Trivia? = nil,
15461580
_ unexpectedBeforePlaceholder: UnexpectedNodesSyntax? = nil,
@@ -1627,6 +1661,9 @@ public struct RepeatWhileStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
16271661
self._syntaxNode = Syntax(data)
16281662
}
16291663

1664+
/// - Parameters:
1665+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1666+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
16301667
public init(
16311668
leadingTrivia: Trivia? = nil,
16321669
_ unexpectedBeforeRepeatKeyword: UnexpectedNodesSyntax? = nil,
@@ -1800,6 +1837,9 @@ public struct ReturnStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
18001837
self._syntaxNode = Syntax(data)
18011838
}
18021839

1840+
/// - Parameters:
1841+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1842+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
18031843
public init(
18041844
leadingTrivia: Trivia? = nil,
18051845
_ unexpectedBeforeReturnKeyword: UnexpectedNodesSyntax? = nil,
@@ -1921,6 +1961,9 @@ public struct ThrowStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
19211961
self._syntaxNode = Syntax(data)
19221962
}
19231963

1964+
/// - Parameters:
1965+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1966+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
19241967
public init(
19251968
leadingTrivia: Trivia? = nil,
19261969
_ unexpectedBeforeThrowKeyword: UnexpectedNodesSyntax? = nil,
@@ -2043,6 +2086,9 @@ public struct WhileStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
20432086
self._syntaxNode = Syntax(data)
20442087
}
20452088

2089+
/// - Parameters:
2090+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
2091+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
20462092
public init(
20472093
leadingTrivia: Trivia? = nil,
20482094
_ unexpectedBeforeWhileKeyword: UnexpectedNodesSyntax? = nil,
@@ -2256,6 +2302,9 @@ public struct YieldStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
22562302
self._syntaxNode = Syntax(data)
22572303
}
22582304

2305+
/// - Parameters:
2306+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
2307+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
22592308
public init(
22602309
leadingTrivia: Trivia? = nil,
22612310
_ unexpectedBeforeYieldKeyword: UnexpectedNodesSyntax? = nil,

0 commit comments

Comments
 (0)