OC自動格式化

git commit時檢查項目中的代碼格式;
如果代碼不符合約定的格式,使用提供的腳本進(jìn)行格式化;
git pull,如果試圖將不符合約定的代碼拉取下來,會失敗。

cd ~/Documents
git clone https://github.com/square/spacecommander.git
cd ~/Documents/spacecommander
open .clang-format

#基于那個配置文件
BasedOnStyle: LLVM

#指針的*的挨著哪邊
PointerAlignment: Right

#縮進(jìn)寬度
IndentWidth: 4

# 連續(xù)的空行保留幾行
MaxEmptyLinesToKeep: 1

# 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly).
ObjCSpaceAfterProperty: true

# OC block后面的縮進(jìn)
ObjCBlockIndentWidth: 4

ObjCSpaceBeforeProtocolList: true

# 是否允許短方法單行
AllowShortFunctionsOnASingleLine: true

# 是否允許短if單行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: true

#注釋對齊
AlignTrailingComments: true

# 換行的時候?qū)R操作符
#AlignOperands: true

# 中括號兩邊空格 [] 
SpacesInSquareBrackets: false

# 小括號兩邊添加空格
SpacesInParentheses : false

#多行聲明語句按照=對齊
AlignConsecutiveDeclarations: true

#連續(xù)的賦值語句以 = 為中心對齊
AlignConsecutiveAssignments: true

#等號兩邊的空格
SpaceBeforeAssignmentOperators: true

# 容器類的空格 例如 OC的字典
SpacesInContainerLiterals: true

#縮進(jìn)
IndentWrappedFunctionNames: true

#在block從空行開始
KeepEmptyLinesAtTheStartOfBlocks: false

#在構(gòu)造函數(shù)初始化時按逗號斷行,并以冒號對齊
BreakConstructorInitializersBeforeComma: true

#函數(shù)參數(shù)換行
AllowAllParametersOfDeclarationOnNextLine: false
BinPackParameters: false

#括號后添加空格
SpaceAfterCStyleCast: true

# 每行字符的長度
ColumnLimit: 80

#tab鍵盤的寬度
TabWidth: 4
UseTab: Never

AlignAfterOpenBracket: Align

AlignEscapedNewlinesLeft: true

AllowShortCaseLabelsOnASingleLine: false

BinPackArguments: false

BreakBeforeBinaryOperators: None

BreakBeforeTernaryOperators: false

IndentCaseLabels: true

/.bash_profile或者/.zshrc中添加格式化命令的別名

# 初始化
alias clangformatsetup="~/Documents/spacecommander/setup-repo.sh"
# 格式化對應(yīng)文件
alias clangformatfile="~/Documents/spacecommander/format-objc-file.sh"
# 格式化所有暫存文件
alias clangformatallfiles="~/Documents/spacecommander/format-objc-files.sh -s"
# 格式化整個倉庫
alias clangformatrepo="~/Documents/spacecommander/format-objc-files-in-repo.sh"

進(jìn)入需要代碼格式化的項目,這里以桌面上的Demo為例

cd ~/Desktop/Demo

clangformatsetup

或者使用下面命名對所有暫存的文件進(jìn)行格式化

clangformatallfiles
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容