How can I modify the code formatting for C++ in Visual Studio Code?
Answer a question
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.
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.
更多推荐
所有评论(0)