Skip to content

Commit cc6fca0

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 28e0edd commit cc6fca0

File tree

11 files changed

+62
-36
lines changed

11 files changed

+62
-36
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,41 @@ 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
73+
74+
/// Whether this child is optional and can be `nil`.
75+
public let isOptional: Bool
76+
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.
6480
public let nameForDiagnostics: String?
65-
public let documentation: 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+
88+
/// If not `nil`, identifiers within this child will be syntax highlighted with this classification.
89+
public let classification: SyntaxClassification?
90+
91+
/// When `true`, `classification` not only applies to identifiers but also overrides the classifcation of keywords.
6692
public let forceClassification: Bool
93+
94+
/// Whether the elements in this child should be indented by another indentation level.
6795
public let isIndented: Bool
68-
public let isOptional: Bool
69-
public let classification: SyntaxClassification?
7096

7197
public var syntaxNodeKind: SyntaxNodeKind {
7298
switch kind {
@@ -151,12 +177,6 @@ public class Child {
151177
}
152178
}
153179

154-
/// Returns `true` if this child's type is one of the base syntax kinds and
155-
/// it's optional.
156-
public var hasOptionalBaseType: Bool {
157-
return hasBaseType && isOptional
158-
}
159-
160180
/// If a classification is passed, it specifies the color identifiers in
161181
/// that subtree should inherit for syntax coloring. Must be a member of
162182
/// ``SyntaxClassification``.
@@ -180,7 +200,8 @@ public class Child {
180200
self.deprecatedName = deprecatedName
181201
self.kind = kind
182202
self.nameForDiagnostics = nameForDiagnostics
183-
self.documentation = documentation
203+
self.documentation = docCommentTrivia(from: documentation)
204+
self.documentationAbstract = String(documentation?.split(whereSeparator: \.isNewline).first ?? "")
184205
self.classification = classificationByName(classification)
185206
self.forceClassification = forceClassification
186207
self.isIndented = isIndented

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public let COMMON_NODES: [Node] = [
182182
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
183183
documentation: """
184184
A placeholder, i.e. `<#decl#>`, that can be inserted into the source code to represent the missing declaration.
185+
185186
This token should always have `presence = .missing`.
186187
"""
187188
),
@@ -202,6 +203,7 @@ public let COMMON_NODES: [Node] = [
202203
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
203204
documentation: """
204205
A placeholder, i.e. `<#expression#>`, that can be inserted into the source code to represent the missing expression.
206+
205207
This token should always have `presence = .missing`.
206208
"""
207209
)
@@ -222,6 +224,7 @@ public let COMMON_NODES: [Node] = [
222224
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
223225
documentation: """
224226
A placeholder, i.e. `<#pattern#>`, that can be inserted into the source code to represent the missing pattern.
227+
225228
This token should always have `presence = .missing`.
226229
"""
227230
)
@@ -242,6 +245,7 @@ public let COMMON_NODES: [Node] = [
242245
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
243246
documentation: """
244247
A placeholder, i.e. `<#statement#>`, that can be inserted into the source code to represent the missing pattern.
248+
245249
This token should always have `presence = .missing`.
246250
"""
247251
)
@@ -262,6 +266,7 @@ public let COMMON_NODES: [Node] = [
262266
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
263267
documentation: """
264268
A placeholder, i.e. `<#syntax#>`, that can be inserted into the source code to represent the missing pattern.
269+
265270
This token should always have `presence = .missing`
266271
"""
267272
)
@@ -281,7 +286,9 @@ public let COMMON_NODES: [Node] = [
281286
name: "Placeholder",
282287
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
283288
documentation: """
284-
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`.
289+
A placeholder, i.e. `<#type#>`, that can be inserted into the source code to represent the missing type.
290+
291+
This token should always have `presence = .missing`.
285292
"""
286293
)
287294
]

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)