mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
50 lines
1.3 KiB
Swift
50 lines
1.3 KiB
Swift
//
|
|
// ConsecutiveBlankLines.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 8/30/16.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Collapse all consecutive blank lines into a single blank line
|
|
static let consecutiveBlankLines = FormatRule(
|
|
help: "Replace consecutive blank lines with a single blank line."
|
|
) { formatter in
|
|
formatter.forEach(.linebreak) { i, _ in
|
|
guard let prevIndex = formatter.index(of: .nonSpace, before: i, if: { $0.isLinebreak }) else {
|
|
return
|
|
}
|
|
if let scope = formatter.currentScope(at: i), scope.isMultilineStringDelimiter {
|
|
return
|
|
}
|
|
if let nextIndex = formatter.index(of: .nonSpace, after: i) {
|
|
if formatter.tokens[nextIndex].isLinebreak {
|
|
formatter.removeTokens(in: i + 1 ... nextIndex)
|
|
}
|
|
} else if !formatter.options.fragment {
|
|
formatter.removeTokens(in: i ..< formatter.tokens.count)
|
|
}
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
func foo() {
|
|
let x = "bar"
|
|
-
|
|
|
|
print(x)
|
|
}
|
|
|
|
func foo() {
|
|
let x = "bar"
|
|
|
|
print(x)
|
|
}
|
|
```
|
|
"""
|
|
}
|
|
}
|