Reputation: 813
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?
Upvotes: 35
Views: 8291
Reputation: 2231
As answered by @Daniel Kiss, the correct configuration to achieve your requested format is:
AlignAfterOpenBracket: AlwaysBreak
BinPackArguments: false
BinPackParameters: false
To inspect the behaviour of those configurations on your example code, using the following command:
echo 'SomeCall(aaa, bbb, ccc);' \
| \
clang-format-14 \
--Werror \
--style="{\
AlignAfterOpenBracket: AlwaysBreak, \
BinPackParameters: false, \
BinPackArguments: false, \
ColumnLimit: 1 \
}"
For ColumnLimit
between 1 to 18 the output is:
SomeCall(
aaa,
bbb,
ccc);
For ColumnLimit
between 19 to 23 the output is:
SomeCall(
aaa, bbb, ccc);
For ColumnLimit
greater than or equal to 24 (or equal 0) the output is:
SomeCall(aaa, bbb, ccc);
Upvotes: 0
Reputation: 371
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.
Upvotes: 20
Reputation: 15603
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:
clang-format
.Upvotes: 1