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

44 lines
1.5 KiB
Swift

//
// BlankLineAfterSwitchCase.swift
// SwiftFormat
//
// Created by Cal Stephens on 2/1/24.
// Copyright © 2024 Nick Lockwood. All rights reserved.
//
import Foundation
public extension FormatRule {
static let blankLineAfterSwitchCase = FormatRule(
help: """
Insert a blank line after multiline switch cases (excluding the last case,
which is followed by a closing brace).
""",
disabledByDefault: true,
orderAfter: [.redundantBreak]
) { formatter in
formatter.forEach(.keyword("switch")) { switchIndex, _ in
guard let switchCases = formatter.switchStatementBranchesWithSpacingInfo(at: switchIndex) else { return }
for switchCase in switchCases.reversed() {
// Any switch statement that spans multiple lines should be followed by a blank line
// (excluding the last case, which is followed by a closing brace).
if switchCase.spansMultipleLines,
!switchCase.isLastCase,
!switchCase.isFollowedByBlankLine
{
switchCase.insertTrailingBlankLine(using: formatter)
}
// The last case should never be followed by a blank line, since it's
// already followed by a closing brace.
if switchCase.isLastCase,
switchCase.isFollowedByBlankLine
{
switchCase.removeTrailingBlankLine(using: formatter)
}
}
}
}
}