Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No line break before comment after else in Eclipse

I've long been a "align curly braces" person. And with wide monitors I put long comments after code. So in Eclipse I want to do format things like this:

if(foo)
{
  something();
}
else
{
  somethingElse();
}

Fine; that's easy. But then I add a comment:

if(foo)  //if foo is true
{
  something();
}
else  //if no foo
{
  somethingElse();
}

Dratted Eclipse formats it like this:

if(foo)  //if foo is true
{
  something();
}
else
//if no foo
{
  somethingElse();
}

Great. That whole wasted line. And it looks ugly. And it's not even precise, because I want to comment the else line.

How can I get Eclipse to format my if/else with aligned braces, but allow my comments to be on the same line as else?

This was tested on Eclipse 4.2.2, and I have just confirmed that it does not work in Eclipse 4.3M7, either.

I have filed a bug on the issue; let's see what the Eclipse developers have to say.

like image 204
Garret Wilson Avatar asked Mar 23 '13 22:03

Garret Wilson


2 Answers

  1. Go to preferences
  2. Type "format" in the searchbox
  3. Select "Java->Code style->Formatter" in the tree
  4. Click the "Edit..." button
  5. Go to the "Comments" tab
  6. Select "preserve whitespace between code and line comments"

That did it for me in Galileo.

like image 199
Jurgen Vinju Avatar answered Sep 27 '22 21:09

Jurgen Vinju


I'm using Eclipse Indigo (3.7), all I had to do was go to Control Statements -> check "insert newline before 'else' in an 'if' statement". (inside Properties/Java Code Style/Formatter/Edit...)

I'm using a copy of the default Eclipse built-in template. If you're sure you haven't tampered with the built-in template, please let us know what version you're using.

like image 40
cleberz Avatar answered Sep 27 '22 23:09

cleberz