Files
SwiftFormat/Sources/Rules/SpaceAroundBraces.swift
T
2024-11-09 11:26:22 +00:00

40 lines
1.3 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
}
}
}
}
}