I've managed to get Visual Studio to keep the opening bracket on the same line like this (the settings for this are under Formatting -> New Lines
; the settings I am looking for, if they exist, are not in this area of options):
void foo() {
bar();
}
but I can't figure out how to keep else and catch blocks on the same line as the ending bracket of the previous statement.
To clarify, this is what Visual Studio auto-formats to:
if (foo) {
return 1;
}
else {
return 2;
}
and this is what I want:
if (foo) {
return 1;
} else {
return 2;
}
2.7 Curly BracesAn opening curly brace should never go on its own line and should always be followed by a new line. A closing curly brace should always go on its own line, unless it's followed by else. Always indent the code, 2 spaces, inside curly braces.
Go to Tool
->Option
then General
->Text editor
->C++
-> Formatting
-> New lines
.
Choose the following settings:
The preview for this last setting will display
if (a < b)
{
} else
{
}
but this doesn't take into account the first option.
If you type an if/else statement, select the region, and ask the editor to reformat. The statement will appear as:
if (test) {
} else {
}
I could try this successfully on Visual Studio 2015. I tested it with a foreign language, so the english wording of above mentioned options might be slightly different, but close enough for being found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With