clang-format: always break if params don't fit?

荒凉一梦 提交于 2020-08-22 07:20:42

问题


I can't figure out if it's possible to configure clang-format to always break if parameters don't fit, ie:

// Try this first:
SomeCall(aaa, bbb, ccc);

// If doesn't fit, try this:
SomeCall(
     aaa, bbb, ccc);

// If still doesn't fit, do NOT try this:
SomeCall(aaa, bbb,
         ccc);

// and NOT this:
SomeCall(aaa,
         bbb,
         ccc);

// but immediately do this:
SomeCall(
     aaa,
     bbb,
     ccc);

So far I've concluded that it's not possible to do this with clang-format 3.4. Is it correct?


回答1:


In newer version of clang-format, this can now be achieved with:

AlignAfterOpenBracket: AlwaysBreak
BinPackArguments: false
BinPackParameters: false

See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for a full explanation of these options.




回答2:


I unfortunately only have access to clang-format 3.8.0 ("clang-format version 3.8.0 (tags/RELEASE_380/final)"), so I can't do testing easily for release 3.4.

There's a manual for the latest release of clang-format available here that I don't know if you've found or not. It links to the list of Clang-Format Style Options. In there, there's a style option that echoes the title of your question: AlignAfterOpenBracket: AlwaysBreak, Always break after an open bracket, if the parameters don’t fit on a single line.

To use this, put the following in your ~/.clang-format file:

AlignAfterOpenBracket: AlwaysBreak

After some testing, it appears that it is doing exactly what you would want it to do, almost.

It formats

SomeCall(aaa, bbb, ccc);

as

SomeCall(
  aaa, bbb,
  ccc);

if aaa, bbb, ccc doesn't fit on one line. It will not break between aaa and bbb until aaa also is too long, in which case bbb and ccc will be on the same line. I.e. it breaks after the opening (, but then tries to fill lines. It doesn't automatically break on all commas.

Looking at corresponding page for clang-format 3.4, it appears as if this configuration option sadly isn't there. This leaves you with two options:

  1. Upgrade to a newer version of clang-format.
  2. Don't.


来源:https://stackoverflow.com/questions/21966530/clang-format-always-break-if-params-dont-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!