Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep else, catch, on same line as closing bracket in Visual Studio 2015?

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;
}
like image 465
MisterEman22 Avatar asked Aug 12 '15 22:08

MisterEman22


People also ask

Should curly braces be on their own line?

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.


1 Answers

Go to Tool->Option then General->Text editor->C++ -> Formatting -> New lines.

Choose the following settings:

  • Position of opening brace for control block: radio button for same line
  • Position of keyword: uncheck the else on new line.

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.

like image 157
Christophe Avatar answered Oct 19 '22 05:10

Christophe