Skip to content

[SourceKit] Align closures inside function arguments. (3.0) #4890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ class FormatContext {
return LineAndColumn;
}

bool exprEndAtLine(Expr *E, unsigned Line) {
return E->getEndLoc().isValid() && SM.getLineNumber(E->getEndLoc()) == Line;
};

bool shouldAddIndentForLine(unsigned Line) {
if (Cursor == Stack.rend())
return false;
Expand Down Expand Up @@ -399,16 +403,29 @@ class FormatContext {
// Character(UnicodeScalar(c))
// }) <--- No indentation here.
auto AtCursorExpr = Cursor->getAsExpr();
if (AtCursorExpr && (isa<ParenExpr>(AtCursorExpr) ||
isa<TupleExpr>(AtCursorExpr))) {
if (AtExprEnd && isa<CallExpr>(AtExprEnd)) {
if (AtExprEnd->getEndLoc().isValid() &&
AtCursorExpr->getEndLoc().isValid() &&
Line == SM.getLineNumber(AtExprEnd->getEndLoc()) &&
Line == SM.getLineNumber(AtCursorExpr->getEndLoc())) {
if (AtExprEnd && AtCursorExpr && (isa<ParenExpr>(AtCursorExpr) ||
isa<TupleExpr>(AtCursorExpr))) {
if (isa<CallExpr>(AtExprEnd)) {
if (exprEndAtLine(AtExprEnd, Line) &&
exprEndAtLine(AtCursorExpr, Line)) {
return false;
}
}

// foo(A: {
// ...
// }, B: { <--- No indentation here.
// ...
// })
if (auto *TE = dyn_cast<TupleExpr>(AtCursorExpr)) {
if (isa<ClosureExpr>(AtExprEnd) && exprEndAtLine(AtExprEnd, Line)) {
for (auto *ELE : TE->getElements()) {
if (exprEndAtLine(ELE, Line)) {
return false;
}
}
}
}
}

// Indent another level from the outer context by default.
Expand Down
13 changes: 13 additions & 0 deletions test/SourceKit/CodeFormat/indent-closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func foo6() {
})
}

func foo7(A: ()->(), B: ()->()) {}

func foo8() {
foo7(A: { _ in
print("hello")
}, B: {
print("world")
})
}

// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s >%t.response
// RUN: %sourcekitd-test -req=format -line=4 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=5 -length=1 %s >>%t.response
Expand All @@ -57,6 +67,7 @@ func foo6() {
// RUN: %sourcekitd-test -req=format -line=31 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=32 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=42 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=50 -length=1 %s >>%t.response
// RUN: %FileCheck --strict-whitespace %s <%t.response

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

// CHECK: key.sourcetext: " })"

// CHECK: key.sourcetext: " }, B: {"