Skip to content

Commit a2e596a

Browse files
authored
Merge pull request #1527 from ahoppen/ahoppen/documentation-for-initializer
Add documentation for `InitializerDeclSyntax` and all its children
2 parents c2734b5 + 0d6913f commit a2e596a

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,16 @@ public let DECL_NODES: [Node] = [
10321032
Node(
10331033
name: "InitializerDecl",
10341034
nameForDiagnostics: "initializer",
1035+
description: """
1036+
An initializer declaration like the following.
1037+
1038+
```swift
1039+
init(someParameter: Int) {
1040+
}
1041+
```
1042+
1043+
The body is optional because this node also represents initializer requirements inside protocols.
1044+
""",
10351045
kind: "Decl",
10361046
traits: [
10371047
"Attributed"
@@ -1041,43 +1051,51 @@ public let DECL_NODES: [Node] = [
10411051
name: "Attributes",
10421052
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
10431053
nameForDiagnostics: "attributes",
1054+
description: "Attributes that are attached to the initializer.",
10441055
isOptional: true
10451056
),
10461057
Child(
10471058
name: "Modifiers",
10481059
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
10491060
nameForDiagnostics: "modifiers",
1061+
description: "Modifiers attached to the initializer",
10501062
isOptional: true
10511063
),
10521064
Child(
10531065
name: "InitKeyword",
1054-
kind: .token(choices: [.keyword(text: "init")])
1066+
kind: .token(choices: [.keyword(text: "init")]),
1067+
description: "The init keyword"
10551068
),
10561069
Child(
10571070
name: "OptionalMark",
10581071
kind: .token(choices: [.token(tokenKind: "PostfixQuestionMarkToken"), .token(tokenKind: "InfixQuestionMarkToken"), .token(tokenKind: "ExclamationMarkToken")]),
1072+
description: "If the initializer is failable, a question mark to indicate that.",
10591073
isOptional: true
10601074
),
10611075
Child(
10621076
name: "GenericParameterClause",
10631077
kind: .node(kind: "GenericParameterClause"),
10641078
nameForDiagnostics: "generic parameter clause",
1079+
description: "Generic parameters of the initializer.",
10651080
isOptional: true
10661081
),
10671082
Child(
10681083
name: "Signature",
10691084
kind: .node(kind: "FunctionSignature"),
1070-
nameForDiagnostics: "function signature"
1085+
nameForDiagnostics: "function signature",
1086+
description: "The arguments of the initializer. While the function signature allows specifying an return clause, doing so is not semantically valid."
10711087
),
10721088
Child(
10731089
name: "GenericWhereClause",
10741090
kind: .node(kind: "GenericWhereClause"),
10751091
nameForDiagnostics: "generic where clause",
1092+
description: "If the initializer had generic parameters, a where clause that can restrict those",
10761093
isOptional: true
10771094
),
10781095
Child(
10791096
name: "Body",
10801097
kind: .node(kind: "CodeBlock"),
1098+
description: "The initializer’s body. Missing if the initialier is a requirement of a protocol declaration.",
10811099
isOptional: true
10821100
),
10831101
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
2323
SourceFileSyntax(leadingTrivia: copyrightHeader) {
2424
for node in SYNTAX_NODES where !node.isBase && node.collectionElement.isEmpty && node.baseKind == emitKind {
2525
// We are actually handling this node now
26-
let nodeDoc = node.description.map { "/// \($0)" }
26+
let nodeDoc = node.description?.split(separator: "\n", omittingEmptySubsequences: false).map { "/// \($0)" }.joined(separator: "\n")
2727
try! StructDeclSyntax(
2828
"""
2929
// MARK: - \(raw: node.name)

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ let buildableNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2222
let type = node.type
2323

2424
if let convenienceInit = try! createConvenienceInitializer(node: node) {
25-
let docComment: SwiftSyntax.Trivia = node.documentation.isEmpty ? [] : .docLineComment("/// \(node.documentation)") + .newline
25+
let docComment = node.description?.split(separator: "\n", omittingEmptySubsequences: false).map { "/// \($0)" }.joined(separator: "\n") ?? ""
2626
ExtensionDeclSyntax(
27-
leadingTrivia: docComment,
27+
leadingTrivia: "\(docComment)\n",
2828
extendedType: SimpleTypeIdentifierSyntax(name: .identifier(type.syntaxBaseName))
2929
) {
3030
convenienceInit

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,14 @@ extension ImportDeclSyntax: CustomReflectable {
31873187

31883188
// MARK: - InitializerDeclSyntax
31893189

3190-
3190+
/// An initializer declaration like the following.
3191+
///
3192+
/// ```swift
3193+
/// init(someParameter: Int) {
3194+
/// }
3195+
/// ```
3196+
///
3197+
/// The body is optional because this node also represents initializer requirements inside protocols.
31913198
public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
31923199
public let _syntaxNode: Syntax
31933200

@@ -3289,6 +3296,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
32893296
}
32903297
}
32913298

3299+
/// Attributes that are attached to the initializer.
32923300
public var attributes: AttributeListSyntax? {
32933301
get {
32943302
return data.child(at: 1, parent: Syntax(self)).map(AttributeListSyntax.init)
@@ -3326,6 +3334,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
33263334
}
33273335
}
33283336

3337+
/// Modifiers attached to the initializer
33293338
public var modifiers: ModifierListSyntax? {
33303339
get {
33313340
return data.child(at: 3, parent: Syntax(self)).map(ModifierListSyntax.init)
@@ -3363,6 +3372,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
33633372
}
33643373
}
33653374

3375+
/// The init keyword
33663376
public var initKeyword: TokenSyntax {
33673377
get {
33683378
return TokenSyntax(data.child(at: 5, parent: Syntax(self))!)
@@ -3381,6 +3391,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
33813391
}
33823392
}
33833393

3394+
/// If the initializer is failable, a question mark to indicate that.
33843395
public var optionalMark: TokenSyntax? {
33853396
get {
33863397
return data.child(at: 7, parent: Syntax(self)).map(TokenSyntax.init)
@@ -3399,6 +3410,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
33993410
}
34003411
}
34013412

3413+
/// Generic parameters of the initializer.
34023414
public var genericParameterClause: GenericParameterClauseSyntax? {
34033415
get {
34043416
return data.child(at: 9, parent: Syntax(self)).map(GenericParameterClauseSyntax.init)
@@ -3417,6 +3429,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
34173429
}
34183430
}
34193431

3432+
/// The arguments of the initializer. While the function signature allows specifying an return clause, doing so is not semantically valid.
34203433
public var signature: FunctionSignatureSyntax {
34213434
get {
34223435
return FunctionSignatureSyntax(data.child(at: 11, parent: Syntax(self))!)
@@ -3435,6 +3448,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
34353448
}
34363449
}
34373450

3451+
/// If the initializer had generic parameters, a where clause that can restrict those
34383452
public var genericWhereClause: GenericWhereClauseSyntax? {
34393453
get {
34403454
return data.child(at: 13, parent: Syntax(self)).map(GenericWhereClauseSyntax.init)
@@ -3453,6 +3467,7 @@ public struct InitializerDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
34533467
}
34543468
}
34553469

3470+
/// The initializer’s body. Missing if the initialier is a requirement of a protocol declaration.
34563471
public var body: CodeBlockSyntax? {
34573472
get {
34583473
return data.child(at: 15, parent: Syntax(self)).map(CodeBlockSyntax.init)

Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,14 @@ extension IfExprSyntax {
819819
}
820820
}
821821

822+
/// An initializer declaration like the following.
823+
///
824+
/// ```swift
825+
/// init(someParameter: Int) {
826+
/// }
827+
/// ```
828+
///
829+
/// The body is optional because this node also represents initializer requirements inside protocols.
822830
extension InitializerDeclSyntax {
823831
/// A convenience initializer that allows initializing syntax collections using result builders
824832
public init(

0 commit comments

Comments
 (0)