Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discourage clang-format to break after = signs?

I'm using clang-format with a fairly minimal configuration file, and I'm not very familiar with the options. For the sake of the question, consider this piece of unformatted code:

int msgResult = ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE, MB_STYLE_ERROR);

When I run clang-format on this snippet, I get

int msgResult
    = ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE, MB_STYLE_ERROR);

But I'd prefer

int msgResult = ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE, 
                         MB_STYLE_ERROR);

Is there a way to enforce not breaking after =, or at least prefer not to?

like image 437
Tamás Szelei Avatar asked Sep 02 '15 11:09

Tamás Szelei


1 Answers

I believe you want these two style options:

BinPackArguments: true
AlignAfterOpenBracket: Align

Without BinPackArguments, clang-format will try to either put all function arguments on one line, or if it can't will put one function argument per line. Most of the pre-defined styles already set BinPackArguments to true, but the Mozilla style does not.

Without AlignAfterOpenBracket set to Align, clang-format will sometimes prefer to put all arguments on the second line, rather than just a single argument that doesn't line up with the other arguments. Most of the pre-defined styles already set AlignAfterOpenBracket to Align, but the WebKit style does not.

See the documentation for more details about these settings.

My testing was done with clang-format 6.0.0. Perhaps you had an earlier version that didn't support all this.

like image 130
Eric Backus Avatar answered Oct 18 '22 18:10

Eric Backus