Skip to content

SourceKit/Indentation: align function names in chained trailing closures #25342

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
Jun 11, 2019
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
18 changes: 18 additions & 0 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,24 @@ class FormatContext {
}
}

// Chained trailing closures shouldn't require additional indentation.
// a.map {
// ...
// }.filter { <--- No indentation here.
// ...
// }.map { <--- No indentation here.
// ...
// }
if (AtExprEnd && AtCursorExpr && isa<CallExpr>(AtExprEnd)) {
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(AtCursorExpr)) {
if (auto *Base = UDE->getBase()) {
if (exprEndAtLine(Base, Line))
return false;
}
}
}


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

func foo9(input: [Int]){
input.map { (ele) in
ele + 1
}.filter{(ele) in
return ele > 10
}.map {(ele) in
return ele + 1
}
}

// 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 @@ -68,6 +78,17 @@ func foo8() {
// 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: %sourcekitd-test -req=format -line=55 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=56 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=57 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=58 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=59 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=60 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=61 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=62 -length=1 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=63 -length=1 %s >>%t.response

// RUN: %FileCheck --strict-whitespace %s <%t.response

// CHECK: key.sourcetext: " var abc = 1"
Expand All @@ -94,3 +115,13 @@ func foo8() {
// CHECK: key.sourcetext: " })"

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

// CHECK: key.sourcetext: "func foo9(input: [Int]){"
// CHECK: key.sourcetext: " input.map { (ele) in"
// CHECK: key.sourcetext: " ele + 1"
// CHECK: key.sourcetext: " }.filter{(ele) in"
// CHECK: key.sourcetext: " return ele > 10"
// CHECK: key.sourcetext: " }.map {(ele) in"
// CHECK: key.sourcetext: " return ele + 1"
// CHECK: key.sourcetext: " }"
// CHECK: key.sourcetext: "}"