简体   繁体   中英

Eclipse formatter alignment of comments in switch statement

I've been experimenting with the code formatter in Eclipse 4.3.2 and have everything set up pretty much the way I want it. However, Eclipse continues to align comments incorrectly within switch statements, 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:

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 the first comment with the opening brace of the switch and make each subsequent comment a part of the preceding case block. But that's not what I want, and I can't seem to 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'm particularly confused as to why the formatter is realigning comments at all, given that I unchecked the options to do so.

Do all line/block comments shift like this or does it happen only with switch body? Either way, this looks like a bug in the formatter to me, did you check bugs.eclipse.org for a bug similar to this?

I have discovered that using break instead of return solves the problem, except for the very first comment which still gets aligned with the opening brace of the switch statement.

I filed a bug report with Eclipse but it was marked a duplicate. It turns out there was already an Eclipse bug report for this:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=124622

The bug report was filed in 2006, last commented on in 2008, and the bug remains in Eclipse some 6 years and 7 versions later. Guess they just don't care.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM