A formatter that puts a Swift file's declarations in reading order, each helper below the code that uses it. You can reorder files in place, or check them in CI, and you choose which declarations may move.
Code reads best from the top down: what a type does first, then how, each helper right below what calls it. This is the stepdown rule. In most projects, though, order is an accident of history. Helpers pile up at the bottom or sit above their callers, and a reader scrolls back and forth to follow one call. Formatters and linters can sort declarations by kind and access, but they don't look at who calls whom. SwiftStepdown does:
- Callers above callees. Each helper moves right below the last declaration that uses it, followed by its own helpers, so a type reads like an outline.
- Your API stays put. By default only
privateandfileprivatedeclarations move; everything else keeps the order you gave it. - No build needed. It matches names with swift-syntax alone, in about 5 ms a file, and agrees with the compiler's own index on 99% of links across 100,000 lines of real code (Accuracy).
- Safe by construction. Order inside a type means nothing to the compiler, so a reordered file builds
and behaves the same. Stored properties,
main.swift, and protocols are never touched.
This file has two helpers above the method that uses them:
struct Checkout {
private func tax(on cents: Int) -> Int { cents / 10 }
private func total(of cart: Cart) -> Int { cart.cents + tax(on: cart.cents) }
func receipt(for cart: Cart) -> String {
"Total: \(total(of: cart))"
}
}swiftstepdown --lint says where each belongs:
Checkout.swift:2:5: error: `tax(on:)` belongs right below `total(of:)`, so each declaration sits below what uses it. [stepdown]
Checkout.swift:4:5: error: `total(of:)` belongs right below `receipt(for:)`, so each declaration sits below what uses it. [stepdown]
And swiftstepdown moves them there, doc comments and all:
struct Checkout {
func receipt(for cart: Cart) -> String {
"Total: \(total(of: cart))"
}
private func total(of cart: Cart) -> Int { cart.cents + tax(on: cart.cents) }
private func tax(on cents: Int) -> Int { cents / 10 }
}SwiftStepdown needs macOS 14+ and Swift 6 (Xcode 16 or later).
-
Install it with Mint, and put
~/.mint/binon yourPATH:mint install acfriedman/SwiftStepdown
-
See what would move, without changing anything:
swiftstepdown --lint Sources
-
Reorder, then review the diff as you would any formatter's:
swiftstepdown Sources
-
To let more than private helpers move, say so in
.swiftstepdown.json:{ "moves": "internal" }
| Guide | Covers |
|---|---|
| Installation | Mint, a version pinned in your repository, building from source |
| Usage | every flag, the output, exit codes, and which files are read |
| Setup | commit hooks, the pre-commit framework, Xcode, CI |
| Ordering | what moves, where it goes, // MARK: sections, cycles, and what is never touched |
| Configuration | .swiftstepdown.json: what moves, shared helpers, sections, excluded paths |
| Accuracy | how matching by name compares with the compiler, on seven real packages |
| Advanced | how names are matched, speed, the libraries, running the accuracy study yourself |
Copyright 2026 Andrew C. Friedman. Licensed under the MIT License.