Clang-Format的使用及其自定义格式
之前想使用ClangFormat插件来定义Xcode中的编码格式,然后上网搜资料,安装完之后自定义的格式一直出问题,总是报错,然后好人有好报,我在网上遇到了一个大神给我解决了。然后我就将我自己对ClangFormat的使用做了一个总结:
1、 在GitHub中下载ClangFormat,下载地址:https://github.com/travisjeffery/ClangFormat-Xcode
2、 安装:打开下载的ClangFormat,在Xcode中编译、运行,然后重启Xcode
3、 使用方式:点击Edit->ClangFormat->选择自己想要的格式
(选择“Format Selected files”,可以格式化多个文件;
选择“Format File in Focus”,可以格式化当前文件;
选择“Format Selected Text”,可以对该段文本格式化;
还可以设置这些功能的快捷键,具体的我就不多说的,网上一搜一大把,主要说的是自定义格式的时候我遇到的问题
4、在工程目录或者workspace目录下创建一个".clang-format"文件,添加类似于以下内容的参数:
- BasedOnStyle: LLVM
- IndentWidth: 4
- MaxEmptyLinesToKeep: 4
- BreakBeforeBraces: Attach
- AllowShortIfStatementsOnASingleLine: true
- IndentCaseLabels: true
- ObjCBlockIndentWidth: 4
- ObjCSpaceAfterProperty: true
- ColumnLimit: 0
- AlignTrailingComments: true
- SpaceAfterCStyleCast: true
- SpacesInParentheses: false
- SpacesInSquareBrackets: false
5、最后在Xcode的“Edit”->“Clang Format”中选中“File”,并让倒数第二行显示“Disable Format On Save”。这样可以自定义编码格式。