Community
Participate
Working Groups
The Java code formatter in Eclipse 4.3.2 aligns comments within switch statements in a way which I consider to be incorrect. This behavior persists even after I have unchecked both "Enable block comment formatting" and "Enable line comment formatting" in the Comments tab of the formatter options. Here's a simple example prior to the formatter: switch( item ) { // Item 1 was selected case 1: doSomething(); return true; // Item 2 was selected case 2: doSomethingElse(); return true; // Selected item was not 1 or 2 default: return false; } When the formatter runs, the comments are shifted: switch( item ) { // Item 1 was selected case 1: doSomething(); return true; // Item 2 was selected case 2: doSomethingElse(); return true; // Selected item was not 1 or 2 default: return false; } It seems like the formatter wants to align each comment with the preceding line, and I can't figure out how to make Eclipse leave these comments alone. I could use the @formatter:off tag to disable formatting of this section entirely, but that's not ideal because I still do want the formatter to run on this code. I've only noticed this behavior in switch statements, but in most other instances (where it doesn't do this) the comment is preceded by a blank line, so perhaps that is problem. I have confirmed that it happens consistently in every switch where I have commented the code in this manner. (If I have improperly categorized this bug then I apologize, please feel free to move it).
I just tested to see if adding a blank line before each comment would solve this, but it did not. The comments still get shifted. For example, this: switch( item ) { // Item 1 was selected case 1: doSomething(); return true; // Item 2 was selected case 2: doSomethingElse(); return true; // Selected item was not 1 or 2 default: return false; } Becomes this: switch( item ) { // Item 1 was selected case 1: doSomething(); return true; // Item 2 was selected case 2: doSomethingElse(); return true; // Selected item was not 1 or 2 default: return false; } So it seems the formatter will align the comment with the last non-blank line, if there is one. Again, I'm only seeing this behavior in switch() statements, but it happens consistently.
Okay, I was able to track down the apparent issue and partially solve this, but it still seems like this shouldn't happen. If I replace the 'return' with a 'break' in each case stanza, then the formatter keeps the comment for the next case where I want it to be. It still moves the first comment, though. So this: switch( item ) { // Item 1 was selected case 1: doSomething(); break; // Item 2 was selected case 2: doSomethingElse(); break; // Selected item was not 1 or 2 default: return false; } Becomes this: switch( item ) { // Item 1 was selected case 1: doSomething(); break; // Item 2 was selected case 2: doSomethingElse(); break; // Selected item was not 1 or 2 default: return false; } This seems like a strange behavior because 'return' and 'break' are both terminal and will halt the execution of the applicable case. I know that, in general, one should use 'break' in a switch-case block and then 'return' at the end of the parent function, but (IMHO) it's not blatantly incorrect to use 'return' in a case stanza, and it seems like the formatter should handle that. And, the formatter seems determined to change the alignment of that first comment regardless of what follows, and that's not desirable.
*** This bug has been marked as a duplicate of bug 124622 ***