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