Separate each entry of brace-enclosed lists with new-line via clang-format

1.5k Views Asked by At

I have brace-enclosed lists to initialize an array. I'd like to have my array initializers new-line for each entry of the array, which looks like:

const char* const array_of_strings[] = {
    "Hey",
    "Hi",
    "Hello",
    "Soup",
    "Atom"
};

Though, clang-format formats this array to:

const char *const array_of_strings[] = {"Hey", "Hi", "Hello", "Soup", "Atom"};

usually new lining once the list gets past ColumnLimit. Is my wanted formatting possible using clang-format?

1

There are 1 best solutions below

1
On BEST ANSWER

It is mostly possible. You need two things:

  • Set the BinPackArguments style option to false. I guess clang-format is treating the brace-enclosed initializer list as arguments. (The documentation doesn't really specify this, but it seems reasonable.)
  • Use the comma-after-last-item feature. To do this, add a comma between "Atom" and the closing brace. This feature is not documented anywhere that I've seen, but it has been in clang-format for many versions and many years.

With both of the above, and otherwise default clang-format settings, your output would look like:

const char *const array_of_strings[] = {
    "Hey",
    "Hi",
    "Hello",
    "Soup",
    "Atom",
};