Scripts2026年7月6日·1 分钟阅读

SwiftFormat — Opinionated Swift Code Formatter

A command-line tool and Xcode extension for automatically formatting Swift code according to configurable style rules.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
SwiftFormat Overview
直接安装命令
npx -y tokrepo@latest install ef0019e0-7959-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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 <rule> and // swiftformat:enable <rule> 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产