Swift Package Manager plugins for SwiftFormat, using the prebuilt SwiftFormat binary.
Forked from MarcoEidinger/SwiftFormatPlugin, which is no longer maintained.
- Fast builds: SwiftFormat ships as a prebuilt binary, so it doesn't get compiled from source (unlike depending on
nicklockwood/SwiftFormatdirectly). - Format on build: a build tool plugin formats your code before every build, not just on demand.
- One config for the whole project: in an Xcode project, both plugins format the entire project directory with the
.swiftformatat its root, including local packages that live inside it. - Local dependencies included:
--target Xalso formats the targets X depends on within the same package. - Always current: an hourly workflow picks up new SwiftFormat releases. Each version is tagged
vX.Y.Zand matches the SwiftFormat release of the same number.
dependencies: [
.package(url: "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/weakfl/SwiftFormatPlugin", from: "0.64.5"),
]Both plugins read a .swiftformat file:
| Context | Config lookup |
|---|---|
| Swift package | package root and/or the target's directory |
| Xcode project | project root |
The build tool plugin fails the build if no config is found. If both the package root and the target directory contain one, SwiftFormat runs once per config.
Formats code before each build.
.target(
name: "MyLibrary",
plugins: [.plugin(name: "SwiftFormatPlugin", package: "SwiftFormatPlugin")]
)This formats the target's directory.
Add SwiftFormatPlugin under the target's Build Phases → Run Build Tool Plug-ins. This formats the whole project directory.
Formats code on demand.
swift package --allow-writing-to-package-directory swiftformat- Without
--target, every target is formatted. --target MyLibraryformatsMyLibraryplus its local dependencies. Pass it multiple times to select more targets.- Any other arguments go to SwiftFormat, e.g.
--swift-version 6.0or--lint.
Right-click the project in the navigator and choose SwiftFormat. The whole project directory is formatted, and --target is ignored.