Skip to content

Use real line number when moving block indentation for close breaks. #190

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
Apr 15, 2020
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
5 changes: 4 additions & 1 deletion Sources/SwiftFormatPrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,14 @@ public class PrettyPrinter {
= openCloseBreakCompensatingLineNumber != matchingOpenBreak.lineNumber

if matchingOpenBreak.contributesBlockIndent {
// The actual line number is used, instead of the compensating line number. When the close
// break is at the start of a new line, the block indentation isn't carried to the new line.
let currentLine = lineNumber
// When two or more open breaks are encountered on the same line, only the final open
// break is allowed to increase the block indent, avoiding multiple block indents. As the
// open breaks on that line are closed, the new final open break must be enabled again to
// add a block indent.
if matchingOpenBreak.lineNumber == openCloseBreakCompensatingLineNumber,
if matchingOpenBreak.lineNumber == currentLine,
let lastActiveOpenBreak = activeOpenBreaks.last,
lastActiveOpenBreak.kind == .block,
!lastActiveOpenBreak.contributesBlockIndent
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,36 @@ final class FunctionCallTests: PrettyPrintTestCase {
func testArgumentStartsWithOpenDelimiter() {
let input =
"""
myFunc(someArray: [
])
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000])
myFunc(someDictionary: [
:])
myFunc(someDictionary: ["foo": "bar", "baz": "quux", "gli": "glop"])
myFunc(someClosure: {
})
myFunc(someClosure: { (a, b, c) in
})
myFunc(someClosure: { foo, bar in baz(1000, 2000, 3000, 4000, 5000) })
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in bar() }
myFunc(someArray: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) { foo in someMuchLongerLineBreakingBarFunction() }
"""

let expected =
"""
myFunc(someArray: [])
myFunc(someArray: [
1000, 2000, 3000, 4000, 5000, 6000, 7000,
8000,
])
myFunc(someDictionary: [:])
myFunc(someDictionary: [
"foo": "bar", "baz": "quux", "gli": "glop",
])
myFunc(someClosure: {
})
myFunc(someClosure: { (a, b, c) in
})
myFunc(someClosure: { foo, bar in
baz(1000, 2000, 3000, 4000, 5000)
})
Expand Down