Skip to content

Commit 4c2c857

Browse files
committed
Fix indentation issue if a parsed syntax node with comments is used inside a builder
1 parent 12f2652 commit 4c2c857

File tree

11 files changed

+1581
-1060
lines changed

11 files changed

+1581
-1060
lines changed

CodeGeneration/Sources/generate-swiftbasicformat/FormatFile.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private func createBuildableNodeFormatFunction(node: Node) -> FunctionDecl {
181181
"result"
182182
AssignmentExpr()
183183
FunctionCallExpr(MemberAccessExpr(base: "result", name: "withLeadingTrivia")) {
184-
TupleExprElement(expression: FunctionCallExpr(MemberAccessExpr(base: "leadingTrivia", name: "addingSpacingAfterNewlinesIfNeeded")))
184+
TupleExprElement(expression: "leadingTrivia")
185185
}
186186
}
187187
}
@@ -203,8 +203,7 @@ private func createBuildableCollectionNodeFormatFunction(node: Node) -> Function
203203
MemberAccessExpr(base: "syntax", name: "map"),
204204
trailingClosure: ClosureExpr {
205205
FunctionCallExpr(MemberAccessExpr(base: "$0", name: "withLeadingTrivia")) {
206-
TupleExprElement(expression: FunctionCallExpr(MemberAccessExpr(
207-
base: TupleExpr {
206+
TupleExprElement(expression: TupleExpr {
208207
SequenceExpr {
209208
"indentedNewline"
210209
BinaryOperatorExpr("+")
@@ -216,9 +215,8 @@ private func createBuildableCollectionNodeFormatFunction(node: Node) -> Function
216215
}
217216
}
218217
}
219-
},
220-
name: "addingSpacingAfterNewlinesIfNeeded"
221-
)))
218+
}
219+
)
222220
}
223221
}
224222
))

CodeGeneration/Sources/generate-swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,14 @@ private func createBuildFunction(node: Node, trivias: [String]) -> FunctionDecl
416416
)
417417
)
418418
) {
419-
ReturnStmt(expression: "node")
419+
ReturnStmt(expression: FunctionCallExpr(MemberAccessExpr(base: "Indenter", name: "indent")) {
420+
TupleExprElement(expression: "node")
421+
TupleExprElement(
422+
label: "indentation",
423+
expression: MemberAccessExpr(base: "format", name: "indentTrivia")
424+
)
425+
}
426+
)
420427
}
421428
}
422429
}

CodeGeneration/Sources/generate-swiftsyntaxbuilder/SyntaxUtilities.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,36 @@ func createTriviaAttachment(varName: ExpressibleAsExprBuildable, triviaVarName:
137137
)
138138
}
139139
) {
140+
VariableDecl(
141+
.let,
142+
name: "trivia",
143+
initializer: FunctionCallExpr(MemberAccessExpr(
144+
base: TupleExpr {
145+
SequenceExpr {
146+
triviaVarName
147+
BinaryOperatorExpr("+")
148+
TupleExpr {
149+
SequenceExpr {
150+
MemberAccessExpr(base: varName, name: trivia)
151+
BinaryOperatorExpr("??")
152+
ArrayExpr()
153+
}
154+
}
155+
}
156+
},
157+
name: "indented"
158+
)) {
159+
TupleExprElement(
160+
label: "indentation",
161+
expression: MemberAccessExpr(base: "format", name: "indentTrivia")
162+
)
163+
}
164+
)
140165
SequenceExpr {
141166
varName
142167
AssignmentExpr()
143168
FunctionCallExpr(MemberAccessExpr(base: varName, name: "with\(trivia.withFirstCharacterUppercased)")) {
144-
TupleExprElement(expression: SequenceExpr {
145-
triviaVarName
146-
BinaryOperatorExpr("+")
147-
TupleExpr {
148-
SequenceExpr {
149-
MemberAccessExpr(base: varName, name: trivia)
150-
BinaryOperatorExpr("??")
151-
ArrayExpr()
152-
}
153-
}
154-
})
169+
TupleExprElement(expression: "trivia")
155170
}
156171
}
157172
}

Sources/SwiftBasicFormat/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_library(SwiftBasicFormat STATIC
10-
Trivia+Extension.swift
1110
generated/Format.swift
1211
)
1312

Sources/SwiftBasicFormat/Trivia+Extension.swift

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)