Bug 328362 - [formatter] Format regions does not format as expected
Summary: [formatter] Format regions does not format as expected
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M4   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-21 10:44 EDT by Dani Megert CLA
Modified: 2010-11-29 04:20 EST (History)
2 users (show)

See Also:


Attachments
Proposed patch (2.17 KB, patch)
2010-11-26 11:42 EST, Frederic Fusier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.