8000 Merge pull request #40 from dylansturg/closure_indentation · adevress/swift-syntax@bcbf033 · GitHub
[go: up one dir, main page]

Skip to content

Commit bcbf033

Browse files
authored
Merge pull request swiftlang#40 from dylansturg/closure_indentation
Prevent extra indentation for trailing closures in specific function …
2 parents 1ae671f + 8ea84fa commit bcbf033

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ public class PrettyPrinter {
334334
// breaks (if they are enabled).
335335
//
336336
// Note that in this case, the transformation of the current line into a continuation line
337-
// must happen unconditionally, not only if the break fires.
337+
// must happen regardless of whether this break fires.
338338
//
339339
// Likewise, we need to do this if we popped an old continuation state off the stack,
340340
// even if the break *doesn't* fire.
341-
currentLineIsContinuation = openedOnDifferentLine
341+
currentLineIsContinuation = matchingOpenBreak.didIndent && openedOnDifferentLine
342342
}
343343

344344
// Restore the continuation state of the scope we were in before the open break occurred.

Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ public class ClosureExprTests: PrettyPrintTestCase {
133133
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: config)
134134
}
135135

136+
public func testClosureArgumentsWithTrailingClosure() {
137+
let input =
138+
"""
139+
someFunc({ return s0 }) { return s2 }
140+
someLongerFunc({ return s0 }) { input in return s2 }
141+
someLongerFunc({ firstInput in someUsefulFunc(firstInput) }) { secondInput in return s2 }
142+
someLongerFunc({ firstInput in
143+
someUsefulFunc(firstInput) }) { secondInput in return someLineBreakingCall(secondInput) }
144+
"""
145+
146+
let expected =
147+
"""
148+
someFunc({ return s0 }) { return s2 }
149+
someLongerFunc({ return s0 }) { input in
150+
return s2
151+
}
152+
someLongerFunc({ firstInput in
153+
someUsefulFunc(firstInput)
154+
}) { secondInput in return s2 }
155+
someLongerFunc({ firstInput in
156+
someUsefulFunc(firstInput)
157+
}) { secondInput in
158+
return someLineBreakingCall(
159+
secondInput)
160+
}
161+
162+
"""
163+
164+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
165+
}
166+
136167
public func testClosuresWithIfs() {
137168
let input =
138169
"""

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public class FunctionCallTests: PrettyPrintTestCase {
140140
myFunc(someDictionary: ["foo": "bar", "baz": "quux", "glip": "glop"])
141141
myFunc(someClosure: { foo, bar in baz(1000, 2000, 3000, 4000, 5000) })
142142
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in bar() }
143+
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in someMuchLongerLineBreakingBarFunction() }
143144
"""
144145

145146
let expected =
@@ -158,6 +159,12 @@ public class FunctionCallTests: PrettyPrintTestCase {
158159
1000, 2000, 3000, 4000, 5000, 6000, 7000,
159160
8000
160161
]) { foo in bar() }
162+
myFunc(someArray: [
163+
1000, 2000, 3000, 4000, 5000, 6000, 7000,
164+
8000
165+
]) { foo in
166+
someMuchLongerLineBreakingBarFunction()
167+
}
161168
162169
"""
163170

@@ -171,6 +178,7 @@ public class FunctionCallTests: PrettyPrintTestCase {
171178
myFunc(["foo": "bar", "baz": "quux", "glip": "glop"])
172179
myFunc({ foo, bar in baz(1000, 2000, 3000, 4000, 5000) })
173180
myFunc([1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in bar() }
181+
myFunc([1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in someMuchLongerLineBreakingBarFunction() }
174182
"""
175183

176184
let expected =
@@ -189,6 +197,12 @@ public class FunctionCallTests: PrettyPrintTestCase {
189197
1000, 2000, 3000, 4000, 5000, 6000, 7000,
190198
8000
191199
]) { foo in bar() }
200+
myFunc([
201+
1000, 2000, 3000, 4000, 5000, 6000, 7000,
202+
8000
203+
]) { foo in
204+
someMuchLongerLineBreakingBarFunction()
205+
}
192206
193207
"""
194208

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extension ClosureExprTests {
9898
("testBasicFunctionClosures_noPackArguments", testBasicFunctionClosures_noPackArguments),
9999
("testBasicFunctionClosures_packArguments", testBasicFunctionClosures_packArguments),
100100
("testBodilessClosure", testBodilessClosure),
101+
("testClosureArgumentsWithTrailingClosure", testClosureArgumentsWithTrailingClosure),
101102
("testClosureCapture", testClosureCapture),
102103
("testClosureCaptureWithoutArguments", testClosureCaptureWithoutArguments),
103104
("testClosuresWithIfs", testClosuresWithIfs),

0 commit comments

Comments
 (0)
0