自动格式化代码

由于Xcode9之后不能使用插件,因此无法在Xcode中自动格式化代码,但是可以使用Space Commander在终端对代码进行格式化

Space Commander

Space Commander原理

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

安装

1.进入本地的文档目录:

cd ~/Documents

2.从github上clone项目

git clone https://github.com/square/spacecommander.git

3.进入sapcecommander目录

cd ~/Documents/spacecommander

4.修改默认的.clang-format文件

open .clang-format

复制下面内容覆盖原来的.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

5.在~/.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"

保存,重新打开终端或者使用srouce加载

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

cd ~/Desktop/Demo

初始化项目

clangformatsetup

这时使用commit会提示哪个文件不符合约定的代码规范

√ chenjiangchuan ~/Desktop/Demo$ git commit                                                                                                    ✚master ‹system›
🚸 Format and stage individual files:
"/Volumes/hhd/download/SourceCode/spacecommander"/format-objc-file.sh 'Demo/Login/Controller/HoriLoginViewController.m' && git add 'Demo/Login/Controller/HoriLoginViewController.m';

🚀  Format and stage all affected files:
     "/Volumes/hhd/download/SourceCode/spacecommander"/format-objc-files.sh -s

🔴  There were formatting issues with this commit, run the👆 above👆 command to fix.
💔  Commit anyway and skip this check by running git commit --no-verify

根据上面的提示HoriLoginViewController.m文件中的代码不符合约定的格式,这时我们对应的命令进行自动格式化

clangformatfile Demo/Login/Controller/HoriLoginViewController.m

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

clangformatallfiles

results matching ""

    No results matching ""