Skip to content

Commit 782fe8a

Browse files
committed
Add Children section to SyntaxCollection nodes
1 parent b0f8e70 commit 782fe8a

File tree

5 files changed

+223
-12
lines changed

5 files changed

+223
-12
lines changed

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,21 @@ public struct CollectionNode {
270270
return choices
271271
}
272272
}
273+
274+
public var grammar: SwiftSyntax.Trivia {
275+
let grammar: String
276+
if let onlyElement = elementChoices.only {
277+
grammar = "``\(onlyElement.syntaxType)`` `*`"
278+
} else {
279+
grammar = "(\(elementChoices.map { "``\($0.syntaxType)``" }.joined(separator: " | "))) `*`"
280+
}
281+
282+
return docCommentTrivia(
283+
from: """
284+
### Children
285+
286+
\(grammar)
287+
"""
288+
)
289+
}
273290
}

CodeGeneration/Sources/SyntaxSupport/Utils.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ public func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
5454
}
5555
return SwiftSyntax.Trivia(pieces: pieces)
5656
}
57+
58+
public extension Collection {
59+
/// If the collection contains a single element, return it, otherwise `nil`.
60+
var only: Element? {
61+
if !isEmpty && index(after: startIndex) == endIndex {
62+
return self.first!
63+
} else {
64+
return nil
65+
}
66+
}
67+
}

CodeGeneration/Sources/Utils/Utils.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,3 @@ public func removedEmptyLines(string: String) -> String {
3535
.filter { !$0.allSatisfy(\.isWhitespace) }
3636
.joined(separator: "\n")
3737
}
38-
39-
public extension Collection {
40-
/// If the collection contains a single element, return it, otherwise `nil`.
41-
var only: Element? {
42-
if !isEmpty && index(after: startIndex) == endIndex {
43-
return self.first!
44-
} else {
45-
return nil
46-
}
47-
}
48-
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2020
try! StructDeclSyntax(
2121
"""
2222
\(raw: node.documentation)
23-
public struct \(raw: node.kind.syntaxType): SyntaxCollection, SyntaxHashable
23+
\(raw: node.documentation.isEmpty ? "" : "///")
24+
\(raw: node.grammar)
25+
public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable
2426
"""
2527
) {
2628
if let onlyElement = node.elementChoices.only {

0 commit comments

Comments
 (0)