Reputation: 939
How do I tell vs code where to look for the .clang_format file? I have this file in location which is not the root of my vscode project. Clang_format_path is apparently not what I need, because it specifies the path to the clang executable:
C_Cpp: Clang_format_path The full path of the clang-format executable. If not specified, and clang-format is available in the environment path, that is used. If not found in the environment path, a copy of clang-format bundled with the extension will be used
Then there is Clang_format_style.
C_Cpp: Clang_format_style Coding style, currently supports: Visual Studio, LLVM, Google, Chromium, Mozilla, WebKit. Use "file" to load the style from a .clang-format file in the current or parent directory. Use {key: value, ...} to set specific parameters. For example, the "Visual Studio" style is similar to: { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
I can set it to file but how do I specify where to look for the file?
Upvotes: 6
Views: 25829
Reputation: 137
Seems like this is the setting:
"C_Cpp.clang_format_style": "file : ./clang_format"
Give it the relative or absolute path from the settings.json
or the xyz.code-workspace
in which the settings are specified.
Source: https://github.com/microsoft/vscode-cpptools/issues/10445#issuecomment-1411249767
From Intellisense:
Coding style, currently supports: Visual Studio, LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU. Use file to load the style from a .clang-format file in the current or parent directory, or use file:/.clang-format to reference a specific path. Use {key: value, ...} to set specific parameters. For example, the Visual Studio style is similar to: { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }.
Upvotes: 2
Reputation: 53
I was looking for the same, however the C/CPP extension by microsoft was unable to do that.
You need to download install Clang which comes bundled with the LLVM https://releases.llvm.org/download.html (Choose the one for your system under the Pre-Built Binaries)
While installing it will ask you to add LLVM to the path directory. If you dont want to add it manually then you have to add it to vscode settings.json later.
Install clang-format extension in VS code first - https://marketplace.visualstudio.com/items?itemName=xaver.clang-format
After installation set clang-format as your default formatter
Place your .clang-format file in the opened project directory
Then head to your settings.json and add this line
"clang-format.language.cpp.style": "file"
(Skip this step if you have already added LLVM to path) Inside settings.json add this line replacing the adress with your LLVM(clang) path
"clang-format.executable": "/absolute/path/to/clang-format"
Now, the extension will automatically search for the .clang-format file in the active directory.
You have to paste the .clang-format file everytime you change your active directories. There is no other way to define the path to .clang-format directly in the settings.
For more info on path setting see the answer here How do I specify a clang-format file?
Upvotes: 4
Reputation: 2185
Unfortunately, it seems that this is not possible at the moment. The extension look for the .clang-format file only in the project directory.
Upvotes: 4