Skip to content

Commit 9adf2df

Browse files
committed
Change type of Child.documentation to Trivia
This also fixes a bug that caused multi-line documentation of children to be rendered incorrectly
1 parent 689efdf commit 9adf2df

File tree

11 files changed

+55
-35
lines changed

11 files changed

+55
-35
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,33 @@ public enum ChildKind {
5858
/// A child of a node, that may be declared optional or a token with a
5959
/// restricted subset of acceptable kinds or texts.
6060
public class Child {
61+
/// The name of the child.
62+
///
63+
/// The first character of the name is always uppercase.
6164
public let name: String
65+
66+
/// If the child has been renamed, its old, now deprecated, name.
67+
///
68+
/// This is used to generate deprecated compatibility layers.
6269
public let deprecatedName: String?
70+
71+
/// The kind of the child (node, token, collection, ...)
6372
public let kind: ChildKind
64-
public let nameForDiagnostics: String?
65-
public let documentation: String?
73+
74+
/// Whether this child is optional and can be `nil`.
6675
public let isOptional: Bool
6776

77+
/// A name of this child that can be shown in diagnostics.
78+
///
79+
/// This is used to e.g. describe the child if all of its tokens are missing in the source file.
80+
public let nameForDiagnostics: String?
81+
82+
/// A doc comment describing the child.
83+
public let documentation: SwiftSyntax.Trivia
84+
85+
/// The first line of the child's documentation
86+
public let documentationAbstract: String
87+
6888
public var syntaxNodeKind: SyntaxNodeKind {
6989
switch kind {
7090
case .node(kind: let kind):
@@ -148,12 +168,6 @@ public class Child {
148168
}
149169
}
150170

151-
/// Returns `true` if this child's type is one of the base syntax kinds and
152-
/// it's optional.
153-
public var hasOptionalBaseType: Bool {
154-
return hasBaseType && isOptional
155-
}
156-
157171
/// If a classification is passed, it specifies the color identifiers in
158172
/// that subtree should inherit for syntax coloring. Must be a member of
159173
/// ``SyntaxClassification``.
@@ -174,7 +188,8 @@ public class Child {
174188
self.deprecatedName = deprecatedName
175189
self.kind = kind
176190
self.nameForDiagnostics = nameForDiagnostics
177-
self.documentation = documentation
191+
self.documentation = docCommentTrivia(from: documentation)
192+
self.documentationAbstract = String(documentation?.split(whereSeparator: \.isNewline).first ?? "")
178193
self.isOptional = isOptional
179194
}
180195
}

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public let COMMON_NODES: [Node] = [
181181
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
182182
documentation: """
183183
A placeholder, i.e. `<#decl#>`, that can be inserted into the source code to represent the missing declaration.
184+
184185
This token should always have `presence = .missing`.
185186
"""
186187
),
@@ -201,6 +202,7 @@ public let COMMON_NODES: [Node] = [
201202
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
202203
documentation: """
203204
A placeholder, i.e. `<#expression#>`, that can be inserted into the source code to represent the missing expression.
205+
204206
This token should always have `presence = .missing`.
205207
"""
206208
)
@@ -221,6 +223,7 @@ public let COMMON_NODES: [Node] = [
221223
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
222224
documentation: """
223225
A placeholder, i.e. `<#pattern#>`, that can be inserted into the source code to represent the missing pattern.
226+
224227
This token should always have `presence = .missing`.
225228
"""
226229
)
@@ -241,6 +244,7 @@ public let COMMON_NODES: [Node] = [
241244
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
242245
documentation: """
243246
A placeholder, i.e. `<#statement#>`, that can be inserted into the source code to represent the missing pattern.
247+
244248
This token should always have `presence = .missing`.
245249
"""
246250
)
@@ -261,6 +265,7 @@ public let COMMON_NODES: [Node] = [
261265
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
262266
documentation: """
263267
A placeholder, i.e. `<#syntax#>`, that can be inserted into the source code to represent the missing pattern.
268+
264269
This token should always have `presence = .missing`
265270
"""
266271
)
@@ -280,7 +285,9 @@ public let COMMON_NODES: [Node] = [
280285
name: "Placeholder",
281286
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
282287
documentation: """
283-
A placeholder, i.e. `<#type#>`, that can be inserted into the source code to represent the missing type. This token should always have `presence = .missing`.
288+
A placeholder, i.e. `<#type#>`, that can be inserted into the source code to represent the missing type.
289+
290+
This token should always have `presence = .missing`.
284291
"""
285292
)
286293
]

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ extension LayoutNode {
7777

7878
func generateInitializerDocComment() -> SwiftSyntax.Trivia {
7979
func generateParamDocComment(for child: Child) -> String? {
80-
guard let documentation = child.documentation,
81-
let firstLine = documentation.split(whereSeparator: \.isNewline).first
82-
else {
80+
if child.documentationAbstract.isEmpty {
8381
return nil
8482
}
85-
86-
return " - \(child.varOrCaseName): \(firstLine)"
83+
return " - \(child.varOrCaseName): \(child.documentationAbstract)"
8784
}
8885

8986
let formattedParams = """

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ import SwiftSyntaxBuilder
1515
import SyntaxSupport
1616
import Utils
1717

18-
extension Child {
19-
public var docComment: SwiftSyntax.Trivia {
20-
guard let description = documentation else {
21-
return []
22-
}
23-
let lines = description.split(separator: "\n", omittingEmptySubsequences: false)
24-
let pieces = lines.map { SwiftSyntax.TriviaPiece.docLineComment("/// \($0)") }
25-
return Trivia(pieces: pieces)
26-
}
27-
}
28-
2918
/// This file generates the syntax nodes for SwiftSyntax. To keep the generated
3019
/// files at a manageable file size, it is to be invoked multiple times with the
3120
/// variable `emitKind` set to a base kind listed in
@@ -168,7 +157,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
168157

169158
try! VariableDeclSyntax(
170159
"""
171-
\(raw: child.docComment)
160+
\(raw: child.documentation)
172161
public var \(child.varOrCaseName.backtickedIfNeeded): \(type)
173162
"""
174163
) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2828
for child in trait.children {
2929
DeclSyntax(
3030
"""
31-
\(raw: child.docComment)
31+
\(raw: child.documentation)
3232
var \(child.varOrCaseName): \(child.syntaxNodeKind.syntaxType)\(raw: child.isOptional ? "?" : "") { get set }
3333
"""
3434
)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4803,7 +4803,9 @@ public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
48034803
}
48044804
}
48054805

4806-
/// A placeholder, i.e. `<#decl#>`, that can be inserted into the source code to represent the missing declaration./// This token should always have `presence = .missing`.
4806+
/// A placeholder, i.e. `<#decl#>`, that can be inserted into the source code to represent the missing declaration.
4807+
///
4808+
/// This token should always have `presence = .missing`.
48074809
public var placeholder: TokenSyntax {
48084810
get {
48094811
return TokenSyntax(data.child(at: 5, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4171,7 +4171,9 @@ public struct MissingExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
41714171
}
41724172
}
41734173

4174-
/// A placeholder, i.e. `<#expression#>`, that can be inserted into the source code to represent the missing expression./// This token should always have `presence = .missing`.
4174+
/// A placeholder, i.e. `<#expression#>`, that can be inserted into the source code to represent the missing expression.
4175+
///
4176+
/// This token should always have `presence = .missing`.
41754177
public var placeholder: TokenSyntax {
41764178
get {
41774179
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13475,7 +13475,9 @@ public struct MissingSyntax: SyntaxProtocol, SyntaxHashable {
1347513475
}
1347613476
}
1347713477

13478-
/// A placeholder, i.e. `<#syntax#>`, that can be inserted into the source code to represent the missing pattern./// This token should always have `presence = .missing`
13478+
/// A placeholder, i.e. `<#syntax#>`, that can be inserted into the source code to represent the missing pattern.
13479+
///
13480+
/// This token should always have `presence = .missing`
1347913481
public var placeholder: TokenSyntax {
1348013482
get {
1348113483
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxPatternNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ public struct MissingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
363363
}
364364
}
365365

366-
/// A placeholder, i.e. `<#pattern#>`, that can be inserted into the source code to represent the missing pattern./// This token should always have `presence = .missing`.
366+
/// A placeholder, i.e. `<#pattern#>`, that can be inserted into the source code to represent the missing pattern.
367+
///
368+
/// This token should always have `presence = .missing`.
367369
public var placeholder: TokenSyntax {
368370
get {
369371
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,9 @@ public struct MissingStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
15891589
}
15901590
}
15911591

1592-
/// A placeholder, i.e. `<#statement#>`, that can be inserted into the source code to represent the missing pattern./// This token should always have `presence = .missing`.
1592+
/// A placeholder, i.e. `<#statement#>`, that can be inserted into the source code to represent the missing pattern.
1593+
///
1594+
/// This token should always have `presence = .missing`.
15931595
public var placeholder: TokenSyntax {
15941596
get {
15951597
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

0 commit comments

Comments
 (0)