iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🧹

Using the Built-in swiftlang/swift-format in Xcode

に公開4件

Summary

Command Line Interface (CLI)

# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
swift format --help
When using xcrun
# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
xcrun --run swift-format --help

# Or use `--find`
$(xcrun --find swift-format) --help

Xcode Menu

Editor > Structure > Format File with 'swift-format' (Ctrl + Shift + I)

What is swiftlang/swift-format?

swiftlang/swift-format (formerly apple/swift-format) is a code formatter and linter that can be used in projects using Swift.

It is located under swiftlang and can be considered the official formatter.

Conventional Installation Methods for swiftlang/swift-format

swiftlang/swift-format could be used as a command-line tool, a Swift Package plugin (command plugin), and more. Examples of using it as a command-line tool are as follows:

Installing as a CLI tool via Swift Package Manager
git clone https://github.com/swiftlang/swift-format
cd swift-format
git checkout refs/tags/600.0.0  # For Swift 6.0
swift package experimental-install --product swift-format
Installing as a CLI tool via Homebrew
brew install swift-format
Installing as a CLI tool via Mint
mint install swift-format

How to Use swiftlang/swift-format as a Command-Line Tool

To display the help, use --help.

# If added to your PATH
swift-format --help

# If not added to your PATH (Swift Package Manager example)
~/.swiftpm/bin/swift-format --help

# If not added to your PATH (Homebrew example)
/opt/homebrew/bin/swift-format --help

# If Mint's path is set
mint run swift-format --help

For detailed usage, refer to the output of --help, the swiftlang/swift-format README, or other explanatory articles.

🎉 swiftlang/swift-format is now included in Xcode's built-in toolchain

Starting from Xcode 16, released to the general public on September 17, 2024 (JST), swiftlang/swift-format is now bundled with the built-in toolchain. As a result, if you want to use it as a command-line tool, you no longer need to install it via Swift Package Manager, Homebrew, or Mint, provided you have Xcode installed.

After installing Xcode 16.0 (16A242d) or later through your preferred method (e.g., App Store), verify that version 16.0 (16A242d) or later is active using xcodebuild.

xcodebuild -version
# Xcode 16.0
# Build version 16A242d

By using the --find option of xcrun, you can output the path of the swiftlang/swift-format included in the built-in toolchain.

xcrun --find swift-format
# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-format

Using the swiftlang/swift-format in Xcode as a Command-Line Tool

By using swift format, you can execute the swiftlang/swift-format from Xcode's built-in toolchain.

# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
swift format --help

When using xcrun

By using the --run option of xcrun, you can execute the swiftlang/swift-format from Xcode's built-in toolchain.

# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
xcrun --run swift-format --help

# Or use `--find`
$(xcrun --find swift-format) --help

In scripts where you previously executed swift-format, you can switch to using the version in Xcode's built-in toolchain by replacing it with swift format.

Using swiftlang/swift-format from the Xcode Menu

To perform formatting using swiftlang/swift-format while editing files in the Xcode editor, click "Editor" > "Structure" > "Format File with 'swift-format'" each time. Alternatively, you can execute it anytime using Ctrl + Shift + I.


Editor > Structure > Format File with 'swift-format' (Ctrl + Shift + I)

Discussion

monomono

xcrun の --run を用いることで、Xcode の内蔵ツールチェーンの swiftlang/swift-format を実行できます。

swift formatで済ませるのが短くて良いと思います。

You can run swift-format from anywhere on the system using swift format
https://github.com/swiftlang/swift-format

1
treastrain / Tanaka Ryogatreastrain / Tanaka Ryoga

ご指摘ありがとうございます!

初稿での xcrun を使う方法についてはアコーディオンに閉じ、swift format を使う方法へと更新させていただきました。

1
Muukii - Kimura HiroshiMuukii - Kimura Hiroshi

.swiftformatでしたっけ、そういう設定ファイルを読んでくれるのか気になってました。
調べてみよう

Muukii - Kimura HiroshiMuukii - Kimura Hiroshi

読んでくれましたわ
.swift-format

例えばこんな設定で

{
  "maximumBlankLines" : 1,
  "indentConditionalCompilationBlocks" : false,
  "lineLength" : 100,
  "rules" : {
    "DoNotUseSemicolons" : false
  },
  "lineBreakBeforeEachGenericRequirement" : true,
  "lineBreakBeforeEachArgument" : true,
  "indentation" : {
    "spaces" : 2
  }
}
1