Reputation: 1
I'm using the C++ Extension for VSCode (Visual Studio Code).
This format's my code when I save my C++ file. But the format results in if-statement and curly braces on same lines rather than on the new line.(I have already set format Curly Braces on Same Line)
C++ VSCode Formatted
if(...){
//...
}else if(...)
//...
}else{
//...
}
What I Want C++ VSCode Formatted Code to Look Like
if(...){
//...
}
else if(...){
//...
}
else{
//...
}
How can I make If-else in C++ format on the differernt line in Visual Studio Code?
Upvotes: 0
Views: 4403
Reputation: 1
I use this clang format and it works with me:
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false, BreakBeforeBraces: Custom, BraceWrapping: { BeforeElse: true } }
The part that fix the problem is "BreakBeforeBraces: Custom, BraceWrapping: { BeforeElse: true }"
Upvotes: 0
Reputation: 323
You need to have C/C++ extension(just check if you have it). Original
1st idea
(Change from "Visual Studio" to "LLVM", "Google" or "WebKit")
Something like this:
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
Also check documentation: here and another one
2nd idea
Install C# FixFormat extension
View > Extension
Search "C# FixFormat"
Install
Shift + Alt + F
If it complains about multiple formatters, then press the Configure button and select C# FixFormat.
It is possible to go back to having open braces on a new line by going to File > Preferences > Settings. Then scroll down to Extensions, C# FixFormat configuration and uncheck Style > Braces: On Same Line
Upvotes: 3
Reputation: 5202
The VS Code extension uses a program called clang-format to format your code. You can change how clang-format behaves by placing a .clang-format
file in your project root and double-checking that the default style is 'file' in VS Code's settings. The setting is C_Cpp: Clang_format_style
.
Here is a minimal .clang-format
that gets as close to your desired output as clang-format allows.
BreakBeforeBraces: Stroustrup
IndentWidth: 4
SpaceBeforeParens: Never
It cannot remove the space before the opening brace, but I'm personally on clang-format's side on that decision. You will naturally want to peruse the documentation, https://clang.llvm.org/docs/ClangFormatStyleOptions.html, to see all the options available to you.
Upvotes: 1