How to wrap struct initializer in clang-format?
The primary thing you need is:
BreakBeforeBinaryOperators: All
You could instead set the overall style to WebKit
, because that sets BreakBeforeBinaryOperators
to All
.
Clearly, clang-format must see the .
as a binary operator. I'm not sure if it should, but that's what it seems to do.
I tested this with clang-format 6.0.0. Presumably newer versions would work the same, but I haven't tested so I can't be sure.
Currently clang-format
doesn't have a useful way of controlling this (as of version 14.0).
While BreakBeforeBinaryOperators: All
does force wrapping (see @eric-backus' s answer), it impacts formatting in many other places too, unrelated to struct declaration.
You can however, workaround this simply by using a trailing comma.
Before:
struct ApplicationState app_state = {.signal =
{
.use_crash_handler = true,
.use_abort_handler = true,
},
.exit_code_on_error = {
.python = 0,
}};
After:
struct ApplicationState app_state = {
.signal = {
.use_crash_handler = true,
.use_abort_handler = true,
},
.exit_code_on_error = {
.python = 0,
},
};
/* ^ notice trailing comma on the second last line! */