A beautiful and intelligent Swift import formatter that keeps your code organized and consistent.
ImportFormatter is a command-line tool that automatically organizes and sorts your Swift import statements according to best practices. It intelligently groups system frameworks, third-party libraries, and testable imports while maintaining proper spacing and formatting.
- π Smart Sorting: Automatically sorts imports alphabetically with system frameworks first
- π± System Framework Recognition: Identifies and prioritizes Apple's system frameworks
- π§ͺ Testable Import Handling: Properly groups
@testableimports separately - β‘ Fast Processing: Built with Swift Syntax for lightning-fast parsing
- π Check Mode: Verify formatting without modifying files
- π Batch Processing: Format multiple files at once
- π¨ Clean Output: Maintains proper spacing and code structure
- macOS 10.15 or later
- Swift 6.1 or later
git clone https://github.com/annurdien/ImportFormatter.git
cd ImportFormatter
swift build -c release
cp .build/release/importformatter /usr/local/bin/Add ImportFormatter as a dependency in your Package.swift:
dependencies: [
.package(url: "https://github.com/annurdien/ImportFormatter.git", branch: "main")
]Format a single Swift file:
importformatter MyFile.swiftFormat multiple files:
importformatter file1.swift file2.swift file3.swiftVerify if files need formatting without modifying them:
importformatter --check Sources/**/*.swiftDisplay usage information:
importformatter --helpimport MyCustomModule
import Foundation
@testable import SomeTestModule
import UIKit
import CoreData
import NetworkExtension
class MyViewController: UIViewController {
// Your code here
}import CoreData
import Foundation
import NetworkExtension
import UIKit
import MyCustomModule
@testable import SomeTestModule
class MyViewController: UIViewController {
// Your code here
}ImportFormatter uses Apple's SwiftSyntax library to:
- Parse your Swift files into an Abstract Syntax Tree (AST)
- Identify import statements and classify them by type
- Sort imports according to these rules:
- System frameworks first (Foundation, UIKit, SwiftUI, etc.)
- Third-party and custom modules second
@testableimports last- Alphabetical ordering within each group
- Format with proper spacing and preserve existing code structure
ImportFormatter recognizes these system frameworks and prioritizes them:
- Foundation
- UIKit
- SwiftUI
- Combine
- CoreData
- CoreGraphics
- QuartzCore
- AVFoundation
- CloudKit
- CoreLocation
- MapKit
- WebKit
- Network
- os
| Option | Description |
|---|---|
--check |
Check if files are formatted without modifying them |
--help, -h |
Show help message |
Add a new "Run Script Phase" to your Xcode project:
if which importformatter >/dev/null; then
find "${SRCROOT}" -name "*.swift" -not -path "*/.*" | xargs importformatter
else
echo "warning: ImportFormatter not installed"
fiCreate a pre-commit hook to automatically format imports:
#!/bin/sh
git diff --cached --name-only --diff-filter=ACM | grep '\.swift$' | xargs importformatter --check
if [ $? -ne 0 ]; then
echo "Import formatting issues found. Run 'importformatter' on your Swift files."
exit 1
fiswift buildswift testImportFormatter/
βββ Sources/
β βββ ImportFormatter/
β βββ ImportFormatter.swift # Main formatter logic
βββ Tests/
β βββ importformatterTests/
β βββ importformatterTests.swift
βββ Package.swift # Package manifest
βββ LICENSE # MIT License
βββ README.md # This file
We welcome contributions! Here's how you can help:
- π΄ Fork the repository
- πΏ Create a feature branch (
git checkout -b feature/amazing-feature) - β¨ Commit your changes (
git commit -m 'Add amazing feature') - π Push to the branch (
git push origin feature/amazing-feature) - π¬ Open a Pull Request
- Write clear, concise commit messages
- Add tests for new functionality
- Follow Swift coding conventions
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with SwiftSyntax by Apple
- Inspired by the need for consistent Swift code formatting
- Thanks to the Swift community for feedback and suggestions
- π Report Issues
- π‘ Request Features
- π§ Contact