If/else brace & block placement when formatting
Answered
I wish to enforce if/else blocks, specifically the else to be in the same line as the if's closing brace: closing 'if' brace, a space, the 'else', a space and the else's opening brace. When I execute code formatting on code that the else is on a new line, the editor leaves it on the new line instead of moving the else to the line of the if's closing brace.
Is there a way of achieving the formatter to format as described?
I'm looking for the formatter to produce:
if (condition) {
// ...
} else {
// ...
}
Instead of:
if (condition) {
// ...
}
else {
// ...
}
Please sign in to leave a comment.
Selecting the default code style and reformatting the code will produce the desired result.
This option needs to be disabled:
Thanks for your reply, Serge Baranov. I should have mentioned in my question above that my settings are already what you suggest. I have:
Force braces: Do not force
'else' on new line: (unchecked)
Special 'else if' treatment: (unchecked)
Still, whether I format the entire document or format a selection, it leaves the else on the next line.
For example, if I'm formatting:
if ( this.media.isActive('md') ){
this.closeBar();
this.deActivateFolded();
}
else
{
this.openBar();
this._detachBackdrop();
}
After formatting I get this
if (this.media.isActive('md')) {this.closeBar();
this.deActivateFolded();
}
else {
this.openBar();
this._detachBackdrop();
}
It only movies the else opening brace to the else line but not the else to the closing if brace line.
(FYI. This is typescript. The settings mentioned above are configured for TypeScript too.)
You should have mentioned that it's not Java from the start. Every language is formatted differently.
For JS it appears to be a known bug: https://youtrack.jetbrains.com/v2/issue/WEB-7308. Probably the same applies to TypeScript.