SwiftStepdown rearranges one list of declarations at a time: a type's or an extension's members, an
#if block's, or a file's own top level. It never moves a declaration from one list to another, so a
method stays in its type, and an extension's members stay in that extension.
A declaration moves when it can be a helper and is no wider than the style allows:
- It runs code when used: a function, a computed property, or a subscript.
- Its access fits
moves:privateandfileprivateby default, which an extension's own modifier sets for members that write none (Configuration).
Everything else is an anchor and keeps its place among the other anchors: stored properties, a computed
property with an init accessor (which a memberwise initializer takes in order, as it does storage),
initializers, deinit, nested types, enum cases, type aliases, #if blocks, and the declarations the
style holds back. A helper that nothing in its list uses is an anchor too, so code only called from
another file, or by a framework, stays where it is.
The new order walks the anchors from the top. After each one come the helpers it uses, in the order it first uses them, each followed in turn by its own helpers, so the list reads from the top down:
func checkout() { charge(total()) } // anchor
private func total() -> Int { … } // checkout's first helper
private func tax() -> Int { … } // total's helper, before checkout's second
private func charge(_ cents: Int) { … }- A helper several declarations use goes below the last of them, so every use is above it. With
"sharedHelpers": "first"it goes below the first instead, nearer where a reader meets it. - Helpers that use each other in a cycle, such as two mutually recursive parsers, are placed from the one their callers reach first; a cycle nothing outside it calls stays where its first member is.
- A stored property's initializer does not pull a helper up among the properties: a helper used only there stays where it is.
- A file's top level treats each type and extension as using the file's own helpers that it calls
and does not declare itself, so a
private funcat file scope goes below the last type that uses it.
By default a // MARK: comment starts a section, and declarations move only within their own section.
A codebase organized into // MARK: - Private sections is left as its author arranged it, apart from
order within each section.
With "sections": "merge", helpers move across sections to sit below their callers. Each // MARK:
stays where it was, above whatever now starts its section, and one left with nothing below it is
removed.
A comment that turns a tool off or on until further notice, such as // swiftlint:disable,
// swiftformat:enable, // swiftformat:sort:begin, or // sourcery:inline:, bounds a region that
nothing moves into or out of, whatever sections says, so a rule disabled for three declarations stays
disabled for those three. A directive for one line, such as // swiftlint:disable:next, moves with its
declaration.
A declaration takes its doc comment, attributes, and the comments directly above it along when it moves.
A // MARK: stays with its section, and a file's header comment, the one separated from the first
declaration by a blank line, stays at the top of the file.
A declaration whose neighbor above is unchanged keeps its spacing exactly. One with a new neighbor gets the wider of its own gap and the gap that followed that neighbor, so a run of one-liners stays a run, and a blank line whenever either of the two spans lines or carries comments.
main.swiftand any file with statements at the top level, whose order is the program's.- A file swift-syntax cannot parse cleanly, whether from a syntax error in a half-typed file or from syntax newer than it knows; it is reported and left as it is.
- A body whose declarations share a line, as in
func a() {}; func b() {}. - Protocols, whose requirements have no bodies to place helpers by.
- Statements inside functions, including local functions.
- Stored properties, whose order sets a struct's memberwise initializer and the order
Codablewrites keys in.
Formatting is idempotent: a second run changes nothing. --lint reports exactly what a run would change.