Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 437223 - Formatter performs an undesired realignment of comments within switch statements
Summary: Formatter performs an undesired realignment of comments within switch statements
Status: CLOSED DUPLICATE of bug 124622
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.3.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-11 14:35 EDT by David Parker CLA
Modified: 2014-06-13 06:49 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Parker CLA 2014-06-11 14:35:00 EDT
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).
Comment 1 David Parker CLA 2014-06-12 11:19:04 EDT
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.
Comment 2 David Parker CLA 2014-06-12 11:33:26 EDT
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.
Comment 3 Dani Megert CLA 2014-06-13 06:49:32 EDT

*** This bug has been marked as a duplicate of bug 124622 ***