Skip to content

Commit 74995aa

Browse files
authored
Merge pull request swiftlang#245 from adellibovi/format
Fix a bug where code lines are lost for OrderedImports rule
2 parents 0708302 + 3174b1f commit 74995aa

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/SwiftFormatRules/OrderedImports.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ func generateLines(codeBlockItemList: CodeBlockItemListSyntax) -> [Line] {
230230
var afterNewline = false
231231
var isFirstBlock = true
232232

233+
func appendNewLine() {
234+
lines.append(currentLine)
235+
currentLine = Line()
236+
afterNewline = true // Note: trailing line comments always come before any newlines.
237+
}
238+
233239
for block in codeBlockItemList {
234240

235241
if let leadingTrivia = block.leadingTrivia {
@@ -240,9 +246,7 @@ func generateLines(codeBlockItemList: CodeBlockItemListSyntax) -> [Line] {
240246
// Create new Line objects when we encounter newlines.
241247
case .newlines(let N):
242248
for _ in 0..<N {
243-
lines.append(currentLine)
244-
currentLine = Line()
245-
afterNewline = true // Note: trailing line comments always come before any newlines.
249+
appendNewLine()
246250
}
247251
default:
248252
if afterNewline || isFirstBlock {
@@ -252,6 +256,8 @@ func generateLines(codeBlockItemList: CodeBlockItemListSyntax) -> [Line] {
252256
}
253257
}
254258
}
259+
} else if currentLine.codeBlock != nil {
260+
appendNewLine()
255261
}
256262
currentLine.codeBlock = block // This represents actual code: imports and otherwise.
257263
isFirstBlock = false

0 commit comments

Comments
 (0)