Skip to content

Commit 0dcb6df

Browse files
committed
Fix CodeGenerationFormat to correctly indent LabeledExprListSyntax in method calls that start on a new line
1 parent a32bdbf commit 0dcb6df

File tree

10 files changed

+561
-549
lines changed

10 files changed

+561
-549
lines changed

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ public class CodeGenerationFormat: BasicFormat {
9696
let children = node.children(viewMode: .all)
9797
// Short tuple element list literals are presented on one line, list each element on a different line.
9898
if children.count > maxElementsOnSameLine {
99+
let inMethodCallThatStartsOnNewline =
100+
node.parent?.as(FunctionCallExprSyntax.self)?.calledExpression.as(MemberAccessExprSyntax.self)?.period
101+
.startsOnNewline ?? false
102+
if inMethodCallThatStartsOnNewline {
103+
increaseIndentationLevel()
104+
}
105+
defer {
106+
if inMethodCallThatStartsOnNewline {
107+
decreaseIndentationLevel()
108+
}
109+
}
110+
99111
return LabeledExprListSyntax(
100112
formatChildrenSeparatedByNewline(children: children, elementType: LabeledExprSyntax.self)
101113
)
@@ -111,10 +123,7 @@ public class CodeGenerationFormat: BasicFormat {
111123
if indentManually {
112124
return false
113125
}
114-
let startsOnNewline =
115-
node.leadingTrivia.contains(where: \.isNewline)
116-
|| node.previousToken(viewMode: .sourceAccurate)?.trailingTrivia.contains(where: \.isNewline) ?? false
117-
if !startsOnNewline {
126+
if !node.startsOnNewline {
118127
return false
119128
}
120129
default:
@@ -151,11 +160,7 @@ public class CodeGenerationFormat: BasicFormat {
151160
var child = child
152161
child.trailingTrivia = Trivia(pieces: child.trailingTrivia.drop(while: \.isSpaceOrTab))
153162

154-
let startsOnNewline =
155-
child.leadingTrivia.contains(where: \.isNewline)
156-
|| child.previousToken(viewMode: .sourceAccurate)?.trailingTrivia.contains(where: \.isNewline) ?? false
157-
158-
if !startsOnNewline {
163+
if !child.startsOnNewline {
159164
child.leadingTrivia = indentedNewline + child.leadingTrivia
160165
}
161166
return child
@@ -174,3 +179,10 @@ public class CodeGenerationFormat: BasicFormat {
174179
return formattedChildren
175180
}
176181
}
182+
183+
private extension SyntaxProtocol {
184+
var startsOnNewline: Bool {
185+
return self.leadingTrivia.contains(where: \.isNewline)
186+
|| self.previousToken(viewMode: .sourceAccurate)?.trailingTrivia.contains(where: \.isNewline) ?? false
187+
}
188+
}

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ public struct AccessorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _LeafDeclS
369369
}
370370
return Syntax(self)
371371
.replacingChild(
372-
at: 1,
373-
with: collection,
374-
rawNodeArena: arena,
375-
allocationArena: arena
376-
)
372+
at: 1,
373+
with: collection,
374+
rawNodeArena: arena,
375+
allocationArena: arena
376+
)
377377
.cast(AccessorDeclSyntax.self)
378378
}
379379

@@ -918,11 +918,11 @@ public struct ActorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _LeafDeclSynt
918918
}
919919
return Syntax(self)
920920
.replacingChild(
921-
at: 1,
922-
with: collection,
923-
rawNodeArena: arena,
924-
allocationArena: arena
925-
)
921+
at: 1,
922+
with: collection,
923+
rawNodeArena: arena,
924+
allocationArena: arena
925+
)
926926
.cast(ActorDeclSyntax.self)
927927
}
928928

@@ -964,11 +964,11 @@ public struct ActorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _LeafDeclSynt
964964
}
965965
return Syntax(self)
966966
.replacingChild(
967-
at: 3,
968-
with: collection,
969-
rawNodeArena: arena,
970-
allocationArena: arena
971-
)
967+
at: 3,
968+
with: collection,
969+
rawNodeArena: arena,
970+
allocationArena: arena
971+
)
972972
.cast(ActorDeclSyntax.self)
973973
}
974974

