Bug 328362

Summary: [formatter] Format regions does not format as expected
Product: [Eclipse Project] JDT Reporter: Dani Megert <daniel_megert>
Component: CoreAssignee: Frederic Fusier <frederic_fusier>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Olivier_Thomann, rthakkar
Version: 3.7   
Target Milestone: 3.7 M4   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Proposed patch none

Description Dani Megert CLA 2010-10-21 10:44:22 EDT
N20101021-2000.

Format regions does not format as expected.

Test Case 1:
1. paste the following snippet into Package Explorer
package test1;

    class  A {

        int  i;

}
2. enable to format edited lines on save
3. delete the space before "A {"
4. delete the space before "i;"
5. save
==> "class A {" is not aligned at column 1

Test Case 2:
public void testFormatFieldDeclWithExtraWhitespace() throws Exception {
	StringBuffer buf= new StringBuffer();
	buf.append("package test1;\n");
	buf.append("    class A {\n");
	buf.append("        int i;\n");
	buf.append("}\n");

	String contents= buf.toString();
	String formatString1= "    class A {";
	String formatString2= "        int i;";

	IRegion[] regions= new Region[] { new Region(contents.indexOf(formatString1), formatString1.length()), new Region(contents.indexOf(formatString2), formatString2.length()) };
	TextEdit edit= ToolFactory.createCodeFormatter(null, ToolFactory.M_FORMAT_EXISTING).format((CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS), contents, regions, 0, "\n");
	assertNotNull(edit);
	Document doc= new Document(contents);
	edit.apply(doc);
	String formatted= doc.get();

	buf= new StringBuffer();
	buf.append("package test1;\n");
	buf.append("class A {\n");
	buf.append("    int i;\n");
	buf.append("}\n");

	String expected= buf.toString();
	assertEqualString(formatted, expected);
}
Comment 1 Frederic Fusier CLA 2010-11-26 11:42:21 EST
Created attachment 183937 [details]
Proposed patch

This was just a small glitch while adapting edits at region edges: when there's no spaces before the region and nothing to replace (i.e. all new lines before the region already matches those that the edit string contains), then the replacement string needed to be flushed...
Comment 2 Frederic Fusier CLA 2010-11-26 11:53:13 EST
Released for 3.7M4 in HEAD stream.
Comment 3 Dani Megert CLA 2010-11-29 04:20:10 EST
Verified in N20101128-2000 and HEAD.