c++ - 如何使 Clang-Format 在 : operator in class/struct 之前插入换行符

标签 c++ clang-format

我正在尝试使用 clang-format 3.7 格式化一些代码。

我有一个看起来有点像这样的结构:

struct NameOfStruct
{
  explicit NameOfStruct()
    : Thing1(-1), Thing2(-1)
  {}

  std::size_t objectA;
  std::size_t objectB;
};

这很好,但是 clang-format 坚持这样格式化它:

struct NameOfStruct
{
  explicit NameOfStruct() : Thing1(-1), Thing2(-1)
  {
  }

  std::size_t objectA;
  std::size_t objectB;
};

我更愿意将 Thing1Thing2 放在下一行,并且更愿意将大括号放在同一行,但我可以没有它。

这是我当前的 .clang-format 文件,有什么办法可以改变它吗?

Language:        Cpp
# BasedOnStyle:  Google
AccessModifierOffset: -1
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignEscapedNewlinesLeft: true
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit:     120
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: true
IndentWidth:     4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All 
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        8
UseTab:          Never

最佳答案

你有 BreakConstructorInitializersBeforeComma: false 你想要 true

关于c++ - 如何使 Clang-Format 在 : operator in class/struct 之前插入换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33895699/

相关文章:

C++ Box2D - 单独放置时不受重力影响的动态物体

Objective-C Clang-Format 方法大括号中断

c++ - 使 clang-format 允许在其自己的行上返回函数类型

c++ - 如何让 clang-format 在 << 和 >> 上中断

C++ 在 Visual Studio Code 中垂直对齐声明

c++ - Valgrind 无效读取大小 4 错误?

c++ - 引用水平第 2 部分

C++ DirectX FPS 相机怪异

c++ - 宏覆盖类成员

C++:clang-format with git