c++ - clang-format:如何将构造函数的初始化列表的每个元素保留在单独的行上
问题描述
我有一个像这样的 C++ 类:
class A {
public:
A() :
m_a(someValue1),
m_b(someValue2),
m_c(someValue3)
{
}
// .... other class members
private:
A m_a;
B m_b;
C m_c;
};
使用 clang-format 格式化此代码后,我得到:
class A {
public:
A() :
m_a(someValue1), m_b(someValue2), m_c(someValue3)
{
}
// .... other class members
private:
A m_a;
B m_b;
C m_c;
};
即构造函数中的初始化列表被格式化为单行,直到达到允许的最大行长度。
我的问题是如何告诉 clang-format 将每个元素留在自己的行中,就像在格式化之前在我的原始代码中一样?我找不到任何合适的参数。我尝试将在我看来最合适的参数 AllowShortBlocksOnASingleLine 设置为 true 和 false,但这对此没有影响。那么任何人都可以建议如何实现这种格式?
这是我的 .clang 格式:
BasedOnStyle: Google
AccessModifierOffset: '-4'
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Left
AlignOperands: 'true'
AlignTrailingComments: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakAfterJavaFieldAnnotations: 'true'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
SplitEmptyFunction: true
BreakBeforeInheritanceComma: 'false'
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: AfterColon
BreakStringLiterals: 'true'
ColumnLimit: '100'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '8'
Cpp11BracedListStyle: 'true'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
ExperimentalAutoDetectBinPacking: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Preserve
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
JavaScriptQuotes: Double
JavaScriptWrapImports: 'true'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: 'false'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'true'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '2'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never
更新:有一个名为“ConstructorInitializerAllOnOneLineOrOnePerLine”的clang格式选项描述如下:“如果构造函数初始化程序不适合一行,则将每个初始化程序放在自己的行上”。但是,它仍然不能满足我的要求,因为它只会在不符合列限制的情况下将初始化程序移动到新行。因此,即使未达到列限制,似乎也无法强制 clang-format 将后续初始化程序放在下一行。如果上述选项变成带有附加值的枚举,即使没有达到列限制,也会强制将初始化程序放到下一行。
解决方案
2021 年更新:
在 clang14 中,有一个名为
PackConstructorInitializers
的新选项可以满足
您的需求。
其他选项ConstructorInitializerAllOnOneLineOrOnePerLine和AllowAllConstructorInitializersOnNextLine无论如何都令人困惑,将被弃用。
结束更新
我很确定这是 clang 格式的错误/缺点。该问题已在 2015 年得到解决,但被 clang 格式开发人员拒绝:https ://reviews.llvm.org/D14484
对于它的价值,我对 clang-format 做了一个简单的改变,应该给你,你的预期行为: https ://github.com/Nikolai-Hlubek/clang/tree/ConstructorInitializer_AlwaysBreakAfterColon
我向上游提出了推送请求,但我怀疑它会被接受。
推荐阅读
- python - 使用数据集的 matplotlib 绘制箱线图
- laravel-6 - 运行 php artisan config:cache 时出错
- javascript - 为什么 cookieparser 不返回值?
- java - 通过 nginx 反向代理终止 gRPC 连接
- oracle - Oracle:如何在没有父节点的情况下查询所有行,但如果有任何引用 - 避免?
- python - 使用 HTTP POST 的 OAuth 2.0 令牌的 python 脚本有问题吗?
- c# - WPF中基于ListView创建自定义控件的问题
- json - 模板中的 Django Json 响应格式
- android - Android SQLiteOpenHelper:确定 onUpgrade 异常的原因
- java - 删除表中的数据问题(Java Swing)