Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 609
ywiyogo Avatar asked Sep 05 '18 15:09

ywiyogo


2 Answers

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.

like image 173
ywiyogo Avatar answered Oct 10 '22 14:10

ywiyogo


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

like image 42
Michael Surette Avatar answered Oct 10 '22 13:10

Michael Surette