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?
Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.
clang-format is a tool to automatically format C/C++/Objective-C code, so that developers don't need to worry about style issues during code reviews. It is highly recommended to format your changed C++ code before opening pull requests, which will save you and the reviewers' time.
clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.
Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named . clang-format or _clang-format — these files often become a part of your project where you keep all code style rules.
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.
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
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With