Use clang-format to format your C/C++/Java/JavaScript/Objective-C/Objective-C++/Protobuf with indentations of 4 spaces like the following:
if (int i = 0; i < n; ++i) {
while (true) {
foo();
}
} else {
baz();
}
The extension is forked from Xaver Hellauer's Repo and modified to use clang-format with indent 4. More usage details can be found in the original repo.
Install clang-format use one of the following methods:
npm install -g clang-format # NPM
sudo apt install clang-format # Ubuntu Linux
winget install -e --id LLVM.LLVM # Windows
Obtain the absolute path to clang-format:
which clang-format
Add the following to your vscode settings.json
file (open with Ctrl/CMD+Shift+P
and select Preferences: Open User Settings (JSON)
):
{
"clang-format.executable": "/absolute/path/to/clang-format",
}
To automatically format a file on save, add the following to your vscode settings.json
file (example for C++):
{
"editor.formatOnSave": true,
"[cpp]": {
"editor.defaultFormatter": "demiaochen.clang-format-indent-4",
}
}
If you want to use a specific clang-format config file, add the following to your vscode settings.json
file, and create a .clang-format
file in the root of your project:
{
"clang-format.style": "file",
}
Clang-Format is a tool to format C/C++/Java/JavaScript/Objective-C/Objective-C++/Protobuf code. It can be configured with a config file named .clang-format
within the working folder or a parent folder. Configuration see: http://clang.llvm.org/docs/ClangFormatStyleOptions.html
Available on github: https://github.com/demiaochen/vscode-clang-format-indent-4