Search code examples
c++clang-format

how to format nested parentheses using .clang-format


I want to obtain the following code to create json object:

nlohmann::json obj = {
    {key1, "value 1"},
    {key2, 22}
};

Instead of

nlohmann::json obj = {{key1, "value 1"}, {key2, 22}};

Is it possible to do that using .clang-format settings?


Solution

  • Something similar can be achieved with AlignArrayOfStructures: Left:

    nlohmann::json obj = {
        {key1, "value 1"},
        {key2, 22       }
    };
    

    You could also disable clang-format for those lines and format them manually:

    // clang-format off
    nlohmann::json obj = {
        {key1, "value 1"},
        {key2, 22}
    };
    // clang-format on