Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

SwiftStepdown

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.

Why

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 private and fileprivate declarations 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 }
}

Get started

SwiftStepdown needs macOS 14+ and Swift 6 (Xcode 16 or later).

  1. Install it with Mint, and put ~/.mint/bin on your PATH:

    mint install acfriedman/SwiftStepdown
  2. See what would move, without changing anything:

    swiftstepdown --lint Sources
  3. Reorder, then review the diff as you would any formatter's:

    swiftstepdown Sources
  4. To let more than private helpers move, say so in .swiftstepdown.json:

    { "moves": "internal" }

Learn more

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

License

Copyright 2026 Andrew C. Friedman. Licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages