Skip to content

Commit f430866

Browse files
committed
Merge pull request swiftlang#1527 from ahoppen/ahoppen/documentation-for-initializer
Add documentation for `InitializerDeclSyntax` and all its children
1 parent b6099e9 commit f430866

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
@@ -1024,6 +1024,16 @@ public let DECL_NODES: [Node] = [
10241024
Node(
10251025
name: "InitializerDecl",
10261026
nameForDiagnostics: "initializer",
1027+
description: """
1028+
An initializer declaration like the following.
1029+
1030+
```swift
1031+
init(someParameter: Int) {
1032+
}
1033+
```
1034+
1035+
The body is optional because this node also represents initializer requirements inside protocols.
1036+
""",
10271037
kind: "Decl",
10281038
traits: [
10291039
"Attributed"
@@ -1033,43 +1043,51 @@ public let DECL_NODES: [Node] = [
10331043
name: "Attributes",
10341044
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
10351045
nameForDiagnostics: "attributes",
1046+
description: "Attributes that are attached to the initializer.",
10361047
isOptional: true
10371048
),
10381049
Child(
10391050
name: "Modifiers",
10401051
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
10411052
nameForDiagnostics: "modifiers",
1053+
description: "Modifiers attached to the initializer",
10421054
isOptional: true
10431055
),
10441056
Child(
10451057
name: "InitKeyword",
1046-
kind: .token(choices: [.keyword(text: "init")])
1058+
kind: .token(choices: [.keyword(text: "init")]),
1059+
description: "The init keyword"
10471060
),
10481061
Child(
10491062
name: "OptionalMark",
10501063
kind: .token(choices: [.token(tokenKind: "PostfixQuestionMarkToken"), .token(tokenKind: "InfixQuestionMarkToken"), .token(tokenKind: "ExclamationMarkToken")]),
1064+
description: "If the initializer is failable, a question mark to indicate that.",
10511065
isOptional: true
10521066
),
10531067
Child(
10541068
name: "GenericParameterClause",
10551069
kind: .node(kind: "GenericParameterClause"),
10561070
nameForDiagnostics: "generic parameter clause",
1071+
description: "Generic parameters of the initializer.",
10571072
isOptional: true
10581073
),
10591074
Child(
10601075
name: "Signature",
10611076
kind: .node(kind: "FunctionSignature"),
1062-
nameForDiagnostics: "function signature"
1077+
nameForDiagnostics: "function signature",
1078+
description: "The arguments of the initializer. While the function signature allows specifying an return clause, doing so is not semantically valid."
10631079
),
10641080
Child(
10651081
name: "GenericWhereClause",
10661082
kind: .node(kind: "GenericWhereClause"),
10671083
nameForDiagnostics: "generic where clause",
1084+
description: "If the initializer had generic parameters, a where clause that can restrict those",
10681085
isOptional: true
10691086
),
10701087
Child(
10711088
name: "Body",
10721089
kind: .node(kind: "CodeBlock"),
1090+
description: "The initializer’s body. Missing if the initialier is a requirement of a protocol declaration.",
10731091
isOptional: true
10741092
),
10751093
]

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)