Skip to content

Commit f8cc5a2

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 32f8a1f commit f8cc5a2

File tree

11 files changed

+62
-37
lines changed

11 files changed

+62
-37
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 = removedEmptyLines(

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +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 dedented = dedented(string: description)
24-
let lines = dedented.split(separator: "\n", omittingEmptySubsequences: false)
25-
let pieces = lines.map { SwiftSyntax.TriviaPiece.docLineComment("/// \($0)") }
26-
return Trivia(pieces: pieces)
27-
}
28-
}
29-
3018
/// This file generates the syntax nodes for SwiftSyntax. To keep the generated
3119
/// files at a manageable file size, it is to be invoked multiple times with the
3220
/// variable `emitKind` set to a base kind listed in
@@ -165,7 +153,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
165153

166154
try! VariableDeclSyntax(
167155
"""
168-
\(raw: child.docComment)
156+
\(raw: child.documentation)
169157
public var \(child.varOrCaseName.backtickedIfNeeded): \(type)
170158
"""
171159
) {

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
@@ -4817,7 +4817,9 @@ public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
48174817
}
48184818
}
48194819

4820-
/// 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`.
4820+
/// A placeholder, i.e. `<#decl#>`, that can be inserted into the source code to represent the missing declaration.
4821+
///
4822+
/// This token should always have `presence = .missing`.
48214823
public var placeholder: TokenSyntax {
48224824
get {
48234825
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
@@ -4223,7 +4223,9 @@ public struct MissingExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
42234223
}
42244224
}
42254225

4226-
/// 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`.
4226+
/// A placeholder, i.e. `<#expression#>`, that can be inserted into the source code to represent the missing expression.
4227+
///
4228+
/// This token should always have `presence = .missing`.
42274229
public var placeholder: TokenSyntax {
42284230
get {
42294231
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
@@ -13579,7 +13579,9 @@ public struct MissingSyntax: SyntaxProtocol, SyntaxHashable {
1357913579
}
1358013580
}
1358113581

13582-
/// 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`
13582+
/// A placeholder, i.e. `<#syntax#>`, that can be inserted into the source code to represent the missing pattern.
13583+
///
13584+
/// This token should always have `presence = .missing`
1358313585
public var placeholder: TokenSyntax {
1358413586
get {
1358513587
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
@@ -369,7 +369,9 @@ public struct MissingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
369369
}
370370
}
371371

372-
/// 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`.
372+
/// A placeholder, i.e. `<#pattern#>`, that can be inserted into the source code to represent the missing pattern.
373+
///
374+
/// This token should always have `presence = .missing`.
373375
public var placeholder: TokenSyntax {
374376
get {
375377
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
@@ -1609,7 +1609,9 @@ public struct MissingStmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
16091609
}
16101610
}
16111611

1612-
/// 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`.
1612+
/// A placeholder, i.e. `<#statement#>`, that can be inserted into the source code to represent the missing pattern.
1613+
///
1614+
/// This token should always have `presence = .missing`.
16131615
public var placeholder: TokenSyntax {
16141616
get {
16151617
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxTypeNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ public struct MissingTypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
15701570

15711571
/// - Parameters:
15721572
/// - 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.
1573-
/// - placeholder: 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`.
1573+
/// - placeholder: A placeholder, i.e. `<#type#>`, that can be inserted into the source code to represent the missing type.
15741574
/// - 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.
15751575
public init(
15761576
leadingTrivia: Trivia? = nil,
@@ -1606,7 +1606,9 @@ public struct MissingTypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
16061606
}
16071607
}
16081608

1609-
/// 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`.
1609+
/// A placeholder, i.e. `<#type#>`, that can be inserted into the source code to represent the missing type.
1610+
///
1611+
/// This token should always have `presence = .missing`.
16101612
public var placeholder: TokenSyntax {
16111613
get {
16121614
return TokenSyntax(data.child(at: 1, parent: Syntax(self))!)

0 commit comments

Comments
 (0)