Bug 378139 - [quick assist] actions on if .. else statement consume comments before the else block
Summary: [quick assist] actions on if .. else statement consume comments before the el...
Status: CLOSED DUPLICATE of bug 250142
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7.2   Edit
Hardware: PC Windows 7
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 106371 367866 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-05-01 03:34 EDT by Lukas Eder CLA
Modified: 2012-08-03 11:52 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Eder CLA 2012-05-01 03:34:06 EDT
Build Identifier: Build Identifier: Version: Indigo Release Build id: M20120208-0800

When using the quick-fix actions on an if .. else statement, some comments are consumed and disappear.

Reproducible: Always

Steps to Reproduce:
1. Create this class

--------------------------------------
public class Test {
    public void x() {
        boolean condition = (1 == 0);

        // If-comment
        if (condition) {
            System.out.println(true);
        }

        // Else-comment
        else {
            System.out.println(false);
        }
    }
}
--------------------------------------

2. Put the cursor on "if"
3. Click Ctrl-1
4. Choose any of the quick fix options:
   - Invert 'if' statement
   - Change 'else' block to statement
   - Change 'if' block to statement
   - Change 'if-else' blocks to statements
5. The outcome is the following (using Invert 'if' statement):

--------------------------------------
        // If-comment
        if (!condition) {
            System.out.println(false);
        }
        else {
            System.out.println(true);
        }
--------------------------------------

The "Else-comment" has gone. While it is hard to find a better solution for "Invert 'if' statement" (as the comments would have to switch, too), the right solution is easy to see for the other actions.

One could argue that the "If-comment" and "Else-comment" could go inside the respective blocks, but I think think this is a matter of taste. The way I usually do it (comments before keyword) seems to be quite popular. Some feedback to a blog post of mine:

http://www.javacodegeeks.com/2012/01/if-else-coding-style-best-practices.html
http://blog.jooq.org/2012/01/18/if-else-coding-style-best-practices/ (original)
Comment 1 Deepak Azad CLA 2012-05-03 04:40:45 EDT
*** Bug 106371 has been marked as a duplicate of this bug. ***
Comment 2 Deepak Azad CLA 2012-05-03 04:40:57 EDT
*** Bug 367866 has been marked as a duplicate of this bug. ***
Comment 3 Deepak Azad CLA 2012-05-03 04:42:33 EDT
(In reply to comment #1)
> *** Bug 106371 has been marked as a duplicate of this bug. ***

(In reply to comment #2)
> *** Bug 367866 has been marked as a duplicate of this bug. ***

While the quick assists mentioned in these bugs are different, it is best to deal with all these issues in one go.
Comment 4 Christian Ullenboom CLA 2012-07-24 06:01:06 EDT
Also see https://bugs.eclipse.org/bugs/show_bug.cgi?id=385478
Comment 5 Markus Keller CLA 2012-08-03 11:52:28 EDT

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