Skip to content

Latest commit

Β 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ ImportFormatter

A beautiful and intelligent Swift import formatter that keeps your code organized and consistent.

Swift Platform License

✨ What is ImportFormatter?

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.

🎯 Key Features

  • πŸ”„ 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 @testable imports 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

πŸš€ Installation

Prerequisites

  • macOS 10.15 or later
  • Swift 6.1 or later

Building from Source

git clone https://github.com/annurdien/ImportFormatter.git
cd ImportFormatter
swift build -c release
cp .build/release/importformatter /usr/local/bin/

Using Swift Package Manager

Add ImportFormatter as a dependency in your Package.swift:

dependencies: [
    .package(url: "https://github.com/annurdien/ImportFormatter.git", branch: "main")
]

πŸ“– Usage

Basic Usage

Format a single Swift file:

importformatter MyFile.swift

Format multiple files:

importformatter file1.swift file2.swift file3.swift

Check Mode

Verify if files need formatting without modifying them:

importformatter --check Sources/**/*.swift

Help

Display usage information:

importformatter --help

🎨 Before & After Examples

Before Formatting:

import MyCustomModule
import Foundation
@testable import SomeTestModule
import UIKit
import CoreData
import NetworkExtension

class MyViewController: UIViewController {
    // Your code here
}

After Formatting:

import CoreData
import Foundation
import NetworkExtension
import UIKit
import MyCustomModule
@testable import SomeTestModule

class MyViewController: UIViewController {
    // Your code here
}

πŸ”§ How It Works

ImportFormatter uses Apple's SwiftSyntax library to:

  1. Parse your Swift files into an Abstract Syntax Tree (AST)
  2. Identify import statements and classify them by type
  3. Sort imports according to these rules:
    • System frameworks first (Foundation, UIKit, SwiftUI, etc.)
    • Third-party and custom modules second
    • @testable imports last
    • Alphabetical ordering within each group
  4. Format with proper spacing and preserve existing code structure

Supported System Frameworks

ImportFormatter recognizes these system frameworks and prioritizes them:

  • Foundation
  • UIKit
  • SwiftUI
  • Combine
  • CoreData
  • CoreGraphics
  • QuartzCore
  • AVFoundation
  • CloudKit
  • CoreLocation
  • MapKit
  • WebKit
  • Network
  • os

πŸŽ›οΈ Command Line Options

Option Description
--check Check if files are formatted without modifying them
--help, -h Show help message

πŸ”„ Integration

Xcode Build Phase

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"
fi

Git Pre-commit Hook

Create 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
fi

πŸ› οΈ Development

Building for Development

swift build

Running Tests

swift test

Project Structure

ImportFormatter/
β”œβ”€β”€ Sources/
β”‚   └── ImportFormatter/
β”‚       └── ImportFormatter.swift    # Main formatter logic
β”œβ”€β”€ Tests/
β”‚   └── importformatterTests/
β”‚       └── importformatterTests.swift
β”œβ”€β”€ Package.swift                    # Package manifest
β”œβ”€β”€ LICENSE                         # MIT License
└── README.md                       # This file

🀝 Contributing

We welcome contributions! Here's how you can help:

  1. 🍴 Fork the repository
  2. 🌿 Create a feature branch (git checkout -b feature/amazing-feature)
  3. ✨ Commit your changes (git commit -m 'Add amazing feature')
  4. πŸš€ Push to the branch (git push origin feature/amazing-feature)
  5. πŸ“¬ Open a Pull Request

Contribution Guidelines

  • Write clear, concise commit messages
  • Add tests for new functionality
  • Follow Swift coding conventions
  • Update documentation as needed

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built with SwiftSyntax by Apple
  • Inspired by the need for consistent Swift code formatting
  • Thanks to the Swift community for feedback and suggestions

πŸ“ž Support


Made with ❀️ for the Swift community

About

Import formatter for swift source code

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages