swift項(xiàng)目用SwiftFormat自動(dòng)格式代碼

1. 安裝 swiftformat
brew install swiftformat
2. 安裝 swiftformat Xcode插件
brew install --cask swiftformat-for-xcode
3. 打開(kāi)swiftformat

在程序塢中直接打開(kāi)

open "/Applications/SwiftFormat For Xcode.app"

會(huì)看到 About, 介紹了 How do I install it?

4. 開(kāi)啟權(quán)限

設(shè)置->隱私與安全性->擴(kuò)展
選擇Xcode Source Editor,勾選SwiftFormat

5. 重啟Xcode

點(diǎn)擊 頂部 Editor目錄可以看到 SwiftFormat, 可以手動(dòng)格式化單個(gè)文件或選中內(nèi)容

6. 配置快捷鍵

參考 http://www.lxweimin.com/p/69883f2a16e1

7. 格式化代碼

在項(xiàng)目目錄批量格式化可以執(zhí)行

swiftformat .

單個(gè)文件

swiftformat path/file.swift
8. 自動(dòng)格式化代碼

8.1 創(chuàng)建鉤子目錄

mkdir -p .git/hooks

8.2 創(chuàng)建 pre-commit 鉤子腳本

cd .git/hooks
touch pre-commit

8.3 編輯 pre-commit 腳本

#!/bin/bash

# Get the staged Swift files
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.swift$')

# Check if any Swift files are staged
if [[ -n $files ]]; then
  # Format the staged Swift files using swiftformat
  swiftformat $files --swiftversion 5

  # Add the formatted files to the staging area
  git add $files
fi

8.4 賦予執(zhí)行權(quán)限

chmod +x pre-commit

完成以上步驟,提交代碼的時(shí)候會(huì)自動(dòng)format

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容