mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
52 lines
1.5 KiB
Swift
52 lines
1.5 KiB
Swift
//
|
|
// SpaceAroundBraces.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 8/22/16.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Ensure that there is space between an opening brace and the preceding
|
|
/// identifier, and between a closing brace and the following identifier.
|
|
static let spaceAroundBraces = FormatRule(
|
|
help: "Add or remove space around curly braces."
|
|
) { formatter in
|
|
formatter.forEach(.startOfScope("{")) { i, _ in
|
|
if let prevToken = formatter.token(at: i - 1) {
|
|
switch prevToken {
|
|
case .space, .linebreak, .operator(_, .prefix), .operator(_, .infix),
|
|
.startOfScope where !prevToken.isStringDelimiter:
|
|
break
|
|
default:
|
|
formatter.insert(.space(" "), at: i)
|
|
}
|
|
}
|
|
}
|
|
formatter.forEach(.endOfScope("}")) { i, _ in
|
|
if let nextToken = formatter.token(at: i + 1) {
|
|
switch nextToken {
|
|
case .identifier, .keyword:
|
|
formatter.insert(.space(" "), at: i + 1)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
- foo.filter{ return true }.map{ $0 }
|
|
+ foo.filter { return true }.map { $0 }
|
|
```
|
|
|
|
```diff
|
|
- foo( {} )
|
|
+ foo({})
|
|
```
|
|
"""
|
|
}
|
|
}
|