OC自动格式化

2023-04-09  本文已影响0人  菲特峰

git commit时检查项目中的代码格式;
如果代码不符合约定的格式,使用提供的脚本进行格式化;
git pull,如果试图将不符合约定的代码拉取下来,会失败。

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

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

#指针的*的挨着哪边
PointerAlignment: Right

#缩进宽度
IndentWidth: 4

# 连续的空行保留几行
MaxEmptyLinesToKeep: 1

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

# OC block后面的缩进
ObjCBlockIndentWidth: 4

ObjCSpaceBeforeProtocolList: true

# 是否允许短方法单行
AllowShortFunctionsOnASingleLine: true

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

#注释对齐
AlignTrailingComments: true

# 换行的时候对齐操作符
#AlignOperands: true

# 中括号两边空格 [] 
SpacesInSquareBrackets: false

# 小括号两边添加空格
SpacesInParentheses : false

#多行声明语句按照=对齐
AlignConsecutiveDeclarations: true

#连续的赋值语句以 = 为中心对齐
AlignConsecutiveAssignments: true

#等号两边的空格
SpaceBeforeAssignmentOperators: true

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

#缩进
IndentWrappedFunctionNames: true

#在block从空行开始
KeepEmptyLinesAtTheStartOfBlocks: false

#在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: true

#函数参数换行
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"
# 格式化对应文件
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"

进入需要代码格式化的项目,这里以桌面上的Demo为例

cd ~/Desktop/Demo

clangformatsetup

或者使用下面命名对所有暂存的文件进行格式化

clangformatallfiles
上一篇 下一篇

猜你喜欢

热点阅读