问题
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?
回答1:
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.
回答2:
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).
来源:https://stackoverflow.com/questions/40380136/how-can-i-configure-clang-format-without-clang-format-file-in-every-workspace