mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
45 lines
1.2 KiB
Swift
45 lines
1.2 KiB
Swift
//
|
|
// LinebreakAtEndOfFile.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 8/22/16.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Always end file with a linebreak, to avoid incompatibility with certain unix tools:
|
|
/// http://stackoverflow.com/questions/2287967/why-is-it-recommended-to-have-empty-line-in-the-end-of-file
|
|
static let linebreakAtEndOfFile = FormatRule(
|
|
help: "Add empty blank line at end of file.",
|
|
sharedOptions: ["linebreaks"]
|
|
) { formatter in
|
|
guard !formatter.options.fragment else { return }
|
|
var wasLinebreak = true
|
|
formatter.forEachToken(onlyWhereEnabled: false) { _, token in
|
|
switch token {
|
|
case .linebreak:
|
|
wasLinebreak = true
|
|
case .space:
|
|
break
|
|
default:
|
|
wasLinebreak = false
|
|
}
|
|
}
|
|
if formatter.isEnabled, !wasLinebreak {
|
|
formatter.insertLinebreak(at: formatter.tokens.count)
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
struct Foo {↩
|
|
let bar: Bar↩
|
|
- }
|
|
+ }↩
|
|
+
|
|
```
|
|
"""
|
|
}
|
|
}
|