分享下自己习惯的.clang-format配置

都配上了注释, 根据自己的喜好来设置自己的clang-format文件吧。让你的C/C++看起来更美观!再也不用烦恼因格式化而导致的代码冲突啦~~

---
# 语言: None, Cpp, C, CSharp, Java, JavaScript, TypeScript, ObjC, ObjCpp, Proto, TableGen, TextProto, OpenCL, Kotlin, Python等
# 风格: Google, Chromium, Mozilla, WebKit, LLVM, Mozilla-Intermediate, WebKit-Intermediate, LLVM-Intermediate, Google-LLVM, Google-LLVM-Intermediate, Chromium-LLVM, Chromium-LLVM-Intermediate, Mozilla-LLVM, Mozilla-LLVM-Intermediate, WebKit-LLVM, WebKit-LLVM-Intermediate, Custom等
# 缩进: Spaces, Tabs, Custom等
# 注释风格: None, Doxygen, Qt, Preprocessor, JavaDoc, JavaScriptDoc, PythonDoc等
# 空格: Before, After, Around等
# 换行: Linux, Windows, Mac, Custom等

# 以下为在同一文件中配置不同语言的格式化风格
# C++ 特定配置
# If:
#   Language: Cpp
# Then:
#   Standard: Latest
#   PointerAlignment: Right

Language:        Cpp
# BasedOnStyle:  Google

# 访问说明符(public、private等)的偏移
AccessModifierOffset: -4
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
AlignAfterOpenBracket: Align
# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: false
# 连续声明时,对齐所有声明的变量名
AlignConsecutiveDeclarations: false
# 转义的换行符左对齐: true, false
AlignEscapedNewlinesLeft: true
# 操作符的对齐: true, false
AlignOperands:   true
# 尾随注释的对齐: true, false
AlignTrailingComments: true
# 允许所有参数在声明时在下一行: true, false
AllowAllParametersOfDeclarationOnNextLine: true
# 允许短块在同一行: true, false
AllowShortBlocksOnASingleLine: false
# 允许短case标签在同一行: true, false
AllowShortCaseLabelsOnASingleLine: false
# 允许短函数在同一行: true, false
AllowShortFunctionsOnASingleLine: All
# 允许短if语句在同一行: true, false
AllowShortIfStatementsOnASingleLine: true
# 允许短循环语句在同一行: true, false
AllowShortLoopsOnASingleLine: true
# 定义返回类型后是否总是换行: None, All, TopLevel
AlwaysBreakAfterDefinitionReturnType: None
# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), 
# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义)
AlwaysBreakAfterReturnType: None
# 总是在多行字符串前换行: true, false
AlwaysBreakBeforeMultilineStrings: true
# 总是在模板声明后换行: true, false
AlwaysBreakTemplateDeclarations: false
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: true
# false表示所有形参要么都在同一行,要么都各自一行
BinPackParameters: true
# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效
BraceWrapping:
  # class定义后面
  AfterClass:      false
  # 控制语句后面
  AfterControlStatement: false
  # enum定义后面
  AfterEnum:       false
  # 函数定义后面
  AfterFunction:   false
  # 命名空间定义后面
  AfterNamespace:  false
  # 结构体定义后面
  AfterStruct:     false
  # 联合体定义后面
  AfterUnion:      false
  # 在catch块之前换行
  AfterObjCDeclaration: false
  # 在else块之前换行
  BeforeElse:      false
  # 在catch块之前换行
  BeforeCatch:     false
  # 缩进大括号
  IndentBraces:    false
    # 分离空函数
  SplitEmptyFunction: false
  # 分离空语句
  SplitEmptyRecord: false
  # 分离空命名空间
  SplitEmptyNamespace: false
# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) 
BreakBeforeBinaryOperators: None
# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), 
#   Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), 
#   Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom
#   注:这里认为语句块也属于函数
BreakBeforeBraces: Custom
# 在三元运算符前换行 true, false
BreakBeforeTernaryOperators: true
# 在构造函数初始化列表中,逗号后是否换行: true, false
BreakConstructorInitializersBeforeComma: true
# 在Java字段注解后换行: true, false
BreakAfterJavaFieldAnnotations: false
# 在字符串字面量前换行: true, false
BreakStringLiterals: true
# 列限制: 0表示不限制列数
ColumnLimit:     100
# 注释风格: 
CommentPragmas:  '^ IWYU pragma:'
# 构造函数的初始化列表要么都在同一行,要么都各自一行
ConstructorInitializerAllOnOneLineOrOnePerLine: true
# 构造函数初始化列表的缩进宽度
ConstructorInitializerIndentWidth: 8
# 换行缩进宽度
ContinuationIndentWidth: 8
# 去除C++11的列表初始化的大括号{后和}前的空格
Cpp11BracedListStyle: true
# 指针对齐方式: Left, Right, Middle
DerivePointerAlignment: false
# 禁止格式化: true, false
DisableFormat:   false
# 自动检测二进制打包: true, false
ExperimentalAutoDetectBinPacking: true
# 宏定义的缩进宽度
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
  - Regex:           '^<.*\.h>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3
IncludeIsMainRegex: '([-_](test|unittest))?$'
# 缩进case标签
IndentCaseLabels: true
# 缩进宽度
IndentWidth:     4
# 函数返回类型换行时,缩进函数声明或函数定义的函数名
IndentWrappedFunctionNames: false
# 字符串字面量的引号风格: Leave, Add, Remove
JavaScriptQuotes: Leave
# 导入语句是否换行: true, false
JavaScriptWrapImports: true
# 空行是否保留: true, false
KeepEmptyLinesAtTheStartOfBlocks: false
# 最大连续空行数: 1
MaxEmptyLinesToKeep: 1
# 命名空间缩进: None, Inner, All
NamespaceIndentation: None
# 块缩进宽度
ObjCBlockIndentWidth: 2
# 协议列表前是否加空格: true, false
ObjCSpaceAfterProperty: false
# 协议列表前是否加空格: true, false
ObjCSpaceBeforeProtocolList: false
# 换行前的空格数: 0表示不限制
PenaltyBreakBeforeFirstCallParameter: 1
# 注释换行时,注释的缩进宽度: 0表示不缩进
PenaltyBreakComment: 300
# 连续的<后是否换行: true, false
PenaltyBreakFirstLessLess: 120
# 字符串字面量换行时,字符串字面量的缩进宽度: 0表示不缩进
PenaltyBreakString: 1000
# 超过行宽的字符数: 1000000
PenaltyExcessCharacter: 1000000
# 函数返回类型单独一行时的缩进宽度: 0表示不缩进
PenaltyReturnTypeOnItsOwnLine: 200
# 指针对齐方式: Left, Right, Middle
PointerAlignment: Left
# 注释是否换行: true, false
ReflowComments:  true
# 允许排序#include
SortIncludes:    true
# 空格是否在C风格类型转换后: true, false
SpaceAfterCStyleCast: false
# 空格是否在模板关键字后: true, false
SpaceAfterTemplateKeyword: true
# 赋值运算符前是否加空格: true, false
SpaceBeforeAssignmentOperators: true
# 左括号前是否加空格: ControlStatements(控制语句的左括号前加空格), Always(总是加空格), Never(从不加空格)
SpaceBeforeParens: ControlStatements
# 空括号是否加空格: true, false
SpaceInEmptyParentheses: false
# 注释前的空格数: 2
SpacesBeforeTrailingComments: 2
# 角括号前是否加空格: true, false
SpacesInAngles:  false
# 容器字面量的元素间是否加空格: true, false
SpacesInContainerLiterals: true
# 类型转换的圆括号前是否加空格: true, false
SpacesInCStyleCastParentheses: false
# 圆括号内是否加空格: true, false
SpacesInParentheses: false
# 方括号内是否加空格: true, false
SpacesInSquareBrackets: false
# 代码风格: Auto, LLVM, Google, Chromium, Mozilla, WebKit, GNU
Standard:        Auto
# 缩进宽度: 2, 4, 8, 16, 32, 64
TabWidth:        4
# 使用Tab还是空格: Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab:          Never
---
Language:        ObjC
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     100
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: true
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
  - Regex:           '^<.*\.h>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth:     4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments:  true
SortIncludes:    true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Auto
TabWidth:        4
UseTab:          Never
...


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容