Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i configure clang format without .clang-format file in every workspace?

I want to configure clang-format without having to copy my .clang-format file to every new workspace.

In my settings.json I currently have

"C_Cpp.clang_format_style": "{BasedOnStyle: Google, IndentWidth: 4, IndentCaseLabels: false, TabWidth: 4, UseTab: ForIndentation, ColumnLimit: 0}",
"C_Cpp.clang_format_fallbackStyle": "Google"

The description for C_Cpp.clang_format_style says

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, e.g.: "{ BasedOnStyle: LLVM, IndentWidth: 8 }"

Which made me think my approach would work, which it doesn't. When I use the auto format it always uses the Google fallbackStyle.

Is this just not possible or am I doing something wrong here?

like image 553
Christoph S. Avatar asked Nov 02 '16 12:11

Christoph S.


2 Answers

I just had the same problem and found that your approach using (I have changed only the formatting)

"C_Cpp.clang_format_style": "{BasedOnStyle: Google, 
IndentWidth: 4, 
IndentCaseLabels: false, 
TabWidth: 4, UseTab: ForIndentation, 
ColumnLimit: 0}",

does work.

like image 53
joelfischerr Avatar answered Sep 23 '22 11:09

joelfischerr


The documentation for clang-format -style=file is somewhat misleading.

You need to define the following in your settings.json (either workspace or global)

{
   "C_Cpp.clang_format_style": "file",
}

As file is not the path to the file you want but rather tells clang-format to use the .clang-format file it can find within the local path - going all the way up (cd .. until it can find one).

If the case is the VSCode C++ extension own clang-format it's in the VSCode extensions folder.

Which should make the clang-format to go cd .. until it finds.clang-format` in your code folder (if one exists).

like image 34
Shahar Hadas Avatar answered Sep 23 '22 11:09

Shahar Hadas