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

46 lines
1.6 KiB
Swift

//
// OrganizeDeclarations.swift
// SwiftFormat
//
// Created by Cal Stephens on 8/16/20.
// Copyright © 2024 Nick Lockwood. All rights reserved.
//
import Foundation
public extension FormatRule {
static let organizeDeclarations = FormatRule(
help: "Organize declarations within class, struct, enum, actor, and extension bodies.",
runOnceOnly: true,
disabledByDefault: true,
orderAfter: [.extensionAccessControl, .redundantFileprivate],
options: [
"categorymark", "markcategories", "beforemarks",
"lifecycle", "organizetypes", "structthreshold", "classthreshold",
"enumthreshold", "extensionlength", "organizationmode",
"visibilityorder", "typeorder", "visibilitymarks", "typemarks",
],
sharedOptions: ["sortedpatterns", "lineaftermarks"]
) { formatter in
guard !formatter.options.fragment else { return }
formatter.mapRecursiveDeclarations { declaration in
switch declaration {
// Organize the body of type declarations
case let .type(kind, open, body, close, originalRange):
let organizedType = formatter.organizeDeclaration((kind, open, body, close))
return .type(
kind: organizedType.kind,
open: organizedType.open,
body: organizedType.body,
close: organizedType.close,
originalRange: originalRange
)
case .conditionalCompilation, .declaration:
return declaration
}
}
}
}