如何让 clang-format 在打开函数的大括号之前添加新行?
how to make clang-format add new line before opening brace of a function?
我有兴趣为函数放置一个大括号(但不是 if 语句和其他上下文)。例如
void foo()
{
...
}
撇开争论不谈,不这样做是否有充分的理由?尽管我对 if/else 和较小的块使用同行左括号,但我认为在这种情况下,较大代码单元 (functions/methods/classes/structs) 的视觉组织可以胜过完美的一致性。
此外,如何让 clang-format 遵循这种风格?
正如 documentation 所说,用 -style=file
调用 clang-format
,并使用放置在封闭目录中的 .clang-format
文件来自定义样式选项。指定大括号位置的格式样式选项称为 BreakBeforeBraces
。从文档中,
BreakBeforeBraces (BraceBreakingStyle
)
The brace breaking style to
use.
Possible values:
BS_Attach
(in configuration: Attach
) Always attach braces to surrounding context.
BS_Linux
(in configuration: Linux
) Like Attach
, but break before braces on function, namespace and class definitions.
BS_Stroustrup
(in configuration: Stroustrup
) Like Attach
, but break before function definitions, and ‘else’.
BS_Allman
(in configuration: Allman
) Always break before braces.
BS_GNU
(in configuration: GNU
) Always break before braces and add an extra level of indentation to braces of control statements, not to
those of class, function or other definitions.
符合您描述的款式是BS_Stroustrup
。将以下条目添加到您的 .clang-format
BreakBeforeBraces: Stroustrup
除了文档之外,clangformat.com 列出了所有选项并用示例说明了其中的许多选项。
Pradhan 有一个 ,但您可能会发现在您不想要的地方会出现中断(正如我发现的那样)。
还有另一个选项,"Custom",至少在 clang-format 版本 3.8 和 5 中(我使用的是 3.8 并在 5 个文档中找到 BS_Custom)。有了这个,您可以在 BraceWrapping 中指定您想要的内容,包括 "AfterFunction" 选项。
在以下示例摘录中,我将其他人列为 true/false,因为 OP 的问题仅指定 AfterFunction(即 "before opening brace of a function"):
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true/false
AfterFunction: true
AfterNamespace: true/false
AfterObjCDeclaration: true/false
AfterStruct: true/false
AfterUnion: true/false
BeforeCatch: true/false
BeforeElse: true/false
IndentBraces: true/false
BreakBeforeBraces: Custom
我已经用我的配置对此进行了测试,它可以更细粒度地控制大括号断开。
我有兴趣为函数放置一个大括号(但不是 if 语句和其他上下文)。例如
void foo()
{
...
}
撇开争论不谈,不这样做是否有充分的理由?尽管我对 if/else 和较小的块使用同行左括号,但我认为在这种情况下,较大代码单元 (functions/methods/classes/structs) 的视觉组织可以胜过完美的一致性。
此外,如何让 clang-format 遵循这种风格?
正如 documentation 所说,用 -style=file
调用 clang-format
,并使用放置在封闭目录中的 .clang-format
文件来自定义样式选项。指定大括号位置的格式样式选项称为 BreakBeforeBraces
。从文档中,
BreakBeforeBraces (
BraceBreakingStyle
)The brace breaking style to use.
Possible values:
BS_Attach
(in configuration:Attach
) Always attach braces to surrounding context.BS_Linux
(in configuration:Linux
) LikeAttach
, but break before braces on function, namespace and class definitions.BS_Stroustrup
(in configuration:Stroustrup
) LikeAttach
, but break before function definitions, and ‘else’.BS_Allman
(in configuration:Allman
) Always break before braces.BS_GNU
(in configuration:GNU
) Always break before braces and add an extra level of indentation to braces of control statements, not to those of class, function or other definitions.
符合您描述的款式是BS_Stroustrup
。将以下条目添加到您的 .clang-format
BreakBeforeBraces: Stroustrup
除了文档之外,clangformat.com 列出了所有选项并用示例说明了其中的许多选项。
Pradhan 有一个
还有另一个选项,"Custom",至少在 clang-format 版本 3.8 和 5 中(我使用的是 3.8 并在 5 个文档中找到 BS_Custom)。有了这个,您可以在 BraceWrapping 中指定您想要的内容,包括 "AfterFunction" 选项。
在以下示例摘录中,我将其他人列为 true/false,因为 OP 的问题仅指定 AfterFunction(即 "before opening brace of a function"):
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true/false
AfterFunction: true
AfterNamespace: true/false
AfterObjCDeclaration: true/false
AfterStruct: true/false
AfterUnion: true/false
BeforeCatch: true/false
BeforeElse: true/false
IndentBraces: true/false
BreakBeforeBraces: Custom
我已经用我的配置对此进行了测试,它可以更细粒度地控制大括号断开。