为什么clang格式在getter括号前不中断? [英] Why doesn't clang-format break before braces of getter?
问题描述
相关帖子的答案如何在打开函数括号之前使clang格式添加新行?无济于事。
The answer of the related post how to make clang-format add new line before opening brace of a function? does not help.
我是在Windows的Eclipse CDT中将clang格式9.0.0与Cppstyle一起使用。 clang-format格式化以下吸气剂,如下所示:
I am using clang-format 9.0.0 with Cppstyle in Eclipse CDT on Windows. clang-format formats the following getter like this:
int returnNumber() { return 3; }
但我更喜欢格式
int returnNumber()
{
return 3;
}
我无法使clang-format做到这一点,也没有破坏样式 BS_Allman
或自定义样式。除了手动格式化还有其他解决方案吗?
I have not been able to make clang-format do that, neither with breaking style BS_Allman
nor with a custom style. Is there another solution than manual formatting?
我的示例源文件如下:
Header.h
#pragma once
namespace Test
{
class MyClass
{
public:
int returnNumber() { return 3; }
};
} /* namespace Test */
我的配置文件看起来像
Language: Cpp
AlwaysBreakTemplateDeclarations: 'true'
BreakBeforeBraces: Allman
ColumnLimit: '80'
IndentWidth: '2'
NamespaceIndentation: None
Standard: Cpp11
TabWidth: '2'
UseTab: Always
PointerAlignment: Left
AlignAfterOpenBracket: DontAlign
BreakConstructorInitializers: AfterColon
MaxEmptyLinesToKeep: 2
推荐答案
配置的问题是您错过了使用简短功能控制 clang-format
行为的选项。
The problem with your config is that you missed this option that controls the behavior of clang-format
with short function.
将此添加到您的配置中,一切将很不错:
Add this to your config and eveything will be good:
AllowShortFunctionsOnASingleLine: None
从 clang格式文档:
AllowShortFunctionsOnASingleLine( ShortFunctionStyle)
AllowShortFunctionsOnASingleLine (ShortFunctionStyle)
取决于值, int f(){返回0; }
可以放在一行上。
Dependent on the value, int f() { return 0; }
can be put on a single line.
可能的值:
-
SFS_None
(在配置中:无)切勿将函数合并为一行。
SFS_None
(in configuration: None) Never merge functions into a single line.
SFS_InlineOnly
(在配置中:InlineOnly)仅合并在类内部定义的函数。与内联相同,不同之处在于它不表示
表示空:即顶层空函数也不会合并。
SFS_InlineOnly
(in configuration: InlineOnly) Only merge functions defined inside a class. Same as "inline", except it does not
implies "empty": i.e. top level empty functions are not merged either.
SFS_Empty
(在配置中:空)仅合并空函数。
SFS_Empty
(in configuration: Empty) Only merge empty functions.
SFS_Inline
(在配置中:内联)仅合并在类内部定义的函数。表示空。
SFS_Inline
(in configuration: Inline) Only merge functions defined inside a class. Implies "empty".
SFS_All
(在配置中:全部)合并所有适合单个函数的功能
SFS_All
(in configuration: All) Merge all functions fitting on a single line.
这篇关于为什么clang格式在getter括号前不中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!