0

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?

pinkk-pig
  • 1
  • 1
  • 1
  • 1
    The tool uses clang-format. If you want it to behave differently, you'll need to create a `.clang-format` file and configure it to your liking and place it in the root of your project. – sweenish Nov 19 '20 at 15:53

3 Answers3

2

You need to have C/C++ extension(just check if you have it). Original

1st idea

  1. Go File-> Preferences -> Settings
  2. Search for C_Cpp.clang_format_fallbackStyle
  3. Click Edit, Copy to Settings
  4. Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"

(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

PredragDj
  • 313
  • 1
  • 3
  • 10
  • 2
    Literally the first thing they say is that they are using the C/C++ extension. And you're suggesting an indent-width setting, they are talking about a line break setting. – sweenish Nov 19 '20 at 16:14
0

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.

sweenish
  • 4,793
  • 3
  • 12
  • 23
0

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 }"