$ show nicklockwood/SwiftFormat
SwiftFormat is a mature, rule-based Swift source code formatter and linter. It operates as a command-line tool, an Xcode source editor extension, and a Swift Package Manager plugin. The core engine tokenizes Swift source code and applies a configurable set of formatting rules, each defined as a static `FormatRule` with help text, options, and a closure that manipulates the token stream via a `Formatter` API. The architecture is modular: rules are isolated in individual files under `Sources/Rules/`, the formatter provides reusable token manipulation primitives, and reporters (JSON, XML, SARIF, GitHub Actions) handle output. Configuration is hierarchical via `.swiftformat` files, inline directives, and command-line options. The system's primary data flow is: source file → tokenization → rule application (ordered by dependencies) → token stream mutation → output. Rules can be disabled, version-gated, or run once. The test suite uses a shared `testFormatting` helper that verifies formatting output, lint idempotency, and change tracking. The project also includes an Xcode extension with a preferences UI and an SPM plugin for build-time formatting. External dependencies are minimal (Foundation, XcodeKit for the extension, and git for header metadata). Key entry points include `CommandLineTool/main.swift` (CLI), `EditorExtension/Extension/FormatFileCommand.swift` (Xcode), and `Plugins/SwiftFormatPlugin/SwiftFormatPlugin.swift` (SPM). The library target `SwiftFormat` exposes `format()` and `lint()` APIs for programmatic use. The codebase demonstrates high craftsmanship with a declarative rule DSL, auto-updating declaration tracking, and a comprehensive test infrastructure that catches regressions across real-world Swift projects (vendored as snapshot fixtures).
The LLM identified 5 files worth highlighting for reuse:
Implements glob pattern expansion and matching with support for brace expansion, wildcards, character classes, and recursive `**` patterns. Isolated utility that can be reused in any file-processing tool. Demonstrates careful handling of edge cases like empty patterns and path separators.
A regex-based English singularization utility used by the formatter for naming conventions. Handles irregular plurals and common suffixes. Self-contained and easily extractable for any project needing plural-to-singular conversion.
Defines a protocol and default implementation for extracting associated values from Swift enums using Mirror. Enables generic handling of enum cases with associated data, used in the editor extension's option models. A clever, lightweight pattern for runtime enum inspection.
Provides a tree of declaration types (SimpleDeclaration, TypeDeclaration, ConditionalCompilationDeclaration) that automatically update their token ranges when the formatter mutates the token array. This non-obvious pattern solves the problem of maintaining valid references during iterative formatting and could be reused in any token-based source manipulation tool.
A reusable XCTest helper that formats input Swift code, compares output to expected, verifies lint idempotency (formatting twice produces no changes), and ensures that any formatting change produces a non-empty `Formatter.Change` array. This pattern enforces deterministic, self-consistent formatting and can be adapted for testing any token-based transformer.