c++clang-format

clang-format: How to never align with any token from the previous line?


I want to achieve that the length of a variable/function/etc does never influence the indent of the following lines, or at least as rarely as possible. For example, I already have AlignAfterOpenBracket: AlwaysBreak which moves all parameters to the next line if e.g. a function name is too long:

const auto result = SomeVeryLooooooonFunctionName(
    parameter1,
    parameter2,
    parameter3,
    parameter4,
    parameter5,
    parameter6,
    parameter7);

Nice: When anything in line one changes, the next lines won't be affected at all (unless the whole call becomes so short that it fits into one line, but that's OK).

But, when I immediately add (or any other binary operation) something, it becomes undesired:

const auto result = SomeVeryLooooooonFunctionName(
                        parameter1,
                        parameter2,
                        parameter3,
                        parameter4,
                        parameter5,
                        parameter6) +
    1;

It looks like the parameters are now aligned with the function name (plus ContinuationIndentWidth: 4). I would be ok with the big indent (though it's ugly imo), but my issue is that now, when I e.g. change the name of result, all following lines will change their indent, which is exactly what I don't want.

This would be desirable:

const auto result =
    SomeVeryLooooooonFunctionName(
        parameter1,
        parameter2,
        parameter3,
        parameter4,
        parameter5,
        parameter6) +
    1;

But really, anything is fine as long as changing a name in line one doesn't cause all following lines to re-indent.

This is my .clang-format file:

---
Language: Cpp
BasedOnStyle: Mozilla

AlignAfterOpenBracket: AlwaysBreak
AlignOperands: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
PenaltyReturnTypeOnItsOwnLine: 0
ColumnLimit: 100
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
IndentWidth: 2
...

Solution

  • I found a way that works. I added PenaltyIndentedWhitespace: 10 and now I get exactly what I wanted:

    const auto result =
        SomeVeryLooooooonFunctionName(
            parameter1,
            parameter2,
            parameter3,
            parameter4,
            parameter5,
            parameter6) +
        1;