Set Visual Studio Code path to .clang_format file - c++

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?

Related

Can you point clang-format to a .clang-format file that is in a sub-dir? [duplicate]

I cannot understand the following text from the clang-format manual:
.clang-format file located in one of the parent
directories of the source file (or current
directory for stdin).
What does "current directory for stdin" mean? Is it the current directory or the stdin?
Is it possible to specify the .clang-format location by a full path?
It is the current directory.
And there is no way to set .clang-format path. clang-format only scans from the directories and up until it encounters the file.
Concerning VSCode I found this feature request:
https://github.com/microsoft/vscode-cpptools/issues/3675
So it is currently not supported to specify the .clang-format location by a full path.

Is there a better way to set C++ format options in VS Code instead of packing them into one long string?

I am using Clang C++ on macOS in VS Code and I have overridden some C++ formatting options in 'settings.json' file. I am able to set some things from the Settings UI, but for more detailed settings I have added the following entry in 'settings.json':
"C_Cpp.clang_format_fallbackStyle": "{ EmptyLineBeforeAccessModifier: Never, AllowShortCaseLabelsOnASingleLine: true, ConstructorInitializerIndentWidth: 2, ColumnLimit: 120, PointerAlignment: Left, AllowShortIfStatementsOnASingleLine: true, SpaceAfterTemplateKeyword: false, AlignOperands: false, ContinuationIndentWidth: 2 }"
I found descriptions of these settings at Clang Format Style Options
It seems to work properly and my C++ files get nicely reformatted when I hit Alt-F, but I was wondering if there was a nicer way to specify the settings instead of using one long string in settings.json.
For example, it would be nice if I could specify each format setting on a different line or split up the string somehow into multiple lines. I don't think splitting a string into multiple lines can be done in regular JSON files, but perhaps VS code has a way?
I'm not sure if it's relevant, but I have set 'C_Cpp: Clang_format_fallback Style' to 'Google' in the Setting UI.
Any suggestions would be greatly appreciated.
Based on a comment from Frank I tried using .clang-format file and after some messing around I got it to work. I first tried putting .clang-format inside the .vscode directory in my project, but that didn't work. After reading move about clang format I moved the file to my home directory (note the file format for .clang-format is YAML instead of JSON) since it needs to be in the same directory or a parent directory of the file being formatted.
I don't think it was required, but I also ended up running brew install clang-format since I couldn't find it in my path. Once that was installed I ran clang-format -style google -dump-config>~/.clang-format and then made the overrides I wanted.
I was able to remove the long fallback string from settings.json and VS Code is still formatting nicely using my style overrides in my new .clang-format file.

clang-format: Use custom style if file doesn't exist

I want to format code using a custom style if .clang-format doesn't exist. Is there any way to do so?
I used --fallback-style but it fails.
clang-format -i --style=file --fallback-style="{BasedOnStyle: google, IndentWidth: 4, ColumnLimit: 100}" a.cpp
It raises an error:
Invalid fallback style "{BasedOnStyle: google, IndentWidth: 4, ColumnLimit: 100}
I doubt there is a command line option to use another style if .clang_format file doesn't exist.
The option -fallback-style does work only with predefined styles, which are llvm, chromium, mozilla, google, webkit, gnu, microsoft. Also there is none to skip formatiing. You can use -fallback-style=google for example.
However I would suggest other ways. Here is how clang-format finds files:
it checks for .clang-format and _clang-format files in all directories from the one which contains a file you want to format, then the parent of this directory, then the parent of this, and so on to the root directory. So there are two options:
Manually check if configuration file for clang-format exists.
Put your custom fallback style in root or home directory, so that clang-format finds your file in case it can't find it in a project

clang-format Visual Studio plugin and cmake

I've installed the latest clang-format plugin for Visual Studio from http://llvm.org/builds/ and I'm trying to use it with a VS solution file that's generated from CMake. I've got the following directory structure:
./
├──project/
│ ├──.clang-format
│ └──source.cpp #all sourcefiles are here
└──build/ # cmake build files
When I open the cmake-generated .sln file and press CTRL+R, CTRL+F, no formatting happens. When I set Tools->Options->LLVM/Clang->ClangFormat->Fallback Style to anything (e.g. 'LLVM') then it formats (to the default style), but not if I set fallback to 'none' - which means my .clang-format file doesn't get loaded.
I tried copying the .clang-format file all over the project directories, i.e. putting it in the project dir, sub-directories, next to the source files, in the build directory, in the build/x64 directory, etc. - but it just doesn't get picked up.
Where do I have to put the file so that it gets picked up by the plugin?
I solved this, and it had nothing to do with where you put the .clang-format file. It's fine to just put it in the root of the source directory, as I did it.
Why it didn't format at all was that a .clang-format requires either the line BasedOnStyle: LLVM (or any other default style) or to define all required parameters.
If you just have a .clang-format file with a few lines, like for example:
---
ColumnLimit: '120'
IndentWidth: '4'
Language: Cpp
Standard: Cpp11
UseTab: Never
...
then it will not do any formatting whatsoever. Probably it can't work without a full set of rules, and in this case, it doesn't know to which default settings whatsoever it should fall back to.

How to feed Visual Studio Clang-Format plugin with clang-format file?

So I downloaded, installed, and inserted into path the clang formatting plugin. I also tested it and it works for Google (Mozilla, etc.) formatting options out of the box, yet I cannot get it working with my .clang-format file. (I've put my file into the same folder as my source file, changed its encoding into UTF-8, also tried to put it into clang install folder, add file into project, write its contents inside '{key:value}' yet formatting does not happen). So how do you feed formatting file to chrome-format extension?
My file contents:
{ BasedOnStyle: "LLVM", IndentWidth: 4 }
My file name:nm.clang-format
Go to Tools->Options->LLVM/Clang->ClangFormat and put file in the Style option field.
Then place your style file named .clang-format (this is the full filename, not an extension) either in the source file's directory or one of its parent directories. Windows Explorer won't let you create filenames with leading . so you need to go to the console for this.
If like me you got confused later on where the .clang-format was living, use procmon to track the file reads of clang-format.exe
For the record, it seems that if both "Fallback Style" and "Style" are set to "file", no formatting will happen even if the style file is at its correct location. Setting "Fallback Style" to something different than "file" (e.g. "none") helps.
In VS2019 works if the clang-format file is named as .clang-format.
It must be .clang-format, not .clang-format.txt or clang-format.txt.