# SwiftFormat — Opinionated Swift Code Formatter > A command-line tool and Xcode extension for automatically formatting Swift code according to configurable style rules. ## Install Save as a script file and run: # SwiftFormat — Opinionated Swift Code Formatter ## Quick Use ```bash brew install swiftformat # Format all Swift files in a directory swiftformat . # Preview changes without writing swiftformat --dryrun . # Use with specific rules swiftformat --rules indent,braces,trailingCommas . ``` ## Introduction SwiftFormat is an opinionated code formatter for Swift that enforces a consistent style across your codebase. It rewrites Swift files in place based on a comprehensive set of configurable rules covering indentation, spacing, brace placement, import sorting, and dozens of other style preferences. ## What SwiftFormat Does - Automatically reformats Swift source files to match configured style rules - Provides 70+ individual formatting rules that can be enabled or disabled - Runs as a CLI tool, Xcode source editor extension, or SPM build plugin - Supports a .swiftformat configuration file for project-wide settings - Integrates with Git hooks and CI pipelines for automated enforcement ## Architecture Overview SwiftFormat parses Swift source into a token stream using its own lightweight tokenizer (not the full Swift compiler). It then applies a sequence of formatting rules as token-level transformations. Each rule is an independent function that scans and modifies the token array. Rules are applied in a defined order, with multiple passes to resolve dependencies between rules. The formatted token stream is then serialized back to source text. ## Self-Hosting & Configuration - Install via Homebrew: `brew install swiftformat` - Or via Mint, CocoaPods, or as an SPM package plugin - Create a .swiftformat file to configure rules, indentation, and excluded paths - Add as a Git pre-commit hook for automatic formatting on commit - Install the Xcode Source Editor Extension for in-editor formatting ## Key Features - Token-based formatting that preserves comments and non-standard syntax - Configurable indentation (tabs or spaces with custom width) - Import sorting, redundant keyword removal, and trailing comma insertion - Xcode extension for formatting selections or entire files within the editor - Lint mode (--lint) that reports violations without modifying files for CI use ## Comparison with Similar Tools - **swift-format (Apple)** — Apple's official formatter; fewer rules and less configurable - **SwiftLint** — focuses on linting and warnings rather than auto-formatting; complementary tool - **Prettier** — multi-language formatter; does not support Swift - **clang-format** — C/C++/Objective-C formatter; no Swift support - **Xcode auto-indentation** — basic indentation only; no comprehensive style enforcement ## FAQ **Q: What is the difference between SwiftFormat and SwiftLint?** A: SwiftFormat rewrites code to match style rules. SwiftLint reports violations and can auto-correct some issues. Many teams use both together. **Q: Can I disable specific rules for parts of my code?** A: Yes. Use `// swiftformat:disable ` and `// swiftformat:enable ` comments to toggle rules inline. **Q: Does SwiftFormat support Swift 6 and strict concurrency?** A: Yes. The tokenizer handles modern Swift syntax including actors, async/await, and parameter packs. **Q: Will SwiftFormat change the behavior of my code?** A: The formatter only changes whitespace, formatting, and certain redundant syntax. It does not alter logic or semantics. The --dryrun flag lets you preview all changes before applying. ## Sources - https://github.com/nicklockwood/SwiftFormat - https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md --- Source: https://tokrepo.com/en/workflows/asset-ef0019e0 Author: Script Depot