Skip to content

Commit 22b6620

Browse files
committed
Generate documentation for traits
1 parent 767c10a commit 22b6620

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftSyntax
14+
1315
public class Trait {
1416
public let traitName: String
17+
public let protocolName: TokenSyntax
18+
public let documentation: SwiftSyntax.Trivia
1519
public let children: [Child]
16-
public let description: String?
1720

18-
init(traitName: String, children: [Child], description: String? = nil) {
21+
init(traitName: String, documentation: String? = nil, children: [Child]) {
1922
self.traitName = traitName
23+
self.protocolName = .identifier("\(traitName)Syntax")
24+
self.documentation = docCommentTrivia(from: documentation)
2025
self.children = children
21-
self.description = description
2226
}
2327
}
2428

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let nodesSections: String = {
6161

6262
addSection(heading: "Miscellaneous Syntax", types: SYNTAX_NODES.map(\.kind.syntaxType.description).filter({ !handledSyntaxTypes.contains($0) }))
6363

64-
addSection(heading: "Traits", types: TRAITS.map { "\($0.traitName)Syntax" })
64+
addSection(heading: "Traits", types: TRAITS.map { "\($0.protocolName)" })
6565

6666
return result
6767
}()

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,30 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
1919
for trait in TRAITS {
2020
try! ProtocolDeclSyntax(
2121
"""
22-
// MARK: - \(raw: trait.traitName)Syntax
22+
// MARK: - \(trait.protocolName)
2323
24-
public protocol \(raw: trait.traitName)Syntax: SyntaxProtocol
24+
\(raw: trait.documentation)
25+
public protocol \(trait.protocolName): SyntaxProtocol
2526
"""
2627
) {
2728
for child in trait.children {
28-
DeclSyntax("var \(raw: child.varName): \(child.syntaxNodeKind.syntaxType)\(raw: child.isOptional ? "?" : "") { get set }")
29+
DeclSyntax(
30+
"""
31+
\(raw: child.docComment)
32+
var \(raw: child.varName): \(child.syntaxNodeKind.syntaxType)\(raw: child.isOptional ? "?" : "") { get set }
33+
"""
34+
)
2935
}
3036
}
3137

32-
try! ExtensionDeclSyntax("public extension \(raw: trait.traitName)Syntax") {
38+
try! ExtensionDeclSyntax("public extension \(trait.protocolName)") {
3339
DeclSyntax(
3440
"""
3541
/// Without this function, the `with` function defined on `SyntaxProtocol`
3642
/// does not work on existentials of this protocol type.
3743
@_disfavoredOverload
38-
func with<T>(_ keyPath: WritableKeyPath<\(raw: trait.traitName)Syntax, T>, _ newChild: T) -> \(raw: trait.traitName)Syntax {
39-
var copy: \(raw: trait.traitName)Syntax = self
44+
func with<T>(_ keyPath: WritableKeyPath<\(trait.protocolName), T>, _ newChild: T) -> \(trait.protocolName) {
45+
var copy: \(trait.protocolName) = self
4046
copy[keyPath: keyPath] = newChild
4147
return copy
4248
}
@@ -48,21 +54,21 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
4854
DeclSyntax(
4955
"""
5056
/// Check whether the non-type erased version of this syntax node conforms to
51-
/// `\(raw: trait.traitName)Syntax`.
57+
/// `\(trait.protocolName)`.
5258
/// Note that this will incur an existential conversion.
53-
func isProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> Bool {
54-
return self.asProtocol(\(raw: trait.traitName)Syntax.self) != nil
59+
func isProtocol(_: \(trait.protocolName).Protocol) -> Bool {
60+
return self.asProtocol(\(trait.protocolName).self) != nil
5561
}
5662
"""
5763
)
5864

5965
DeclSyntax(
6066
"""
6167
/// Return the non-type erased version of this syntax node if it conforms to
62-
/// `\(raw: trait.traitName)Syntax`. Otherwise return `nil`.
68+
/// `\(trait.protocolName)`. Otherwise return `nil`.
6369
/// Note that this will incur an existential conversion.
64-
func asProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> \(raw: trait.traitName)Syntax? {
65-
return Syntax(self).asProtocol(SyntaxProtocol.self) as? \(raw: trait.traitName)Syntax
70+
func asProtocol(_: \(trait.protocolName).Protocol) -> \(trait.protocolName)? {
71+
return Syntax(self).asProtocol(SyntaxProtocol.self) as? \(trait.protocolName)
6672
}
6773
"""
6874
)

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// MARK: - BracedSyntax
1616

17+
1718
public protocol BracedSyntax: SyntaxProtocol {
1819
var leftBrace: TokenSyntax {
1920
get
@@ -55,6 +56,7 @@ public extension SyntaxProtocol {
5556

5657
// MARK: - DeclGroupSyntax
5758

59+
5860
public protocol DeclGroupSyntax: SyntaxProtocol {
5961
var attributes: AttributeListSyntax? {
6062
get
@@ -111,6 +113,7 @@ public extension SyntaxProtocol {
111113

112114
// MARK: - EffectSpecifiersSyntax
113115

116+
114117
public protocol EffectSpecifiersSyntax: SyntaxProtocol {
115118
var unexpectedBeforeAsyncSpecifier: UnexpectedNodesSyntax? {
116119
get
@@ -167,6 +170,7 @@ public extension SyntaxProtocol {
167170

168171
// MARK: - FreestandingMacroExpansionSyntax
169172

173+
170174
public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
171175
var poundToken: TokenSyntax {
172176
get
@@ -238,6 +242,7 @@ public extension SyntaxProtocol {
238242

239243
// MARK: - IdentifiedDeclSyntax
240244

245+
241246
public protocol IdentifiedDeclSyntax: SyntaxProtocol {
242247
var identifier: TokenSyntax {
243248
get
@@ -274,6 +279,7 @@ public extension SyntaxProtocol {
274279

275280
// MARK: - ParenthesizedSyntax
276281

282+
277283
public protocol ParenthesizedSyntax: SyntaxProtocol {
278284
var leftParen: TokenSyntax {
279285
get
@@ -315,6 +321,7 @@ public extension SyntaxProtocol {
315321

316322
// MARK: - WithAttributesSyntax
317323

324+
318325
public protocol WithAttributesSyntax: SyntaxProtocol {
319326
var attributes: AttributeListSyntax? {
320327
get
@@ -351,6 +358,7 @@ public extension SyntaxProtocol {
351358

352359
// MARK: - WithCodeBlockSyntax
353360

361+
354362
public protocol WithCodeBlockSyntax: SyntaxProtocol {
355363
var body: CodeBlockSyntax {
356364
get
@@ -387,6 +395,7 @@ public extension SyntaxProtocol {
387395

388396
// MARK: - WithModifiersSyntax
389397

398+
390399
public protocol WithModifiersSyntax: SyntaxProtocol {
391400
var modifiers: ModifierListSyntax? {
392401
get
@@ -423,6 +432,7 @@ public extension SyntaxProtocol {
423432

424433
// MARK: - WithStatementsSyntax
425434

435+
426436
public protocol WithStatementsSyntax: SyntaxProtocol {
427437
var statements: CodeBlockItemListSyntax {
428438
get
@@ -459,6 +469,7 @@ public extension SyntaxProtocol {
459469

460470
// MARK: - WithTrailingCommaSyntax
461471

472+
462473
public protocol WithTrailingCommaSyntax: SyntaxProtocol {
463474
var trailingComma: TokenSyntax? {
464475
get

0 commit comments

Comments
 (0)