clang-format: How to format an 'Array of Structures' initialization

208 Views Asked by At

For a retro C project I have among other things a ColumnLimit: 72 set, and all braces should be on a new line: BreakBeforeBraces: Allman. Additionally I would like to achieve that initialization of 'array of structs' is not wrapped at all.

So the following array initialization should remain as it is:

CharacterInfo characterInfo[] =
{
  {CT_HERO, ST_CUSTOM_BOB, "Dev:project/gfx/gondola_hero.ilbm", 6, NULL},
  {CT_GONDOLA_EMPTY,  ST_CUSTOM_BOB, "Dev:project/gfx/gondola_empty.ilbm", 1, NULL},
  {CT_WOLF, ST_CUSTOM_BOB, "Dev:project/gfx/wolf.ilbm", 14, NULL},
  {CT_BALLOON, ST_CUSTOM_BOB, "Dev:project/gfx/balloon.ilbm", 10, NULL},
  {CT_ARROW, ST_EXT_SPRITE, "Dev:project/gfx/arrow.ilbm", 2, NULL},
  {CT_CHESTNUT, ST_EXT_SPRITE, "Dev:project/gfx/chestnut.ilbm", 1, NULL},
};

I've meanwhile come to the following .clang-format file:

ColumnLimit: 72
ConstructorInitializerIndentWidth: 2
IndentWidth: 2
Language: Cpp
TabWidth: 2
UseTab: Never
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Allman
BreakConstructorInitializers: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentFunctionDeclarationAfterType: false
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerAlignment: Right
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false

But using this my array initialization is wrapped like that:

CharacterInfo characterInfo[] = {
  {CT_HERO, ST_CUSTOM_BOB, "Dev:project/gfx/gondola_hero.ilbm", 6,
   NULL},
  {CT_GONDOLA_EMPTY, ST_CUSTOM_BOB,
   "Dev:project/gfx/gondola_empty.ilbm", 1, NULL},
  {CT_WOLF, ST_CUSTOM_BOB, "Dev:project/gfx/wolf.ilbm", 14, NULL},
  {CT_BALLOON, ST_CUSTOM_BOB, "Dev:project/gfx/balloon.ilbm", 10, NULL},
  {CT_ARROW, ST_EXT_SPRITE, "Dev:project/gfx/arrow.ilbm", 2, NULL},
  {CT_CHESTNUT, ST_EXT_SPRITE, "Dev:project/gfx/chestnut.ilbm", 1,
   NULL},
};

I would like to get { on the first line wrapped to a new line and that the individual initialization lines in the array not to be wrapped at all.

How can this be achieved?

1

There are 1 best solutions below

0
J i N On

To achieve the desired formatting for the 'array of structs' initialization in your C project using Clang-Format, you can make a few modifications to your .clang-format configuration.

1. Set BraceWrapping options to control the formatting of braces.

2. Set BinPackArguments and BinPackParameters to false.

3. Set ConstructorInitializerAllOnOneLineOrOnePerLine to false.

ColumnLimit: 72
ConstructorInitializerIndentWidth: 2
IndentWidth: 2
Language: Cpp
TabWidth: 2
UseTab: Never
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false  # Set to false
BinPackParameters: false  # Set to false
BreakBeforeBinaryOperators: All
BraceWrapping:
AfterStruct: true
AfterUnion: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: true
BreakConstructorInitializers: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false  # Set to false
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentFunctionDeclarationAfterType: false
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerAlignment: Right
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false

With these changes, the initialization of the 'array of structs' should remain as you desire, with the opening brace on a new line, and the individual initialization lines in the array not wrapped.