clang-format: How to align list of struct inits

I have an array of structs:
(assume that the identifiers are #defined somewhere else…)

typedef struct
{
    int a;
    char id[2+1];
} T;

const T T_list[] = {
    { PRIO_HOUSE, "HI" },
    { PRIO_SOMETHING_ELSE, "WO" }
    // ...
}

I would like clang-format to format it like this:

const T T_list[] = {
    { PRIO_HOUSE         , "HI" },
    { PRIO_SOMETHING_ELSE, "WO" }
    // ...
}

Is it possible?

I already read the docs, but I didnt find something helpful in this regard. https://clang.llvm.org/docs/ClangFormatStyleOptions.html

This is my .clang-format

---
BasedOnStyle: WebKit
BreakBeforeBraces: Allman
BraceWrapping:
  AfterEnum: false  
IndentCaseLabels: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignEscapedNewlines: 'true'
AlignTrailingComments: 'true'
AllowShortFunctionsOnASingleLine: 'false'
#...

Answer

No. clang-format cannot do this.

The way I do it is:

  1. use a third party tool to align it
  2. before the formatted region put: //clang-format off
  3. after the formatted region put: //clang-format on

Leave a Reply

Your email address will not be published. Required fields are marked *