Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clang-format to break on << and >>

How can I get clang-format to break on '<<' and '>>' operators?

I tried setting BreakBeforeBinaryOperators to All, but this does not seem to make any difference here.

I would like my code to be formatted like this:

in >> a
   >> b
   >> c;

instead of

in >> a >> b >> c;
like image 201
A Trung Avatar asked Nov 07 '22 16:11

A Trung


1 Answers

To achieve this, you need two style settings:

BreakBeforeBinaryOperators: All
ColumnLimit: 0

Or, set the style to the pre-defined WebKit style, which will set those two style settings for you.

The documentation says:

A column limit of 0 means that there is no column limit. In this case, clang-format will respect the input’s line breaking decisions within statements unless they contradict other rules.

This isn't a great solution, for several reasons:

  • As the documentation says, clang-format will respect the line breaks that you started with. But it won't add line breaks if they're not already there. So a line in >> a >> b >> c; will not get broken into separate lines.

  • The output does not line up the >> of the first line with the >> of the subsequent lines. It is simply indented by the usual continued-line amount. For example, if your normal indentation is 4, you will get:

    in >> a
        >> b
        >> c;
    

    This is actually a little strange, because if the operator was <<, clang-format would line up all three of the operators.

  • You may, and probably do, want ColumnLimit set to something else like 80 so that other lines get broken in reasonable places.

But this seems to be the best that can be done with clang-format.

I tested with clang-format 6.0.0, but I believe newer versions will act similarly, and I don't see any new style options which look like they would help.

like image 98
Eric Backus Avatar answered Nov 15 '22 05:11

Eric Backus