# swiftlang/swift-format architecture

> Architecture diagram of the GitHub repository swiftlang/swift-format, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swiftlang/swift-format
- Repository: https://github.com/swiftlang/swift-format

## Overview

##### Main Components
- Command-Line Interface (CLI): This is the entry point for users invoking swift-format from the shell. It manages the subcommands (“format”, “lint”, “dump-configuration”, etc.) and the basic parsing of command-line options.
- API Layer: Provided via Swift Package Manager, exposing modules like SwiftFormatter and SwiftLinter so that other tools can import and use swift-format’s capabilities programmatically.
- Formatting Engine Components:
– Core: Contains the main formatting logic (e.g., FormatPipeline, Rule implementations, SyntaxFormatRule, etc.).
– Pretty Printing: Handles output formatting, indentation, whitespace, etc. from its submodule “PrettyPrint”.
– Rules Engine: Under the “Rules” directory are many individual style rules (each a Swift file) that check for various Swift code style guidelines.
- Linting Components: Similar to formatting but focused on diagnostic generation (SwiftLinter, lint pipelines, and diagnostic utilities).
- Plugin Support: The Plugins folder (FormatPlugin and LintPlugin) suggests that there are extension points to add or override behaviors.
- Build & Test Infrastructure: Directories and files like CMakeLists.txt, the Sources/generate-swift-format (for rule documentation generation), and a comprehensive set of test suites (under Tests/) indicate a well-defined build pipeline and automated test harness.
- External Dependencies: The project is built on top of SwiftSyntax (for parsing Swift code) and leverages Swift’s Package Manager for dependency management.

##### Component Relationships and Interactions
- The CLI component is the user-facing layer – it accepts inputs, loads configuration (from JSON files or defaults via “dump-configuration”), and then passes off file content or file URLs to the API layer.
- The API layer (via SwiftFormatter and SwiftLinter) internally calls into the formatting engine. The engine’s core works on an abstract Syntax node (provided by SwiftSyntax) and then applies the rule-based transformation pipeline.
- Both the formatting and linting pipelines interact with the PrettyPrint module to generate formatted output and/or diagnostics.
- The rules in the “Rules” directory serve as independent modules plugged into the core pipeline, and they are aggregated using generated files such as RuleRegistry+Generated.swift.
- The Plugins may modify or extend behavior – they could be seen as “middleware” between the CLI/API inputs and the actual formatting/linting engines.
- Configuration management (via JSON configuration files) is loaded early in the process and influences which rules or formatting styles are applied.
- Testing bundles (located in the Tests directory) simulate various inputs to the CLI and API to ensure alignment with expected output.

##### Architectural Patterns and Design Principles Revealed
- Modular Design: The separation into API, Core, PrettyPrint, Rules, and Utilities suggests adherence to modular design practices.
- Plugin Extensibility: The Plugins directory supports the idea of abstraction and extension, where external formatting/linting modules can be integrated.
- Pipeline Architecture: Both the formatting and linting processes follow a pipeline-based approach that processes a SwiftSyntax tree through a series of rule-based transformations.
- Build Separation: There is a clear distinction between the build system (CMake, Swift Package Manager integration) and the core logic, allowing portability and external integrations.

a. User Interaction Layer
– CLI and API: show these as distinct input channels.
b. Configuration Loader
c. Formatting and Linting Engine
– Show a “Formatting Pipeline” block that includes “Core Processing,” “Rules Engine,” and “Pretty Printing.”
– Within the Rules Engine, consider sub-blocks for individual rule sets (e.g., naming conventions, whitespace rules, etc.).
d. Plugin Management
– Have a side module labeled “Plugin Support” that feeds into the engine.
e. Testing and Build Infrastructure
– Optionally, represent the build, testing, and CI/CD processes that tie into this system.
f. External Dependencies
– Show integration with SwiftSyntax and potential linking to SourceKit-LSP.

- Flows from Plugins to the Formatting Engine (as modification points).

