clang-format: how to avoid aligned indentation on wrapping

Hi

I like to keep the { on the same line when wrapping isn’t required, eg

for (a; b; c) {
d;
}

but I don’t like it on the same line when wrapped:

for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
cccccccccccccccccccccccccccccc) {
dddddddddddddddddddddddddddddd;
}

I find it hard to distinguish the scope quickly as the c’s and d’s nearly align. I find:

for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
cccccccccccccccccccccccccccccc)
{
dddddddddddddddddddddddddddddd;
}

preferable in this case. I can’t find a why to selectively have the { positioned. BreakBeforeBraces in Allman style doesn’t support the single line case and the others don’t support the later case.

Is there a way to make work? If not I’m glad to work on coding a solution but would appreciate some pointers.

Thanks,
Matt

This doesn’t work yet.

To get this addressed, there are basically three somewhat distinct tasks:

  1. Come up with a way to configure this. I am not yet sure how best to do that as the decision is somewhat independent of BreakBeforeBraces. I am thinking we might want an additional option UseBraceStyleForOneLiners or something. On the other hand, not all combinations make sense (the flag would be ineffective for BS_Attach). Maybe somebody else has an idea?

  2. Write tests in unittests/Format/FormatTest.cpp. There are already plenty of tests concerning the brace style and we probably want to verify that if the configuration is set accordingly, we do the right thing for one-liners as well as wrapped statements. And that for if/for/while/switch/function/class/…

  3. Implement the behavior. I think the easiest way to achieve this might be to use the LineJoiner (lib/Format/UnwrappedLineFormatter.cpp) and allow it to merge the “{” back to the previous line if both fit into the ColumnLimit.

Personal note: I find that style very distracting ;-).

Hi

I like to keep the { on the same line when wrapping isn't required, eg

for (a; b; c) {
    d;
}
[...]
for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
     cccccccccccccccccccccccccccccc)
{
    dddddddddddddddddddddddddddddd;
}

This doesn't work yet.
[...]
Personal note: I find that style very distracting ;-).

FWIW, a large code base I work on uses a variant of that style:

for (a; b; c) {
  d;
}

for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
    cccccccccccccccccccccccccccccc
) {
    d;
}

i.e. on the same line when it fits (the line length limitation); on the
next line along with the closing paren when it doesn't.

Some devs use yet another variant:

for (
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
    cccccccccccccccccccccccccccccc
) {
    d;
}

Like Matt, I find the scope to be visually much better defined that way,
but of course I've seen and used that style for years, so YMMV.

Some might argue our variant is more consistent, in the sense the braces
are "right-left" in all cases. --D

Thanks for the help. I had been trying to allow canBreakBefore to allow the ‘)’ and the ‘{’ to split but it wasn’t working very well. I’ll give the LineJoiner a try instead.

In terms of an option I’m wondering what values UseBraceStyleForOneLiners would take? Is it true/false where false implies always breaking and true is whatever the style does by default? Perhaps AllowBraceOnWrappedLine?

I don’t want to tackle all the various styles Dominique mentioned though they are interesting.

Matt