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.
2 Answers 2
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
2 Comments
.clang-format:30:1: error: unknown key 'ArrayInitializerAlignmentStyle'ArrayInitializerAlignmentStyle is not a key, it's the type of the AlignArrayOfStructures setting. So AlignArrayOfStructures: Left is what you are looking for.The option I found out is BinPackArguments. Setting it to false prevents packing function arguments and array initializer elements onto the same line.
PackConstructorInitializersStylevalueCurrentLine. This requires clang-format 14.