13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ywiyogo
  • 738
  • 2
  • 7
  • 21
  • 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?) – Swift - Friday Pie Sep 05 '18 at 15:32
  • Related: https://stackoverflow.com/questions/29973357/how-do-you-format-code-in-visual-studio-code-vscode – handle Oct 18 '19 at 07:17
  • I get confused between `Ctrl` + `K` +`F` (format) and `Ctrl` + `K`, `F` (close directory) all the time. – nomad Apr 18 '21 at 12:54
  • 1
    `"C_Cpp.formatting": "vcFormat"` solved this very issue to me, the pointers are placed close to the type, not close to the identifier. – dandev486 Feb 09 '22 at 23:47

2 Answers2

8

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ywiyogo
  • 738
  • 2
  • 7
  • 21
  • 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 Sep 29 '19 at 00:20
  • So I have {BasedOnStyle: Chromium, ColumnLimit: 150, PointerAlignment: Left} – Lukas Salich Sep 29 '19 at 00:27
  • 1
    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. – Pulseczar Mar 12 '21 at 18:41
  • @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. – lurker Apr 26 '21 at 22:35
  • @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. – Pulseczar Apr 28 '21 at 20:05
  • 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 time – CreativiTimothy Mar 30 '23 at 18:57
4

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

handle
  • 5,859
  • 3
  • 54
  • 82
Michael Surette
  • 701
  • 1
  • 4
  • 12
  • 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. – ywiyogo Sep 06 '18 at 08:46