Kotlin and Swift Code Formatter

Format .kt, .kts and .swift files side by side in one place, with ktlint and swift-format presets, trailing commas, sorted imports and a diff for each file.

Berjalan dalam pelayar anda. Fail anda tidak pernah meninggalkan peranti. Lihat cara menyemaknya

…

Cara melakukannya

  1. 1
    Open files

    Add .kt, .kts and .swift files or use the two samples.

  2. 2
    Set Kotlin options

    Choose ktlint_official, intellij_idea or android_studio.

  3. 3
    Set Swift options

    Choose swift-format or a four-space Xcode style.

  4. 4
    Compare

    Read each file's diff and issue list.

  5. 5
    Apply

    Apply the formatting to the current file or all files.

  6. 6
    Download

    Save one file, a ZIP of all files, or a .patch.

Tentang alat ini

Kotlin and Swift look alike on the page: both drop semicolons, write types after a colon, use trailing lambdas or closures and support string templates. Their style tools differ, though. ktlint applies the Kotlin coding conventions, and its ktlint_official style adds trailing commas wherever a list is split over several lines and orders imports with java, javax and kotlin last and aliases at the very end. swift-format, the formatter maintained by the Swift project, defaults to two-space indentation, sorts imports into regular, declaration and @testable groups, and adds trailing commas to multi-line array and dictionary literals. This tool formats both languages in one workspace and picks the language from each file's extension. Its tokenizers understand Kotlin templates with ${...} holes that contain more strings, raw triple-quoted strings, backtick names and nested block comments, and Swift interpolation with \( ), extended raw strings like #"..."#, multi-line strings and #if compiler blocks. On top of that the formatter re-indents blocks and continuation lines such as .map chains, spaces colons the way each language expects (class A : B in Kotlin declarations, class A: B in Swift), keeps ranges like 1..5 and 0..<n tight, and recognises String? and value!! as type and operator suffixes rather than ternaries.

What else is checked

Every file gets a bracket balance check with line numbers. Kotlin wildcard imports are reported because ktlint's no-wildcard-imports rule rejects them. In Swift, force unwraps with ! are counted, as swift-format's NeverForceUnwrap rule does, and #if blocks without a matching #endif are reported. Semicolons at the end of a statement are removed in both languages, except where Kotlin needs one after enum entries that are followed by members.

Worked example

The Kotlin sample UserRepository.kt has 25 lines. With ktlint_official it becomes 27: 14 lines change, 3 are added and 1 is removed. The imports come out as com.example.model.User, kotlinx.coroutines.flow.Flow, java.time.Instant, kotlin.math.max and finally the alias com.example.net.Api as RemoteApi, with blank lines after the package line and the imports. The semicolon after cache[id] goes, "id:String" becomes "id: String", the when branches read 0 -> "none", and a trailing comma is added after "limit: Int" in the multi-line parameter list. The Swift sample Cart.swift grows from 22 to 24 lines: Foundation and UIKit are sorted, @testable import Shop gets its own group, indentation drops to two spaces, the semicolon after the tax constant is removed and the regions dictionary gains a comma after its last entry.

Limits

The engines keep your line breaks; they do not wrap long expressions or rewrite code, and Kotlin unused imports are not removed because operator and delegate imports are hard to judge without a compiler. No official formatter can be loaded for these languages, because ktlint runs on the JVM and swift-format needs the Swift toolchain.

Contoh berselesaian

UserRepository.kt with ktlint_official: 25 lines become 27, trailing comma after limit: Int, alias import last; Cart.swift gains a trailing comma.

Ringkasnya

Soalan lazim

Which Kotlin styles are included?

ktlint_official, intellij_idea and android_studio. They differ in trailing commas, import order and the column limit used for the long-line report.

Does it add trailing commas?

Yes, when set to Add. Kotlin gets them in multi-line parameter, argument and collection lists; Swift only in multi-line array and dictionary literals, which is where swift-format adds them.

How are imports ordered?

Kotlin uses ktlint's layout: all other imports, then java, javax and kotlin, then aliases. Swift sorts regular imports, then declaration imports such as import struct, then @testable imports.

Can I format Kotlin and Swift files together?

Yes. Open both kinds of file; each one is formatted with the options of its own language, and the ZIP download contains them all.

Are string templates and interpolations safe?

Yes. Kotlin ${...} holes and Swift \( ) interpolations are read as part of the string, including nested strings, so nothing inside them changes.

Does it need a network connection?

No. Both formatters are built into the page and work offline once it has loaded.

Percuma sepenuhnyaTiada bayaran, tiada pendaftaran
Fail kekal peribadiSemuanya berjalan dalam pelayar anda
Mudah dalam bahasa andaTersedia dalam banyak bahasa