Vscode-cpptools: No option to indent namespace content / case labels

Created on 1 Aug 2017  路  7Comments  路  Source: microsoft/vscode-cpptools

Apparently not indenting code inside a namespace { } and case labels inside a switch () { } is a thing but I don't like it when everything is smushed to the left. The settings don't seem to allow changing that behaviour, though.
What I expect:

namespace Foo
{
    namespace Bar
    {
        void Baz(int x)
        {
            switch (x)
            {
                case 1:
                    // Code Here
                    break;
            }
        }
    }
}

What I get:

namespace Foo
{
namespace Bar
{
void Baz(int x)
{
    switch (x)
    {
    case 1:
        // Code Here
        break;
    }
}
}
}

The (what I think to be) relevant settings in my settings.json are:

"editor.autoIndent": true,
"C_Cpp.clang_format_path": null,
"C_Cpp.clang_format_style": "file",
"C_Cpp.clang_format_fallbackStyle": "Visual Studio",
"C_Cpp.formatting": "Default",

Most helpful comment

If you put a .clang-format file in the root of your project, then you can edit the formatting.

Here is the contents of mine, which does half of what you want:

UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
NamespaceIndentation: All
AccessModifierOffset: -4
PointerAlignment: Left

Specifically, the NamespaceIndentation: All is what you want for namespaces.

For switches, I think you can put IndentCaseLabels: true in your .clang-format file.

All 7 comments

If you put a .clang-format file in the root of your project, then you can edit the formatting.

Here is the contents of mine, which does half of what you want:

UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
NamespaceIndentation: All
AccessModifierOffset: -4
PointerAlignment: Left

Specifically, the NamespaceIndentation: All is what you want for namespaces.

For switches, I think you can put IndentCaseLabels: true in your .clang-format file.

You can find the rest of the options here

Yeah, @horvay is right if you're using the explicit format command (or indirectly via a format on save). If you're using some auto indent, then I'm unable to repro the behavior. Our extension doesn't provide any auto-indent capabilities currently.

Thank you very much. After some initial problems it worked flawlessly.
For future reference: I have downloaded LLVM 4.0.1 from this website: http://releases.llvm.org/download.html and added the lines "C_Cpp.clang_format_path": "C:\\Program Files\\LLVM\\bin\\clang-format.exe" and "C_Cpp.clang_format_style": "{UseTab: Never, IndentWidth: 4, ....... }" to my settings.json.
Using a .clang file did not work. This could be related to https://github.com/Microsoft/vscode-cpptools/issues/604 but I haven't tried a file called .clang-format. I prefer that to be in the global settings anyways so that's fine for me.

On a side note: The website you have linked says

BS_Allman (in configuration: Allman) Always break before braces.

But the example below that never has breaks before opening braces, which seems wrong, according to https://en.wikipedia.org/wiki/Indent_style#Allman_style

@Niko-O FYI, you shouldn't need to download clang-format.exe unless you want features in a newer version, because clang-format 4.0 is already installed along with our extension. The name of the settings file is .clang-format, not .clang, but using the explicit style is fine too. I don't understand what the issue is with Allman.

Right you are. Thank you again.
Regarding Allman:
Wikipedia says Allman should look like this:

if (foo())
{
}

But the linked page's example looks like this:

if (foo()) {
}

The automatic formatting produces the former so I'm happy.

Ah, I see. Yeah, the example of Allman at https://clang.llvm.org/docs/ClangFormatStyleOptions.html is incorrect.

Was this page helpful?
0 / 5 - 0 ratings