如何在定义之间添加空行?

How to add blank lines between definitions?

我成功地让 clang-format 像 iIwant 一样格式化了我的代码。但是,有一件事让我很烦恼:

我想在 structs/classes/functions 的定义和函数声明之间留一个空行。目前,在格式化时,clang-format 会删除空行,这会使所有内容都被压缩。

这是我的文件:

---
AlignAfterOpenBracket: DontAlign
AlignTrailingComments: "true"
AllowAllArgumentsOnNextLine: "false"
AllowAllConstructorInitializersOnNextLine: "true"
AllowAllParametersOfDeclarationOnNextLine: "false"
AllowShortBlocksOnASingleLine: "false"
AllowShortCaseLabelsOnASingleLine: "false"
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: "false"
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: "false"
AlwaysBreakTemplateDeclarations: "Yes"
BinPackArguments: "true"
BinPackParameters: "true"
BreakBeforeTernaryOperators: "false"
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: "170"
CompactNamespaces: "false"
ConstructorInitializerAllOnOneLineOrOnePerLine: "true"
IncludeBlocks: Merge
IndentCaseLabels: "true"
IndentPPDirectives: BeforeHash
IndentWidth: "4"
IndentWrappedFunctionNames: "false"
KeepEmptyLinesAtTheStartOfBlocks: "false"
Language: Cpp
MaxEmptyLinesToKeep: "0"
NamespaceIndentation: All
PointerAlignment: Left
SortIncludes: "true"
SortUsingDeclarations: "true"
SpaceAfterCStyleCast: "false"
SpaceAfterLogicalNot: "false"
SpaceAfterTemplateKeyword: "false"
SpaceBeforeAssignmentOperators: "true"
SpaceBeforeCpp11BracedList: "false"
SpaceBeforeCtorInitializerColon: "false"
SpaceBeforeInheritanceColon: "false"
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: "false"
SpaceInEmptyParentheses: "false"
SpacesBeforeTrailingComments: "3"
SpacesInAngles: "false"
SpacesInCStyleCastParentheses: "false"
SpacesInContainerLiterals: "false"
SpacesInParentheses: "false"
SpacesInSquareBrackets: "false"
Standard: Auto
UseTab: Always
TabWidth: "4"

外观如下:

我想要两个结构之间有一个空行。

你有

MaxEmptyLinesToKeep: "0"

需要设置为 1,以确保不会删除定义之间的多个空行。

重要的是,该值必须是无符号类型,而不是字符串。

所以你想要的正确设置应该是

MaxEmptyLinesToKeep: 1

请注意,如果定义之间没有至少一个空行,这将不起作用,但从 clang-format 14 开始,您可以使用

SeparateDefinitionBlocks : Always

这将在每个定义之间添加一个空行。其他选项是 Leave,它不会改变任何东西,Never,它将删除空行,如果有的话。

我认为在以前的版本中没有可让您执行此操作的选项。

来源:Clang format style options.