Skip to content

Commit a723958

Browse files
authored
Merge pull request #1576 from ahoppen/ahoppen/5.9/update-basic-format
[5.9] Update BasicFormat
2 parents 1b0cd42 + 2057d63 commit a723958

File tree

87 files changed

+11190
-10002
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+11190
-10002
lines changed

CodeGeneration/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let package = Package(
1111
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"])
1212
],
1313
dependencies: [
14-
.package(url: "https://github.com/apple/swift-syntax.git", revision: "39b3336c3f3bfcd4ddbcbf6a111d8814ffe542f3"),
15-
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.2.2")),
14+
.package(url: "https://github.com/apple/swift-syntax.git", revision: "c7087adb193b26f02b59d21cd06d17ec202d1bf5"),
15+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
1616
],
1717
targets: [
1818
.executableTarget(

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public let COMMON_NODES: [Node] = [
144144
Node(
145145
name: "MissingDecl",
146146
nameForDiagnostics: "declaration",
147+
description: "In case the source code is missing a declaration, this node stands in place of the missing declaration.",
147148
kind: "Decl",
148149
traits: [
149150
"Attributed"
@@ -152,44 +153,108 @@ public let COMMON_NODES: [Node] = [
152153
Child(
153154
name: "Attributes",
154155
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
156+
description: "If there were standalone attributes without a declaration to attach them to, the `MissingDeclSyntax` will contain these.",
155157
isOptional: true
156158
),
157159
Child(
158160
name: "Modifiers",
159161
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
162+
description: "If there were standalone modifiers without a declaration to attach them to, the `MissingDeclSyntax` will contain these.",
160163
isOptional: true
161164
),
165+
Child(
166+
name: "Placeholder",
167+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
168+
description: """
169+
A placeholder, i.e. `<#decl#>` that can be inserted into the source code to represent the missing declaration.
170+
This token should always have `presence = .missing`.
171+
"""
172+
),
162173
]
163174
),
164175

165176
Node(
166177
name: "MissingExpr",
167178
nameForDiagnostics: "expression",
168-
kind: "Expr"
179+
description: "In case the source code is missing a expression, this node stands in place of the missing expression.",
180+
kind: "Expr",
181+
children: [
182+
Child(
183+
name: "Placeholder",
184+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
185+
description: """
186+
A placeholder, i.e. `<#expression#>` that can be inserted into the source code to represent the missing expression.
187+
This token should always have `presence = .missing`.
188+
"""
189+
)
190+
]
169191
),
170192

171193
Node(
172194
name: "MissingPattern",
173195
nameForDiagnostics: "pattern",
174-
kind: "Pattern"
196+
description: "In case the source code is missing a pattern, this node stands in place of the missing pattern.",
197+
kind: "Pattern",
198+
children: [
199+
Child(
200+
name: "Placeholder",
201+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
202+
description: """
203+
A placeholder, i.e. `<#pattern#>` that can be inserted into the source code to represent the missing pattern.
204+
This token should always have `presence = .missing`.
205+
"""
206+
)
207+
]
175208
),
176209

177210
Node(
178211
name: "MissingStmt",
179212
nameForDiagnostics: "statement",
180-
kind: "Stmt"
213+
description: "In case the source code is missing a statement, this node stands in place of the missing statement.",
214+
kind: "Stmt",
215+
children: [
216+
Child(
217+
name: "Placeholder",
218+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
219+
description: """
220+
A placeholder, i.e. `<#statement#>` that can be inserted into the source code to represent the missing pattern.
221+
This token should always have `presence = .missing`.
222+
"""
223+
)
224+
]
181225
),
182226

183227
Node(
184228
name: "Missing",
185229
nameForDiagnostics: nil,
186-
kind: "Syntax"
230+
description: "In case the source code is missing a syntax node, this node stands in place of the missing node.",
231+
kind: "Syntax",
232+
children: [
233+
Child(
234+
name: "Placeholder",
235+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
236+
description: """
237+
A placeholder, i.e. `<#syntax#>` that can be inserted into the source code to represent the missing pattern.
238+
This token should always have `presence = .missing`
239+
"""
240+
)
241+
]
187242
),
188243

189244
Node(
190245
name: "MissingType",
191246
nameForDiagnostics: "type",
192-
kind: "Type"
247+
description: "In case the source code is missing a type, this node stands in place of the missing type.",
248+
kind: "Type",
249+
children: [
250+
Child(
251+
name: "Placeholder",
252+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")], requiresLeadingSpace: false, requiresTrailingSpace: false),
253+
description: """
254+
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`.
255+
"""
256+
)
257+
]
193258
),
194259

195260
Node(

CodeGeneration/Sources/SyntaxSupport/Trivia.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class Trivia {
1515
public let comment: String
1616
public let characters: [Character]
1717
public let swiftCharacters: [Character]
18-
public let isNewLine: Bool
1918
public let isComment: Bool
2019

2120
public var lowerName: String { lowercaseFirstWord(name: name) }
@@ -36,17 +35,23 @@ public class Trivia {
3635

3736
public var isCollection: Bool { charactersLen > 0 }
3837

38+
public var isBlank: Bool {
39+
characters.contains { $0.isWhitespace }
40+
}
41+
42+
public var isNewLine: Bool {
43+
characters.contains { $0.isNewline }
44+
}
45+
3946
init(
4047
name: String,
4148
comment: String,
4249
characters: [Character] = [],
4350
swiftCharacters: [Character] = [],
44-
isNewLine: Bool = false,
4551
isComment: Bool = false
4652
) {
4753
self.name = name
4854
self.comment = comment
49-
self.isNewLine = isNewLine
5055
self.isComment = isComment
5156
self.characters = characters
5257

@@ -86,8 +91,7 @@ public let TRIVIAS: [Trivia] = [
8691
],
8792
swiftCharacters: [
8893
Character("\r")
89-
],
90-
isNewLine: true
94+
]
9195
),
9296

9397
Trivia(
@@ -100,8 +104,7 @@ public let TRIVIAS: [Trivia] = [
100104
swiftCharacters: [
101105
Character("\r"),
102106
Character("\n"),
103-
],
104-
isNewLine: true
107+
]
105108
),
106109

107110
Trivia(
@@ -142,8 +145,7 @@ public let TRIVIAS: [Trivia] = [
142145
],
143146
swiftCharacters: [
144147
Character("\n")
145-
],
146-
isNewLine: true
148+
]
147149
),
148150

149151
Trivia(

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import SwiftSyntax
1515

1616
/// A format style for files generated by CodeGeneration.
1717
public class CodeGenerationFormat: BasicFormat {
18-
public override var indentation: TriviaPiece { .spaces(indentationLevel * 2) }
18+
public init() {
19+
super.init(indentationWidth: .spaces(2))
20+
}
21+
22+
var indentedNewline: Trivia {
23+
.newline + currentIndentationLevel
24+
}
1925

2026
public override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax {
2127
let children = node.children(viewMode: .all)
@@ -66,7 +72,7 @@ public class CodeGenerationFormat: BasicFormat {
6672

6773
public override func visit(_ node: MemberDeclListItemSyntax) -> MemberDeclListItemSyntax {
6874
let formatted = super.visit(node)
69-
if node.indexInParent != 0 && !node.decl.is(EnumCaseDeclSyntax.self) {
75+
if node != node.parent?.children(viewMode: .sourceAccurate).first?.as(MemberDeclListItemSyntax.self) && !node.decl.is(EnumCaseDeclSyntax.self) {
7076
return ensuringTwoLeadingNewlines(node: formatted)
7177
} else {
7278
return formatted
@@ -92,26 +98,26 @@ public class CodeGenerationFormat: BasicFormat {
9298
}
9399

94100
private func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
95-
if node.leadingTrivia?.first?.isNewline ?? false {
96-
return node.with(\.leadingTrivia, indentedNewline + (node.leadingTrivia ?? []))
101+
if node.leadingTrivia.first?.isNewline ?? false {
102+
return node.with(\.leadingTrivia, indentedNewline + node.leadingTrivia)
97103
} else {
98-
return node.with(\.leadingTrivia, indentedNewline + indentedNewline + (node.leadingTrivia ?? []))
104+
return node.with(\.leadingTrivia, indentedNewline + indentedNewline + node.leadingTrivia)
99105
}
100106
}
101107

102108
private func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] {
103-
indentationLevel += 1
109+
increaseIndentationLevel()
104110
var formattedChildren = children.map {
105111
self.visit($0).as(SyntaxType.self)!
106112
}
107113
formattedChildren = formattedChildren.map {
108-
if $0.leadingTrivia?.first?.isNewline == true {
114+
if $0.leadingTrivia.first?.isNewline == true {
109115
return $0
110116
} else {
111-
return $0.with(\.leadingTrivia, indentedNewline + ($0.leadingTrivia ?? []))
117+
return $0.with(\.leadingTrivia, indentedNewline + $0.leadingTrivia)
112118
}
113119
}
114-
indentationLevel -= 1
120+
decreaseIndentationLevel()
115121
if !formattedChildren.isEmpty {
116122
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(\.trailingTrivia, indentedNewline)
117123
}

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
8080
let fileSpecs: [GeneratedFileSpec] =
8181
[
8282
// SwiftBasicFormat
83-
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat.swift"], basicFormatFile),
83+
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat+Extensions.swift"], basicFormatExtensionsFile),
8484

8585
// IDEUtils
8686
GeneratedFileSpec(swiftIdeUtilsGeneratedDir + ["SyntaxClassification.swift"], syntaxClassificationFile),

0 commit comments

Comments
 (0)