Sort import statements in your Swift source code.
Add sort-swift-imports package dependency to your Swift Package:
.package(
url: "https://github.com/darrarski/sort-swift-imports.git",
from: "1.0.0"
),Add SortSwiftImports library as a dependency to your targets:
.product(
name: "SortSwiftImports",
package: "sort-swift-imports"
),Use SortSwiftImports from your code:
import SortSwiftImports
let sortSwiftImports: SortSwiftImports = .live
let code: String = // swift source code
let result = sortSwiftImports(in: code)
switch result {
case let .success(output):
print(output)
case let .failure(error):
print(error)
}
Native SwiftUI macOS application that you can use to sort imports in your Swift source code. Just paste your code and click the Sort swift imports button.
It also contains Xcode Source Editor extensionso that you can sort your Swift imports directly from Xcode. Ensure you have enabled Sort Imports extension in System Preferences → Extensions → Xcode Source Editor.
| macOS |
|---|
![]() ![]() |
Native SwiftUI iOS application that you can use to sort imports in your Swift source code. Just paste your code and tap ⚙️ → Sort swift imports.
On your iPad, you can use multiple windows of the app simultaneously.
| iPhone | iPad |
|---|---|
![]() ![]() |
![]() ![]() |
Open SortSwiftImports.xcworkspace in Xcode (⩾15.4) for development.
SortSwiftImports (Xcode Workspace)
├─ sort-swift-imports (Swift Package)
│ └─ SortSwiftImports (Library)
├─ sort-swift-imports-app (Swift Package)
│ ├─ AppFeature (Library)
│ └─ EditorFeature (Library)
├─ sort-swift-imports-app-icon (Swift Package)
│ ├─ AppIcon (Library)
│ └─ AppIconExport (macOS executable)
└─ Project (Xcode Project)
├─ SortSwiftImportsApp (iOS and macOS Application)
└─ SortSwiftImportsXcodeExtension (Xcode Source Editor Extension)
- Use
SortSwiftImportsAppscheme to build, run, and test the application on iOS and macOS. - Use
SortSwiftImportsXcodeExtensionscheme to build and run the Xcode Source Editor Extension. - Use
AppIconscheme for building & previewing the app icon in Xcode (use macOS destination). - Use
ExportAppIconscheme for exporting the app icon into asset catalog (use macOS destination).
Use other schemes for building and testing individual feature-libraries in isolation.
- All contributions are welcome!
- Create a pull request containing your changes or bug fixes.
- Make sure to add tests for the new/updated code.
Consider supporting further development and buy me a coffee.
Copyright © 2021 Dariusz Rybicki Darrarski
License: MIT





