Skip to content

Commit 513a2c7

Browse files
dylansturgallevato
authored andcommitted
Fix counting of consecutive newlines.
The math for calculating how many blank lines to allow didn't account for existing consecutive blank lines when the maximum blank lines configuration value was used. That meant an existing newline in the output would result in allowing max blank lines + 1 blank lines.
1 parent 5cca489 commit 513a2c7

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public class PrettyPrinter {
211211
numberToPrint = consecutiveNewlineCount == 0 ? 1 : 0
212212
case .soft(let count, _):
213213
// We add 1 to the max blank lines because it takes 2 newlines to create the first blank line.
214-
numberToPrint = min(count - consecutiveNewlineCount, configuration.maximumBlankLines + 1)
214+
numberToPrint = min(count, configuration.maximumBlankLines + 1) - consecutiveNewlineCount
215215
case .hard(let count):
216216
numberToPrint = count
217217
}

Tests/SwiftFormatPrettyPrintTests/NewlineTests.swift

+56
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,60 @@ final class NewlineTests: PrettyPrintTestCase {
7474

7575
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
7676
}
77+
78+
func testNewlinesBetweenMembers() {
79+
let input =
80+
"""
81+
82+
83+
class MyClazz {
84+
85+
lazy var memberView: UIView = {
86+
let view = UIView()
87+
return view
88+
}()
89+
90+
91+
func doSomething() {
92+
print("!")
93+
}
94+
95+
96+
func doSomethingElse() {
97+
print("else!")
98+
}
99+
100+
101+
let constMember = 1
102+
103+
104+
105+
}
106+
"""
107+
108+
let expected =
109+
"""
110+
class MyClazz {
111+
112+
lazy var memberView: UIView = {
113+
let view = UIView()
114+
return view
115+
}()
116+
117+
func doSomething() {
118+
print("!")
119+
}
120+
121+
func doSomethingElse() {
122+
print("else!")
123+
}
124+
125+
let constMember = 1
126+
127+
}
128+
129+
"""
130+
131+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
132+
}
77133
}

0 commit comments

Comments
 (0)