@@ -1362,11 +1362,11 @@ public struct ArrayExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _LeafExprSynt
13621362
}
13631363
return Syntax(self)
13641364
.replacingChild(
1365-
at: 3,
1366-
with: collection,
1367-
rawNodeArena: arena,
1368-
allocationArena: arena
1369-
)
1365+
at: 3,
1366+
with: collection,
1367+
rawNodeArena: arena,
1368+
allocationArena: arena
1369+
)
13701370
.cast(ArrayExprSyntax.self)
13711371
}
13721372

@@ -2095,11 +2095,11 @@ public struct AssociatedTypeDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _Lea
20952095
}
20962096
return Syntax(self)
20972097
.replacingChild(
2098-
at: 1,
2099-
with: collection,
2100-
rawNodeArena: arena,
2101-
allocationArena: arena
2102-
)
2098+
at: 1,
2099+
with: collection,
2100+
rawNodeArena: arena,
2101+
allocationArena: arena
2102+
)
21032103
.cast(AssociatedTypeDeclSyntax.self)
21042104
}
21052105

@@ -2141,11 +2141,11 @@ public struct AssociatedTypeDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _Lea
21412141
}
21422142
return Syntax(self)
21432143
.replacingChild(
2144-
at: 3,
2145-
with: collection,
2146-
rawNodeArena: arena,
2147-
allocationArena: arena
2148-
)
2144+
at: 3,
2145+
with: collection,
2146+
rawNodeArena: arena,
2147+
allocationArena: arena
2148+
)
21492149
.cast(AssociatedTypeDeclSyntax.self)
21502150
}
21512151

@@ -3264,11 +3264,11 @@ public struct AttributedTypeSyntax: TypeSyntaxProtocol, SyntaxHashable, _LeafTyp
32643264
}
32653265
return Syntax(self)
32663266
.replacingChild(
3267-
at: 1,
3268-
with: collection,
3269-
rawNodeArena: arena,
3270-
allocationArena: arena
3271-
)
3267+
at: 1,
3268+
with: collection,
3269+
rawNodeArena: arena,
3270+
allocationArena: arena
3271+
)
32723272
.cast(AttributedTypeSyntax.self)
32733273
}
32743274

@@ -3310,11 +3310,11 @@ public struct AttributedTypeSyntax: TypeSyntaxProtocol, SyntaxHashable, _LeafTyp
33103310
}
33113311
return Syntax(self)
33123312
.replacingChild(
3313-
at: 3,
3314-
with: collection,
3315-
rawNodeArena: arena,
3316-
allocationArena: arena
3317-
)
3313+
at: 3,
3314+
with: collection,
3315+
rawNodeArena: arena,
3316+
allocationArena: arena
3317+
)
33183318
.cast(AttributedTypeSyntax.self)
33193319
}
33203320

@@ -3750,11 +3750,11 @@ public struct AvailabilityConditionSyntax: SyntaxProtocol, SyntaxHashable, _Leaf
37503750
}
37513751
return Syntax(self)
37523752
.replacingChild(
3753-
at: 5,
3754-
with: collection,
3755-
rawNodeArena: arena,
3756-
allocationArena: arena
3757-
)
3753+
at: 5,
3754+
with: collection,
3755+
rawNodeArena: arena,
3756+
allocationArena: arena
3757+
)
37583758
.cast(AvailabilityConditionSyntax.self)
37593759
}
37603760

@@ -4309,11 +4309,11 @@ public struct BackDeployedAttributeArgumentsSyntax: SyntaxProtocol, SyntaxHashab
43094309
}
43104310
return Syntax(self)
43114311
.replacingChild(
4312-
at: 5,
4313-
with: collection,
4314-
rawNodeArena: arena,
4315-
allocationArena: arena
4316-
)
4312+
at: 5,
4313+
with: collection,
4314+
rawNodeArena: arena,
4315+
allocationArena: arena
4316+
)
43174317
.cast(BackDeployedAttributeArgumentsSyntax.self)
43184318
}
43194319

0 commit comments

Comments
 (0)