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

2

16

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.

Gabbie answered 5/9, 2018 at 15:28 Comment(4)
ctrl+K+F is core VS functionality, it's unclear what tool you're talking about. I'd say, you should redirect it to other site (maybe VS tech support?)Tann
Related: #29973857Bidarka
I get confused between Ctrl + K +F (format) and Ctrl + K, F (close directory) all the time.Aiguillette
"C_Cpp.formatting": "vcFormat" solved this very issue to me, the pointers are placed close to the type, not close to the identifier.Darbie
G
11

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.

Gabbie answered 6/9, 2018 at 9:10 Comment(6)
It's funny that I use Chromium instead, but searched for this answer to override just those 2 settings (column limit and pointer alignment) :DSoonsooner
So I have {BasedOnStyle: Chromium, ColumnLimit: 150, PointerAlignment: Left}Soonsooner
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.Knoxville
@Knoxville 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.Rain
@Rain 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.Knoxville
When pasting functions using this answer (C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: WebKit, ColumnLimit: 120, PointerAlignment: Left}), such as copying the following with line breaks where the '\n' is (since StackOverflow doesn't allow line breaks in comments): void func(int& var) {\n return;\n } and pasting, VSCode seems to paste it all into one line: void func(int& var) { return; } Undoing once with "Ctrl+Z" fixes this after paste, but you must do it every timeAfferent
N
5

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

Nominee answered 5/9, 2018 at 15:43 Comment(1)
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.Gabbie

© 2022 - 2024 — McMap. All rights reserved.