Clang-format indents inner initialisers with continuation indent
Asked Answered
G

0

6

I have some C++11 code with clang-format rules that uses an normal indent (IndentWidth) of 4 and continuation indent (ContinuationIndentWidth) of 8. So a long function looks something like this:

// All indented "correctly"
void this_function_has_a_very_long_name_and_lots_of_parameters(
        int parameter_a, int parameter_b, int parameter_c)
{
    this_function_has_a_very_long_name_and_lots_of_parameters(
            parameter_a, parameter_b, parameter_c);
}

However, I also have data like this (note the final commas to prevent bin-packing and keep the items one per line - in real life these numbers are not just 1, 2, 3):

static std::vector<std::vector<int>> data{
    {
        1, // Comment
        2,
        3, // Comment
    },
};

The above is how I expect it to look, considering that Cpp11BracedListStyle is set to false, so it should use the block indent (4), not the continuation indent (8). From the clang-format docs, if this is true:

"Important differences: - No spaces inside the braced list. - No line break before the closing brace. - Indentation with the continuation indent, not with the block indent."

So, I expected to see the "block indent" (4) used. However, what I actually get is:

static std::vector<std::vector<int>> data{
    {
           1, // Comment - indented by 4 + 8!
           2,
           3, // Comment
    },
};

As you can see, the "inner" initialiser list is indented by 8, but the outer list's elements are only indented by 4 (as expected).

If I change Cpp11BracedListStyle to true, all levels are indented by 8 (as expected according the the documentation):

static std::vector<std::vector<int>> data{
        {
                1,
                2,
                3,
        },
};

How can I get clang-format to format these lists without changing the 8-space continuation indent that I use elsewhere in the code?

N.B. clang-format version is 7.0.1 (tags/RELEASE_701/final), but this behaviour seems to be the same in various versions according to the configurator.

A simple .clang-format file that applies the relevant rules as they are currently is:

BasedOnStyle: LLVM
BreakBeforeBraces: Allman
ContinuationIndentWidth: 8
ColumnLimit: 72
Cpp11BracedListStyle: false
IndentWidth: 4
Guiltless answered 10/1, 2019 at 11:26 Comment(1)
This might be better suited to the relevant dev mailing list.Boggess

© 2022 - 2024 — McMap. All rights reserved.