Take this example before clang-format runs:
struct ApplicationState app_state = {
.signal = {
.use_crash_handler = true,
.use_abort_handler = true,
},
.exit_code_on_error = {
.python = 0,
}
};
After running, clang-format applies as follows:
struct ApplicationState app_state = {.signal =
{
.use_crash_handler = true,
.use_abort_handler = true,
},
.exit_code_on_error = {
.python = 0,
}};
Is there a way to add a newline after the brace, before the struct member so it's more like the first example and not like the second?
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! */
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With