Skip to content

Commit 7cb5d9a

Browse files
authored
[SourceKit] When function calls taking multiple closures, align the end of them. rdar://27473586 (#4787)
1 parent 74c3544 commit 7cb5d9a

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

lib/IDE/Formatting.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class FormatContext {
232232
return LineAndColumn;
233233
}
234234

235+
bool exprEndAtLine(Expr *E, unsigned Line) {
236+
return E->getEndLoc().isValid() && SM.getLineNumber(E->getEndLoc()) == Line;
237+
};
238+
235239
bool shouldAddIndentForLine(unsigned Line) {
236240
if (Cursor == Stack.rend())
237241
return false;
@@ -399,16 +403,29 @@ class FormatContext {
399403
// Character(UnicodeScalar(c))
400404
// }) <--- No indentation here.
401405
auto AtCursorExpr = Cursor->getAsExpr();
402-
if (AtCursorExpr && (isa<ParenExpr>(AtCursorExpr) ||
403-
isa<TupleExpr>(AtCursorExpr))) {
404-
if (AtExprEnd && isa<CallExpr>(AtExprEnd)) {
405-
if (AtExprEnd->getEndLoc().isValid() &&
406-
AtCursorExpr->getEndLoc().isValid() &&
407-
Line == SM.getLineNumber(AtExprEnd->getEndLoc()) &&
408-
Line == SM.getLineNumber(AtCursorExpr->getEndLoc())) {
406+
if (AtExprEnd && AtCursorExpr && (isa<ParenExpr>(AtCursorExpr) ||
407+
isa<TupleExpr>(AtCursorExpr))) {
408+
if (isa<CallExpr>(AtExprEnd)) {
409+
if (exprEndAtLine(AtExprEnd, Line) &&
410+
exprEndAtLine(AtCursorExpr, Line)) {
409411
return false;
410412
}
411413
}
414+
415+
// foo(A: {
416+
// ...
417+
// }, B: { <--- No indentation here.
418+
// ...
419+
// })
420+
if (auto *TE = dyn_cast<TupleExpr>(AtCursorExpr)) {
421+
if (isa<ClosureExpr>(AtExprEnd) && exprEndAtLine(AtExprEnd, Line)) {
422+
for (auto *ELE : TE->getElements()) {
423+
if (exprEndAtLine(ELE, Line)) {
424+
return false;
425+
}
426+
}
427+
}
428+
}
412429
}
413430

414431
// Indent another level from the outer context by default.

test/SourceKit/CodeFormat/indent-closure.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func foo6() {
4242
})
4343
}
4444

45+
func foo7(A: ()->(), B: ()->()) {}
46+
47+
func foo8() {
48+
foo7(A: { _ in
49+
print("hello")
50+
}, B: {
51+
print("world")
52+
})
53+
}
54+
4555
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s >%t.response
4656
// RUN: %sourcekitd-test -req=format -line=4 -length=1 %s >>%t.response
4757
// RUN: %sourcekitd-test -req=format -line=5 -length=1 %s >>%t.response
@@ -57,6 +67,7 @@ func foo6() {
5767
// RUN: %sourcekitd-test -req=format -line=31 -length=1 %s >>%t.response
5868
// RUN: %sourcekitd-test -req=format -line=32 -length=1 %s >>%t.response
5969
// RUN: %sourcekitd-test -req=format -line=42 -length=1 %s >>%t.response
70+
// RUN: %sourcekitd-test -req=format -line=50 -length=1 %s >>%t.response
6071
// RUN: %FileCheck --strict-whitespace %s <%t.response
6172

6273
// CHECK: key.sourcetext: " var abc = 1"
@@ -81,3 +92,5 @@ func foo6() {
8192
// CHECK: key.sourcetext: " }()"
8293

8394
// CHECK: key.sourcetext: " })"
95+
96+
// CHECK: key.sourcetext: " }, B: {"

0 commit comments

Comments
 (0)