安装 clang-format
在 LLVM Download Page 中下载 Windows 版本并安装
需要注意选择 Add LLVM to the system PATH for current user
或 Add LLVM to the system PATH for all users
其中的一项
安装完成后,在命令行中测试:
clang-format -version
- 打开 Tools->
Custom Commands
- 点击
Add...
按钮,具体内容为:Command
值为clang-format for current file
Run
值为clang-format --style=file -i %f
Dir
留空(即项目目录,这个目录里需要有.clang-format
文件)- 勾选
Save Files First
- 不要勾选
Pause When Done
- 勾选
Wait Until Done
- 点击
Menu...
按钮,在合适的菜单中加入刚刚定义的命令(可选),例如在tools中,insert ,然后就可以在tools菜单中看到clang-format选项,点击后会格式化当前源文件 - 点击
Keys...
按钮,为刚刚定义的命令分配合适的快捷键(可选)
linux 下使用方法为:clang-format -i xxx.cpp
左括号的配置参考:https://clang.llvm.org/docs/ClangFormatStyleOptions.html BreakBeforeBraces
#基于那个配置文件
BasedOnStyle: Chromium
#指针的*的挨着哪边
#PointerAlignment: Right
#缩进宽度
IndentWidth: 4
# 连续的空行保留几行
MaxEmptyLinesToKeep: 1
# 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly).
#ObjCSpaceAfterProperty: true
# OC block后面的缩进
#ObjCBlockIndentWidth: 4
# 是否允许短方法单行
#AllowShortFunctionsOnASingleLine: true
# 是否允许短if单行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: true
#注释对齐
AlignTrailingComments: true
# 换行的时候对齐操作符
#AlignOperands: true
# 中括号两边空格 []
#SpacesInSquareBrackets: true
# 小括号两边添加空格
SpacesInParentheses : false
#多行声明语句按照=对齐
#AlignConsecutiveDeclarations: true
#连续的赋值语句以 = 为中心对齐
AlignConsecutiveAssignments: true
#等号两边的空格
SpaceBeforeAssignmentOperators: true
# 容器类的空格 例如 OC的字典
#SpacesInContainerLiterals: true
#缩进
#IndentWrappedFunctionNames: true
BreakConstructorInitializersBeforeComma: false
AlwaysBreakBeforeMultilineStrings: false
#BAS_AlwaysBreak : false
#BreakBeforeBraces: Allman
BreakBeforeBraces: Allman
BreakConstructorInitializersBeforeComma: false
AlwaysBreakBeforeMultilineStrings: false
AlignAfterOpenBracket: Align
#在block从空行开始
#KeepEmptyLinesAtTheStartOfBlocks: true
#在构造函数初始化时按逗号断行,并以冒号对齐
#BreakConstructorInitializersBeforeComma: true
#函数参数换行
AllowAllParametersOfDeclarationOnNextLine: true
#括号后添加空格
#SpaceAfterCStyleCast: true
#tab键盘的宽度
TabWidth: 4
UseTab: Never
AllowShortFunctionsOnASingleLine: None
ColumnLimit: 200
转自
https://biggates.github.io/2018/05/22/git-clang-format-integration.html