microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

Wrong or unchanged indentation when initializing structs. #7669

Open DD-Diana opened 3 years ago

DD-Diana commented 3 years ago

Bug type: Language Service

With VSCode 1.56 (April 2021) & cpptools 1.4.1

There are problems with the indentations of struct initializers.

typedef struct thing {
    int a;
    int b;
} thing;

thing get_thing() {

    /* In these situations: indentation is unchanged */
    thing a = {
            10, // wrong indentation not corrected
    10 // wrong indentation not corrected
    };
    thing t = {
                .a = 10, // wrong indentation not corrected
    .b = 10 // wrong indentation not corrected
    };
    t = (thing){
        .a = 10, // had to manually indent those ones
        .b = 10 // indentation is unchanged by formatter
    };

    /* In these situations: indentation is worng */
    return (thing) {
        .a = 10, // good
            .b = 10 // wrong indentation formatted
    };
}

Configuration:

"C_Cpp.formatting": "vcFormat"
"C_Cpp.vcFormat.indent.caseLabels": true
sean-mcmanus commented 3 years ago

I've reported bug https://developercommunity.visualstudio.com/t/cc-formatting-doesnt-work-well-with-struct-initial/1446139 against our shared formatter code.

FYI, the () around the types is not valid standard C++ and doesn't compile with the cl.exe compiler (it seems to be a gcc/clang extension)...that doesn't appear to affect formatting though.

DD-Diana commented 3 years ago

Yes, the () around the types is C99, see compound literal.

sean-mcmanus commented 3 years ago

Okay, that makes sense now.

FYI, you may also want to consider changing C_Cpp.formatting to "clangFormat" instead of "vcFormat" -- vcFormat may not support C formatting as well.