- Clear labels for components (e.g., “CLI Interface”, “SwiftFormatter Module”, “Rule Engine”, “Pretty Print Module”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interaction Layer"
        CLI_Interface["Command-Line Interface (CLI)"]:::user
        API_Layer["API Layer"]:::user
    end

    ConfigLoader["Configuration Loader"]:::config

    CLI_Interface -->|"loads_configuration"| ConfigLoader
    API_Layer -->|"invokes_API"| ConfigLoader

    subgraph "Formatting and Linting Engine"
        subgraph "Formatting Pipeline"
            CoreProcessing["Core Processing"]:::engine
            RulesEngine["Rules Engine"]:::engine
            PrettyPrint["Pretty Print Module"]:::engine
            CoreProcessing -->|"applies_rules"| RulesEngine
            RulesEngine -->|"formats_output"| PrettyPrint
        end
        subgraph "Linting Pipeline"
            SwiftLinter["Swift Linter"]:::engine
            LintPipeline["Lint Pipeline"]:::engine
            SwiftLinter -->|"generates_diagnostics"| LintPipeline
        end
    end

    ConfigLoader -->|"provides_settings"| CoreProcessing
    API_Layer -->|"calls_formatting"| CoreProcessing

    PluginSupport["Plugin Support"]:::plugin
    PluginSupport -->|"extends_engine"| CoreProcessing

    BuildInfra["Build & Test Infrastructure"]:::infra
    BuildInfra -->|"builds_and_tests"| CoreProcessing

    ExternalDeps["External Dependencies"]:::ext
    ExternalDeps -->|"supports_engine"| CoreProcessing

    PrettyPrint -->|"outputs"| Output["Formatted Code / Diagnostics"]
    LintPipeline -->|"outputs"| Output

    %% Click Events
    click CLI_Interface "https://github.com/swiftlang/swift-format/tree/main/Sources/swift-format/Frontend"
    click CLI_Interface "https://github.com/swiftlang/swift-format/tree/main/Sources/swift-format/Subcommands"
    click API_Layer "https://github.com/swiftlang/swift-format/tree/main/Sources/SwiftFormat/API"
    click ConfigLoader "https://github.com/swiftlang/swift-format/blob/main/Sources/swift-format/Frontend/ConfigurationLoader.swift"
    click CoreProcessing "https://github.com/swiftlang/swift-format/tree/main/Sources/SwiftFormat/Core"
    click PrettyPrint "https://github.com/swiftlang/swift-format/tree/main/Sources/SwiftFormat/PrettyPrint"
    click RulesEngine "https://github.com/swiftlang/swift-format/tree/main/Sources/SwiftFormat/Rules"
    click SwiftLinter "https://github.com/swiftlang/swift-format/blob/main/Sources/SwiftFormat/API/SwiftLinter.swift"
    click LintPipeline "https://github.com/swiftlang/swift-format/blob/main/Sources/SwiftFormat/Core/LintPipeline.swift"
    click PluginSupport "https://github.com/swiftlang/swift-format/tree/main/Plugins/FormatPlugin"
    click PluginSupport "https://github.com/swiftlang/swift-format/tree/main/Plugins/LintPlugin"
    click BuildInfra "https://github.com/swiftlang/swift-format/blob/main/CMakeLists.txt"
    click BuildInfra "https://github.com/swiftlang/swift-format/blob/main/Sources/CMakeLists.txt"
    click BuildInfra "https://github.com/swiftlang/swift-format/tree/main/Tests/"

    %% Styles
    classDef user fill:#f9e79f,stroke:#d35400,stroke-width:2px;
    classDef config fill:#fad7a0,stroke:#e67e22,stroke-width:2px;
    classDef engine fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef plugin fill:#d7bde2,stroke:#8e44ad,stroke-width:2px;
    classDef infra fill:#d5f5e3,stroke:#1e8449,stroke-width:2px;
    classDef ext fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
```
