How can I modify the code formatting for C++ in Visual Studio Code?

14,101

Solution 1

After some experiments, the simple solution is to add this line in the User Settings (settings.json):

"C_Cpp.clang_format_fallbackStyle": "{ PointerAlignment: Left}"

However, this settings allow me to keep my previous settings without breaking my function line:

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: WebKit, ColumnLimit: 120, PointerAlignment: Left}"

Using "BasedOnStyle: Visual Studio" such as this line:

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Visual Studio, ColumnLimit: 120, PointerAlignment: Left}"

doesn't work. It is may be a bug. I used Visual Studio Code version 1.26.1.

Additionally, a .clangformat outside the workspace folder will still be applied. So, if this file is corrupt the auto-format will not work.

Solution 2

I use clang-format, which integrates quite well and is very configurable. See https://code.visualstudio.com/docs/cpp/cpp-ide#_code-formatting

Share:
14,101

Related videos on Youtube

ywiyogo
Author by

ywiyogo

Updated on July 21, 2022

Comments

  • ywiyogo
    ywiyogo almost 2 years

    So far after installing the C++ extension tool, I can use Ctrl + K + F to auto-format my C++ code. However, I would like to make some modification, for example I would like to force the pointer alignment to be near the type, instead of next to the variable name, such as this rule:

    # Force pointers to the type for C++.
    DerivePointerAlignment: false
    PointerAlignment: Left
    

    How can I do this modification? I've tried to create a .clang-format file, but it doesn't work.

  • ywiyogo
    ywiyogo almost 6 years
    Thanks Michael, actually I've read the docs. Today I found the issue. I placed a .clangformat file outside the workspace (inside my Windows user folder), which contains a wrong format in a line. After I removed it or repaired the content it works again. I thought this file should not have effect. So the VS-Code not just look after the file inside the workspace.
  • Lukas Salich
    Lukas Salich over 4 years
    It's funny that I use Chromium instead, but searched for this answer to override just those 2 settings (column limit and pointer alignment) :D
  • Lukas Salich
    Lukas Salich over 4 years
    So I have {BasedOnStyle: Chromium, ColumnLimit: 150, PointerAlignment: Left}
  • Pulseczar
    Pulseczar over 3 years
    I don't understand why anyone would want type information, such as whether something is a pointer or not, to be grouped with the identifier. If class Student is a large structure, like 100,000 bytes in size, there is a big difference between a Student and a pointer to a Student, since pointers are generally 4 bytes or 8 bytes, depending on the architecture. And this is only taking into consideration the difference in size, not the fact that they are two very different things, conceptually.
  • lurker
    lurker about 3 years
    @Pulseczar I actually prefer the pointer indication to be near the identifier rather than the type because, syntactically, that's how the language treats it. If I have multiple identifiers identified with one type, int *p, n; declares p to be an int pointer and n to be an int. Writing it as int* p, n; seems visually misleading. I suppose it's subjective.
  • Pulseczar
    Pulseczar about 3 years
    @lurker That seems like a poor reason to split up type information. I wonder why the creators of C did it that way, to begin with. Thanks for the response.