简要说明: 本文主要描述了clang-format应用说明,基于Vs Code测试。


设置Visual Code中应用

在工程根目录下新建.clang-format文件,内容如下:

# 表示注释,但貌似不支持中文

# 基于模板样式 LLVM,Google,Visual Studio,Mozillat,WebKit等
BasedOnStyle: LLVM

# 缩进字节数
IndentWidth: 2

# case标签是否缩进
IndentCaseLabels: true 

# Never表示空格代替制表符
UseTab: Never
# 制表符宽度
TabWidth: 2

# 每行限制字节数,超出此值后换行,0表示不限制
ColumnLimit: 0

# 最大的连续空行数
MaxEmptyLinesToKeep: 2

# 指针的*的挨着哪边(区分大小写)
PointerAlignment: Right

# 中括号两边添加空格 [] 
SpacesInSquareBrackets: false
# 小括号两边添加空格 ()
SpacesInParentheses : false
# 等号两边的空格
SpaceBeforeAssignmentOperators: true

# 注释对齐
AlignTrailingComments: true
# 多行声明语句对齐(不选)
AlignConsecutiveDeclarations: false
# 连续的赋值语句以 = 为中心对齐
AlignConsecutiveAssignments: true

大括号换行模式

BreakBeforeBraces: Allman

Attach  总是将大括号与上下文连在一起。
Linux   像Attach一样, 但是在一个方法、命名空间或一个类定义的大括号之前换行
Mozilla 像Attach一样, 但是在一个枚举、方法或记录定义前换行。
Stroustrup  像Attach一样,但是在方法定义、catch、和else前换行
WebKit  像Attach一样, 但是在方法前换行。
Allman  总是在大括号之前换行。
GNU     总是在括号前中断,并添加一个额外的级别的缩进到控件语句的括号中,而不是类、函数或其他定义的括号中。
Custom  在“BraceWrapping”里配置每一个单独的大括号。
BreakBeforeBraces: Custom
BraceWrapping: 
  # 注意这里的缩进,否则vscode格式化时报错  
  AfterClass:      false
  AfterFunction:   true           # 函数换行(一般为true)
  AfterControlStatement: true     # 控制块(if-else/while/switch等)
  AfterStruct:     true           # 结构体(不包括typedef)
  AfterUnion:      false          # 联合体
  AfterEnum:       true           # 枚举
  AfterNamespace:  false          # 命名空间
  AfterObjCDeclaration: false      
  BeforeCatch:     false          # catch与前边的花括号是否换行
  BeforeElse:      true           # else与前边的花括号是否换行
  IndentBraces:    false          # 缩进换行的大括号(一般为false)
# 结构体数组赋值时用的到,具体描述待查
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false

比较好看的配置:

BasedOnStyle: LLVM
IndentWidth: 2
IndentCaseLabels: true 
UseTab: Never
TabWidth: 2
ColumnLimit: 0
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
SpacesInSquareBrackets: false
SpacesInParentheses : false
SpaceBeforeAssignmentOperators: true
AlignTrailingComments: true
AlignConsecutiveDeclarations: false
AlignConsecutiveAssignments:  true

BreakBeforeBraces: Custom
BraceWrapping:   
  AfterClass:      false
  AfterFunction:   true
  AfterControlStatement: true
  AfterStruct:     true
  AfterEnum:       true
  AfterNamespace:  false
  AfterObjCDeclaration: false      
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      true
  IndentBraces:    false

参考资料:
https://www.cnblogs.com/PaulpauL/p/5929753.html
https://www.jianshu.com/p/346f439d230c
https://www.jianshu.com/p/c2dd26fe6f78
https://blog.csdn.net/u013187057/article/details/85273775