7

Clang-format, given an array of structs with initializers, is putting two items per line:

sym keywords[] = {
 {0, 0, "C"}, {0, 0, "T"},
 {0, 0, "V"}, {0, 0, "ax"},
 {0, 0, "bool"}, {0, 0, "break"},
 ...
 {0, 0, "val"}, {0, 0, "vector"},
 {0, 0, "version"}, {0, 0, "void"},
 {0, 0, "while"},
};

How can I get it to just put one item per line?

clang-format, array initialisers is the closest I have been able to find to a discussion of this problem, but neither of the proposed solutions has any effect; Cpp11BracedListStyle: false does the same thing except with extra spaces between the braces, and as you can see in the above, making sure there is a comma after the last item, doesn't help.

asked Oct 12, 2021 at 8:07
4
  • hi did you find any solution? any update? Commented Jan 13, 2022 at 3:51
  • @Ted Erm, the solution I found was to switch the project that was complex enough to generate such constructs from C++ to Java (for other reasons, granted), that's probably not very useful from your perspective, sorry! Commented Jan 13, 2022 at 23:59
  • @Ted I might have a solution, not sure if you are still interested. Commented Nov 2, 2022 at 15:55
  • 1
    Try: PackConstructorInitializersStyle value CurrentLine. This requires clang-format 14. Commented Nov 2, 2022 at 16:09

2 Answers 2

3

I'm not entirely sure, but ArrayInitializerAlignmentStyle set to left might achieve that result. This option was added with clang-format version 13.

At least my current settings turn your code into:

sym keywords[] = {
 {0, 0, "C" },
 {0, 0, "T" },
 {0, 0, "V" },
 {0, 0, "ax" },
 {0, 0, "bool" },
 {0, 0, "break" },
 {0, 0, "val" },
 {0, 0, "vector" },
 {0, 0, "version"},
 {0, 0, "void" },
 {0, 0, "while" },
};

If I remove that setting I get something similar to your result.

My current settings, which probably don't do what you expect them at other locations:

---
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
BreakBeforeBraces: Allman
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: 120
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: true
IncludeBlocks: Regroup
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
IndentWidth: 4
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PackConstructorInitializers: NextLine
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 10
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 10
PenaltyBreakString: 10
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 10
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
ReflowComments: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpacesInCStyleCastParentheses: false
Standard: Latest
TabWidth: 4
UseTab: Never
answered Nov 2, 2022 at 15:55
Sign up to request clarification or add additional context in comments.

2 Comments

I have clang-format 14.0.0 and your suggestion gives me errors: .clang-format:30:1: error: unknown key 'ArrayInitializerAlignmentStyle'
@zwol ArrayInitializerAlignmentStyle is not a key, it's the type of the AlignArrayOfStructures setting. So AlignArrayOfStructures: Left is what you are looking for.
0

The option I found out is BinPackArguments. Setting it to false prevents packing function arguments and array initializer elements onto the same line.

answered Sep 21 at 3:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.