Skip to content

Commit 071b3aa

Browse files
committed
Use real line number when moving block indentation for close breaks.
1 parent 86d467f commit 071b3aa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,14 @@ public class PrettyPrinter {
359359
= openCloseBreakCompensatingLineNumber != matchingOpenBreak.lineNumber
360360

361361
if matchingOpenBreak.contributesBlockIndent {
362+
// The actual line number is used, instead of the compensating line number. When the close
363+
// break is at the start of a new line, the block indentation isn't carried to the new line.
364+
let currentLine = lineNumber
362365
// When two or more open breaks are encountered on the same line, only the final open
363366
// break is allowed to increase the block indent, avoiding multiple block indents. As the
364367
// open breaks on that line are closed, the new final open break must be enabled again to
365368
// add a block indent.
366-
if matchingOpenBreak.lineNumber == openCloseBreakCompensatingLineNumber,
369+
if matchingOpenBreak.lineNumber == currentLine,
367370
let lastActiveOpenBreak = activeOpenBreaks.last,
368371
lastActiveOpenBreak.kind == .block,
369372
!lastActiveOpenBreak.contributesBlockIndent

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,36 @@ final class FunctionCallTests: PrettyPrintTestCase {
135135
func testArgumentStartsWithOpenDelimiter() {
136136
let input =
137137
"""
138+
myFunc(someArray: [
139+
])
138140
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000])
141+
myFunc(someDictionary: [
142+
:])
139143
myFunc(someDictionary: ["foo": "bar", "baz": "quux", "gli": "glop"])
144+
myFunc(someClosure: {
145+
})
146+
myFunc(someClosure: { (a, b, c) in
147+
})
140148
myFunc(someClosure: { foo, bar in baz(1000, 2000, 3000, 4000, 5000) })
141149
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in bar() }
142150
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in someMuchLongerLineBreakingBarFunction() }
143151
"""
144152

145153
let expected =
146154
"""
155+
myFunc(someArray: [])
147156
myFunc(someArray: [
148157
1000, 2000, 3000, 4000, 5000, 6000, 7000,
149158
8000,
150159
])
160+
myFunc(someDictionary: [:])
151161
myFunc(someDictionary: [
152162
"foo": "bar", "baz": "quux", "gli": "glop",
153163
])
164+
myFunc(someClosure: {
165+
})
166+
myFunc(someClosure: { (a, b, c) in
167+
})
154168
myFunc(someClosure: { foo, bar in
155169
baz(1000, 2000, 3000, 4000, 5000)
156170
})

0 commit comments

Comments
 (0)