简体   繁体   中英

clang-format: how to prevent all function arguments on next line?

I have a C++ function call that I've manually and intentionally formatted like this:

DoSomethingForAPurposeThatCausesALongFunctionName(
    arg_0,
    arg_1,
    arg_2);

clang-format wants to re-format it like this:

DoSomethingForAPurposeThatCausesALongFunctionName(
    arg_0, arg_1, arg_2)

I do not want this. AllowAllParametersOfDeclarationOnNextLine appears to control this behavior for function declarations, but what about function calls? Is there a corresponding setting?

My .clang-format looks like this:

BasedOnStyle: Google
BinPackArguments: false
BinPackParameters: false
AllowAllParametersOfDeclarationOnNextLine: false
AlignAfterOpenBracket: AlwaysBreak

I'm also interested in preventing multiple arguments on the same line, but below the fun( line. In my case, I don't mind if the first argument is still on the same line as fun( . I just want them to either be all on the fun( line or all stacked.

I was able to achieve this by increasing PenaltyBreakBeforeFirstCallParameter to 100. Presumably you may need a different value depending on your other penalty settings.

This yields:

fun(my_long_argument_0,
    my_long_argument_1,
    my_long_argument_2);

In your case, assuming DoSomethingForAPurposeThatCausesALongFunctionName(arg_0, is too long for one line, it would yield:

DoSomethingForAPurposeThatCausesALongFunctionName(
    arg_0,
    arg_1,
    arg_2);

The following configuration is correct to get your desired formatting with clang-format 5.0.

AlignAfterOpenBracket: AlwaysBreak
AllowAllParametersOfDeclarationOnNextLine: false
BinPackParameters: false

The solution in the top voted answer from a similar question worked for me,

try setting:

BinPackArguments: false
BinPackParameters: false

And make sure

ExperimentalAutoDetectBinPacking: false
AllowAllParametersOfDeclarationOnNextLine: false

or not defined.

I think you need

AllowAllParametersOfDeclarationOnNextLine: true

It works for me unless the first argument is on the same line as the function call, in which case it puts them all on the same line.

I also have

ExperimentalAutoDetectBinPacking: false

which may have some effect?

I'm using version 3.8.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM