Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put break before colon and after comma in constructor

I'm trying to create a constructor initializer format like:

Constructor()
  : initializer1(),
    initializer2()

According the documentation, I tried to set BreakConstructorInitializers to BeforeColon (the description says: "Break constructor initializers before the colon and after the commas.", and this is exactly what I want), but it is putting the break before the comma, which is not what I want. Am I doing something wrong or is this a bug in clang-format? I tried both clang-format-5.0 and clang-format-6.0, and they behaved the same way.

Here is my full .clang-format file:

---
Language:        Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: TopLevel
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:   
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: GNU
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     140
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:   
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeCategories: 
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth:     2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments:  true
SortIncludes:    false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  true
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        8
UseTab:          Never
...

I tried every possible combination of the BreakConstructorInitializersBeforeComma and the BreakConstructorInitializers keys; none of them seems what I want.

Here are the results:

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
Constructor() : a(1), b(1)
{
}

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
Constructor()
  : a(1)
  , b(1)
{
}

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
Constructor() : a(1), b(1)
{
}

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
Constructor()
  : a(1)
  , b(1)
{
}

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeComma
Constructor()
  : a(1)
  , b(1)
{
}   

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: AfterColon
Constructor() : a(1), b(1)
{
}

like image 402
Ádám Bakai Avatar asked Dec 19 '17 10:12

Ádám Bakai


1 Answers

I have the same problem as you and investigated a bit.

So first, BreakConstructorInitializers was changed to be an enum and BreakConstructorInitializersBeforeComma was deprecated. (https://reviews.llvm.org/D32479)

When I look at what you actually wanted to achieve, you wanted BreakConstructorInitializersAFTERComma instead, which is not a thing. You can achieve what you want via ColumnLimits only. Sadly.

like image 153
Stan Avatar answered Sep 28 '22 07:09

Stan