如何让 clang-format 在 << 和 >> 上中断

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

如何让 clang-format 在“<<”和“>>”运算符上中断?

我尝试将 BreakBeforeBinaryOperators 设置为 All,但这似乎没有任何区别。

我希望我的代码格式如下:

in >> a
   >> b
   >> c;

而不是

in >> a >> b >> c;

为此,您需要两个样式设置:

BreakBeforeBinaryOperators: All
ColumnLimit: 0

或者,将样式设置为 pre-defined WebKit 样式,这将为您设置这两个样式设置。

documentation 说:

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.

这不是一个很好的解决方案,原因如下:

  • 如文档所述,clang-format 将遵守您开始时使用的换行符。但如果它们不存在,它不会添加换行符。所以一行 in >> a >> b >> c; 不会被分成单独的行。

  • 输出没有将第一行的 >> 与后续行的 >> 对齐。它只是缩进了通常的 continued-line 量。例如,如果你的正常缩进是 4,你将得到:

    in >> a
        >> b
        >> c;
    

    这其实有点奇怪,因为如果运算符是<<,clang-format会把三个运算符都排成一行。

  • 您可能希望 ColumnLimit 设置为其他值,例如 80,以便其他行在合理的地方中断。

但这似乎是 clang-format 所能做到的最好的了。

我用 clang-format 6.0.0 进行了测试,但我相信较新的版本也会有类似的行为,而且我没有看到任何看起来会有帮助的新样式选项。