Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 110059

Summary: [ast rewrite] strange formatting after change to enhanced for loop
Product: [Eclipse Project] JDT Reporter: Martin Aeschlimann <martinae>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: deepakazad, markus.kell.r, satyam.kandula
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug

Description Martin Aeschlimann CLA 2005-09-20 12:52:05 EDT
20050920-0010

code in attachment 26800 [details] results in

	public void endTest(Test test) throws CloneNotSupportedException {
		for (TestListener nextElement : cloneListeners()) {
nextElement.endTest(test);
}
	}
Comment 1 Markus Keller CLA 2011-09-13 13:03:12 EDT
HEAD, probably a similar problem as bug 350285

- new workspace
- paste into Package Explorer:

package test1;

import java.util.Iterator;
import java.util.List;

public class E {
    public void m(List<? extends Number> nums) {
        for (Iterator<? extends Number> iterator = nums.iterator(); iterator
                .hasNext();) {
            Number number = iterator.next();
            System.out.println(number.doubleValue());
            // comment
            System.out.println(number.doubleValue());
        }
    }
}

- format the whole CU (using the default profile)
- put caret into 'for'
- apply quick assist "Convert to enhanced 'for' loop"

=> result has non-indented body statements:

public class E {
    public void m(List<? extends Number> nums) {
        for (Number number : nums) {
System.out.println(number.doubleValue());
// comment
System.out.println(number.doubleValue());
}
    }
}

The problem does not occur when the entire "for (...) {" header is on a single line.

In ConvertIterableLoopOperation#convert(..) line 314, we set the body of the new EnhancedForStatement to the result of ASTRewrite#createMoveTarget(..). I suspect the ASTRewrite gets confused because the indentation of the starting line of the block is larger than the indentation of the body statements.
Comment 2 Satyam Kandula CLA 2011-10-04 01:22:17 EDT
In the original string, the line where the blocks starts
has an indentation of 4. In the modified string, the line where the block
starts has an indentation of 2. Thus, the indentation logic thinks that the
code needs to be moved left by 2 and so the problem.
Comment 3 Satyam Kandula CLA 2011-10-10 06:37:27 EDT
(In reply to comment #2)
Test case provided at bug 350285 comment 12 fails for the same reason as this.
Comment 4 Eclipse Genie CLA 2020-03-31 04:01:18 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.