简体   繁体   中英

Set Visual Studio Code path to .clang_format file

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?

Unfortunately, it seems that this is not possible at the moment. The extension look for the .clang-format file only in the project directory.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM