25

Using clang-format with default settings, the following:

if ((exprA) && 
    (exprB))

turns into:

if ((exprA) && (exprB))

I'm trying to prevent the collapsing the conditions into a single line, with no success.
Is there currently a way to achieve this?

In clang-format documentation, the BreakBeforeBinaryOperators parameter seems the closest to what I'm after:

BreakBeforeBinaryOperators (BinaryOperatorStyle)
The way to wrap binary operators.

  • BOS_None (in configuration: None) Break after operators.

But it seems to kick in only when wrapping is required (column limit is exceeded), which isn't the usual case.

valiano
  • 16,433
  • 7
  • 64
  • 79
  • 2
    A workaround would be to add a comment at the end of the first line. clang-format won't join the lines then. – ambiso Oct 13 '18 at 14:06
  • You can also set the ColumnLimit to 0. See https://stackoverflow.com/questions/33656800/clang-format-line-breaks/34353127 – ambiso Oct 13 '18 at 14:08
  • 1
    Thanks for the helpful hints, @ambiso! Unfortunately, changing the source code is not feasible, since this formatting is spread through the entire application. Setting the column limit to 0 indeed works, but with some undesired side effects, like messing the alignment of c'tor parameters (which may be fixed with some tweaking). It's also not ideal since then there's no enforcing of the column limit, but still it's a step forward from what I had until now. – valiano Oct 13 '18 at 14:41
  • 1
    @ambiso Could you please format your comments into an answer, and maybe extend it a bit? This way I could award you the bounty, if there are no other answers by the time the bounty expires. – valiano Oct 18 '18 at 06:48

1 Answers1

2

According to Clang 10 document, your request can be done. You have to do these two changes into your clang file

  1. In BraceWrapping: set AfterControlStatement: true.
  2. After than set ColumnLimit: 1
output -: if ((exprA) &&
              (exprB))

You can try this at Here If you have any doubt. but problem is your other codes will also be formatted.

There is no way to split only if ((exprA) && (exprB)). It can be done if anyone can create a patch for that but creating a patch for this is not so much easy. It takes a lot of time and energy.

Kalana
  • 5,631
  • 7
  • 30
  • 51