This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 262164

Summary: Bad formatting of if-statement
Product: java Reporter: Peter Nabbefeld <epdv>
Component: EditorAssignee: Dusan Balek <dbalek>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.1   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:

Description Peter Nabbefeld 2016-05-20 12:25:40 UTC
Formatting of an if-statement like this does not work correctly:

if (condition_1) {
    // Comment-1
    doSomething(1);
} else {
    // Comment-2
    if (condition_2) {
        doSomething(2);
    } else {
        doSomething(3);
    }
}

Is formatted as:

if (condition_1) {
    // Comment-1
    doSomething(1);
} else // Comment-2
if (condition_2) {
    doSomething(2);
} else {
    doSomething(3);
}

As "Comment-2" is behind the "else", it doesn't look like a comment for the following if-statement, but as a comment for the else-statement.

Instead, formatting should not change anything in this special case (the block following the else contains a comment in the very first line). However, I really like building a switch-like if-else structure, when there is no comment in the first line of the block following else, like this:

if (condition_1) {
    // Comment-1
    doSomething(1);
} else {
    if (condition_2) {
        doSomething(2);
    } else {
        doSomething(3);
    }
}

being formatted to:

if (condition_1) {
    // Comment-1
    doSomething(1);
} else if (condition_2) {
    doSomething(2);
} else {
    doSomething(3);
}

as this does not change source code semantics.