### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java,v retrieving revision 1.25 diff -u -r1.25 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 21 Aug 2008 08:53:21 -0000 1.25 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 29 Aug 2008 11:33:34 -0000 @@ -84,7 +84,7 @@ formatUnit("bugs.b230944", "X01.java"); } public void testBug230944b() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b230944", "X02.java"); } @@ -97,7 +97,7 @@ formatUnit("bugs.b231263", "BadFormattingSample.java"); } public void testBug231263a() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b231263", "X.java"); } @@ -107,29 +107,29 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=231297" */ public void testBug231297() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b231297", "X.java"); } public void testBug231297a() throws JavaModelException { - this.preferences.comment_line_length = 30; + this.formatterPrefs.comment_line_length = 30; formatUnit("bugs.b231297", "X01.java"); } public void testBug231297b() throws JavaModelException { // Difference with old formatter: // 1) fixed non formatted inline tag description - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b231297", "X02.java"); } public void testBug231297c() throws JavaModelException { // Difference with old formatter: // 1) fixed non formatted inline tag description - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b231297", "X03.java"); } public void testBug231297d() throws JavaModelException { // Difference with old formatter: // 1) fixed non formatted inline tag description - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b231297", "X03b.java"); } @@ -139,43 +139,43 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=232285" */ public void testBug232285a() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01.java"); } public void testBug232285b() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01b.java"); } public void testBug232285c() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01c.java"); } public void testBug232285d() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01d.java"); } public void testBug232285e() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01e.java"); } public void testBug232285f() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X01f.java"); } public void testBug232285g() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X02.java"); } public void testBug232285h() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X03.java"); } public void testBug232285i() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X04.java"); } public void testBug232285j() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232285", "X04b.java"); } @@ -185,7 +185,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=232488" */ public void testBug232488() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232488", "X01.java"); } @@ -195,11 +195,11 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=232466" */ public void testBug232466a() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232466", "X01.java"); } public void testBug232466b() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b232466", "X02.java"); } @@ -218,11 +218,10 @@ "\r\n#]" + "}\r\n" + ""; - // TODO fix the incorrect indentation before the javadoc comment (also in 3.3 and 3.4M6) formatSource(source, "public class A {\n" + "\n" + - " /*\n" + + " /*\n" + " * A block comment on two lines\n" + " */\n" + "\n" + @@ -247,7 +246,7 @@ " public void foo() {\n" + " }\n" + "\n" + - " /*\n" + + " /*\r\n" + " * A block comment \n" + " * on two lines\n" + " */\n" + @@ -272,17 +271,15 @@ "#]\n" + "\n" + "}"; - // TODO fix the incorrect indentation before the javadoc comment (also in 3.3 and 3.4M6) formatSource(source, "public class C {\n" + " \n" + - " /**\n" + + " /**\n" + " * a b c d .\n" + " */\n" + " void m1() {\n" + "\n" + - " } \n" + - "\n" + + " }\n" + "\n" + "}" ); @@ -313,7 +310,7 @@ " * a b c d .\n" + " */\n" + " void m1() {\n" + - " \n" + + "\n" + " } \n" + "\n" + "\n" + @@ -370,11 +367,10 @@ "\n" + "\n" + "}"; - // TODO fix the incorrect indentation before the javadoc comment (also in 3.3 and 3.4M6) formatSource(source, "public class C {\n" + - " \n" + - " /**\n" + + "\n" + + " /**\n" + " * a b c d .\n" + " */\n" + " void m1 ( ) {\n" + @@ -468,17 +464,15 @@ "#]\n" + "\n" + "}"; - // TODO fix the incorrect indentation before the block comment (also in 3.3 and 3.4M6) formatSource(source, "public class D {\n" + " \n" + - " /*\n" + + " /*\n" + " * a b c d .\n" + " */\n" + " void m2() {\n" + "\n" + - " } \n" + - "\n" + + " }\n" + "\n" + "}" ); @@ -509,7 +503,7 @@ " * a b c d .\n" + " */\n" + " void m2() {\n" + - " \n" + + "\n" + " } \n" + "\n" + "\n" + @@ -566,11 +560,10 @@ "\n" + "\n" + "}"; - // TODO fix the incorrect indentation before the block comment (also in 3.3 and 3.4M6) formatSource(source, "public class D {\n" + - " \n" + - " /*\n" + + "\n" + + " /*\n" + " * a b c d .\n" + " */\n" + " void m2 ( ) {\n" + @@ -657,13 +650,11 @@ " }\n" + "#] \n" + "}"; - // TODO fix the incorrect indentation before the method declaration (also in 3.3 and 3.4M6) - // Note that the line comment wasn't formatted using 3.3 and 3.4 M6 formatSource(source, "public class E {\n" + " \n" + "\n" + - " void m3() { // this is a bug\n" + + " void m3() { // this is a bug\n" + "\n" + " }\n" + " \n" + @@ -707,7 +698,7 @@ "public class E {\n" + " \n" + "\n" + - " void m3() { // this is a bug\n" + + " void m3() { // this is a bug\n" + "\n" + " }\n" + "\n" + @@ -750,7 +741,7 @@ "public class E { \n" + " \n" + "\n" + - " void m3() { // this is a bug\n" + + " void m3() { // this is a bug\n" + "\n" + " }\n" + " \n" + @@ -772,7 +763,7 @@ "public class E { \n" + " \n" + "\n" + - " void m3() { // this is a bug\n" + + " void m3() { // this is a bug\n" + "\n" + " }\n" + " \n" + @@ -796,7 +787,7 @@ " \n" + "\n" + " void m3() { \n" + - " // this is a bug\n" + + " // this is a bug\n" + "\n" + " }\n" + " \n" + @@ -832,63 +823,63 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=232788" */ public void testBug232788_Tabs01() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X01_tabs.java"); } public void testBug232788_Spaces01() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.SPACE; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X01_spaces.java"); } public void testBug232788_Mixed01() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.MIXED; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X01_mixed.java"); } public void testBug232788_Tabs02() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_size = 0; - this.preferences.indentation_size = 0; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_size = 0; + this.formatterPrefs.indentation_size = 0; formatUnit("bugs.b232788", "X02_tabs.java"); } public void testBug232788_Spaces02() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.SPACE; - this.preferences.tab_size = 0; - this.preferences.indentation_size = 0; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.tab_size = 0; + this.formatterPrefs.indentation_size = 0; formatUnit("bugs.b232788", "X02_spaces.java"); } public void testBug232788_Mixed02() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.MIXED; - this.preferences.tab_size = 0; - this.preferences.indentation_size = 0; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + this.formatterPrefs.tab_size = 0; + this.formatterPrefs.indentation_size = 0; formatUnit("bugs.b232788", "X02_mixed.java"); } public void testBug232788_Tabs03() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X03_tabs.java"); } public void testBug232788_Spaces03() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.SPACE; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X03_spaces.java"); } public void testBug232788_Mixed03() throws JavaModelException { - this.preferences.comment_line_length = 40; - this.preferences.tab_char = DefaultCodeFormatterOptions.MIXED; - this.preferences.tab_size = 1; - this.preferences.indentation_size = 1; + this.formatterPrefs.comment_line_length = 40; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + this.formatterPrefs.tab_size = 1; + this.formatterPrefs.indentation_size = 1; formatUnit("bugs.b232788", "X03_mixed.java"); } @@ -908,7 +899,7 @@ " * block [# #] \n" + " * comment\n" + " */\n" + - " // single [# #] line comment\n" + + " // [#single line#] comment\n" + "}"; formatSource(source, "\n" + @@ -916,10 +907,10 @@ " /**\n" + " * Javadoc comment\n" + " */\n" + - " /*\n" + + " /*\n" + " * block comment\n" + " */\n" + - " // single line comment\n" + + " // single line comment\n" + "}" ); } @@ -951,7 +942,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=233224" */ public void testBug233224() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("bugs.b233224", "X01.java"); } @@ -961,7 +952,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=233259" */ public void testBug233259a() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; String source = "public class X {\n" + " /**\n" + @@ -981,7 +972,7 @@ ); } public void testBug233259b() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; // difference with 3.3 formatter: // split html reference as this allow not to go over the max line width String source = @@ -1004,7 +995,7 @@ ); } public void testBug233259c() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; String source = "public class X {\n" + " /**\n" + @@ -1025,7 +1016,7 @@ ); } public void testBug233259d() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; String source = "public class X {\n" + " /**\n" + @@ -1052,7 +1043,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=237942" */ public void testBug237942a() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; String source = "public class X {\n" + " /**\n" + @@ -1072,7 +1063,7 @@ ); } public void testBug237942b() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; // difference with 3.3 formatter: // do not split string reference as this can lead to javadoc syntax error String source = @@ -1094,7 +1085,7 @@ ); } public void testBug237942c() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; // difference with 3.3 formatter: // do not split string reference as this can lead to javadoc syntax error String source = @@ -1116,7 +1107,7 @@ ); } public void testBug237942d() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; // difference with 3.3 formatter: // do not split string reference as this can lead to javadoc syntax error String source = @@ -1162,6 +1153,317 @@ ); } +//static { TESTS_PREFIX = "testBug234583"; } +/** + * @bug 234583: [formatter] Code formatter should adapt edits instead of regions + * @test Ensure that selected region(s) are correctly formatted + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583" + */ +public void testBug234583a() throws JavaModelException { + String source = + "public class X {\n" + + "[# int i= 1; #]\n" + + "}\n"; + formatSource(source, + "public class X {\n" + + " int i = 1;\n" + + "}\n" + ); +} +public void testBug234583b() throws JavaModelException { + String source = + "public class X { \n" + + "\n" + + "\n" + + "\n" + + "[# int i= 1; #]\n" + + "\n" + + "\n" + + "\n" + + "\n" + + " }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + "\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "\n" + + "\n" + + "\n" + + " }\n" + ); +} +public void testBug234583c() throws JavaModelException { + String source = + "public class X { \n" + + "\n" + + "\n" + + "\n" + + "[# int i= 1; \n" + + "#]\n" + + "\n" + + "\n" + + "\n" + + " }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + "\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "\n" + + "\n" + + " }\n" + ); +} +public void testBug234583d() throws JavaModelException { + String source = + "public class X { \n" + + "\n" + + "\n" + + "[#\n" + + " int i= 1; \n" + + "\n" + + "#]\n" + + "\n" + + "\n" + + " }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "\n" + + " }\n" + ); +} +public void testBug234583e() throws JavaModelException { + String source = + "public class X { \n" + + "\n" + + "[#\n" + + "\n" + + " int i= 1; \n" + + "\n" + + "\n" + + "#]\n" + + "\n" + + " }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + " int i = 1;\n" + + "\n" + + " }\n" + ); +} +public void testBug234583f() throws JavaModelException { + String source = + "public class X { \n" + + "[#\n" + + "\n" + + "\n" + + " int i= 1; \n" + + "\n" + + "\n" + + "\n" + + "#]\n" + + " }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + " int i = 1;\n" + + "\n" + + " }\n" + ); +} +public void testBug234583g() throws JavaModelException { + String source = + "public class X { [#\n" + + "\n" + + "\n" + + "\n" + + " int i= 1; \n" + + "\n" + + "\n" + + "\n" + + "\n" + + "#] }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + " int i = 1;\n" + + "\n" + + " }\n" + ); +} +public void testBug234583h() throws JavaModelException { + String source = + "public class X { [# \n" + + "\n" + + "\n" + + "\n" + + " int i= 1; \n" + + "\n" + + "\n" + + "\n" + + "\n" + + " #] }\n"; + formatSource(source, + "public class X { \n" + + "\n" + + " int i = 1;\n" + + "\n" + + " }\n" + ); +} +public void testBug234583i() throws JavaModelException { + String source = + "public class X {[# \n" + + "\n" + + "\n" + + "\n" + + " int i= 1; \n" + + "\n" + + "\n" + + "\n" + + "\n" + + " #]}\n"; + formatSource(source, + "public class X {\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "}\n" + ); +} +// duplicate https://bugs.eclipse.org/bugs/show_bug.cgi?id=239447 +public void testBug234583_Bug239447() throws JavaModelException { + String source = + "public class Bug239447 {\n" + + " private static final String CONTENT = \"test.ObjectB {\\n\"\n" + + "[# + \" multiEle = { name=\\\"Foo\\\" }\\n\"#]\n" + + " + \" multiEle = :x { name=\\\"Bar\\\" }\\n\" + \" singleEle = x;\\n\"\n" + + " + \"}\";\n" + + "\n" + + "}\n"; + formatSource(source, + "public class Bug239447 {\n" + + " private static final String CONTENT = \"test.ObjectB {\\n\"\n" + + " + \" multiEle = { name=\\\"Foo\\\" }\\n\"\n" + + " + \" multiEle = :x { name=\\\"Bar\\\" }\\n\" + \" singleEle = x;\\n\"\n" + + " + \"}\";\n" + + "\n" + + "}\n" + ); +} +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=237453 +public void testBug234583_Bug237453a() throws JavaModelException { + String source = + "package test1;\n" + + "\n" + + "public class E1 {\n" + + " void foo() {\n" + + " }\n" + + " [#\n" + + " #]\n" + + " void bar() {\n" + + " }\n" + + "}"; + formatSource(source, + "package test1;\n" + + "\n" + + "public class E1 {\n" + + " void foo() {\n" + + " }\n" + + " \n" + + " void bar() {\n" + + " }\n" + + "}" + ); +} +public void testBug234583_Bug237453b() throws JavaModelException { + String source = + "package test1;\n" + + "\n" + + "public class E1 {\n" + + " void foo() {\n" + + " }\n" + + " \n" + + "[# #]\n" + + " void bar() {\n" + + " }\n" + + "}"; + formatSource(source, + "package test1;\n" + + "\n" + + "public class E1 {\n" + + " void foo() {\n" + + " }\n" + + " \n" + + " \n" + + " void bar() {\n" + + " }\n" + + "}" + ); +} +// duplicate https://bugs.eclipse.org/bugs/show_bug.cgi?id=237592 +public void testBug234583_Bug237592() throws JavaModelException { + String source = + "package test;\n" + + "\n" + + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " }\n" + + "\n" + + "[# #]\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "[# #]\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " void bar() {\n" + + " }\n" + + "\n" + + "}\n"; + formatSource(source, + "package test;\n" + + "\n" + + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " }\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " void bar() {\n" + + " }\n" + + "\n" + + "}\n" + ); +} + /** * @bug 236230: [formatter] SIOOBE while formatting a compilation unit. * @test Ensure that no exception occurs while formatting @@ -1239,7 +1541,7 @@ ); } public void testBug236230c() throws JavaModelException { - this.preferences.comment_format_header = true; + this.formatterPrefs.comment_format_header = true; String source = "/**\n" + " * Need a javadoc comment before to get the exception.\n" + @@ -1493,7 +1795,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=238090" */ public void testBug238090() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; String source = "package test.bugs;\n" + "public class LongNameClass {\n" + @@ -1964,7 +2266,7 @@ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=241345" */ public void testBug241345() throws JavaModelException { - this.preferences.comment_format_html = false; + this.formatterPrefs.comment_format_html = false; String source = "/**\n" + " *

Should not format HTML paragraph

\n" + Index: src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java,v retrieving revision 1.234 diff -u -r1.234 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 19 Aug 2008 01:01:29 -0000 1.234 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 29 Aug 2008 11:33:37 -0000 @@ -54,6 +54,8 @@ private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private long time; + DefaultCodeFormatterOptions formatterPrefs; + static { // TESTS_NUMBERS = new int[] { 715 }; // TESTS_RANGE = new int[] { 715, -1 }; @@ -147,6 +149,14 @@ return org.eclipse.jdt.internal.core.util.Util.editedString(source, edit); } + + /** + * Init formatter preferences with Eclipse default settings. + */ + protected void setUp() throws Exception { + super.setUp(); + this.formatterPrefs = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); + } /** * Create project and set the jar placeholder. @@ -203,6 +213,69 @@ assertSourceEquals("Different number of length", Util.convertToIndependantLineDelimiter(expectedContents), actualContents); } + DefaultCodeFormatter codeFormatter() { + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs, JAVA_PROJECT.getOptions(true)); + return codeFormatter; + } + + void assertLineEquals(String actualContents, String originalSource, String expectedContents) { + String outputSource = expectedContents == null ? originalSource : expectedContents; + assertLineEquals(actualContents, originalSource, outputSource, false /* do not check null */); + } + + void formatSource(String source, String formattedOutput) { + formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/); + } + + void formatSource(String source, String formattedOutput, boolean repeat) { + formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, repeat); + } + + void formatSource(String source, String formattedOutput, int kind, int indentationLevel, boolean repeat) { + int regionStart = source.indexOf("[#"); + if (regionStart != -1) { + IRegion[] regions = new Region[10]; + int idx = 0; + int start = 0; + int delta = 0; + StringBuffer buffer = new StringBuffer(); + while (regionStart != -1) { + buffer.append(source.substring(start, regionStart)); + int regionEnd = source.indexOf("#]", regionStart+2); + buffer.append(source.substring(regionStart+2, regionEnd)); + regions[idx++] = new Region(regionStart-delta, regionEnd-(regionStart+2)); + delta += 4; + start = regionEnd + 2; + regionStart = source.indexOf("[#", start); + } + buffer.append(source.substring(start, source.length())); + String newSource = buffer.toString(); + String result; + if (idx == 1) { + // Use offset and length until bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=233967 is fixed + result = runFormatter(codeFormatter(), newSource, kind, indentationLevel, regions[0].getOffset(), regions[0].getLength(), LINE_SEPARATOR, repeat); + } else { + System.arraycopy(regions, 0, regions = new Region[idx], 0, idx); + result = runFormatter(codeFormatter(), newSource, kind, indentationLevel, regions, LINE_SEPARATOR); + } + assertLineEquals(result, newSource, formattedOutput); + } else { + formatSource(source, formattedOutput, kind, indentationLevel, false, 0, -1, null, repeat); + } + } + + void formatSource(String source, String formattedOutput, int kind, int indentationLevel, boolean checkNull, int offset, int length, String lineSeparator, boolean repeat) { + DefaultCodeFormatter codeFormatter = codeFormatter(); + String result; + if (length == -1) { + result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, source.length(), lineSeparator, repeat); + } else { + result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, length, lineSeparator, repeat); + } + assertLineEquals(result, source, formattedOutput); + } + + private void runTest(String packageName, String compilationUnitName) { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; @@ -9456,6 +9529,7 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=203304 public void test671() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); @@ -9464,6 +9538,27 @@ new Region(101, 20) }; runTest(codeFormatter, "test671", "A.java", CodeFormatter.K_COMPILATION_UNIT, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + " public static void main(String[] args) {\n" + + "[# int a = 1;#]\n" + + " int b = 2;\n" + + "[# int c = 3;#]\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class A {\n" + + " public static void main(String[] args) {\n" + + " int a = 1;\n" + + " int b = 2;\n" + + " int c = 3;\n" + + " }\n" + + "}\n", + CodeFormatter.K_COMPILATION_UNIT, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=203304 @@ -9492,6 +9587,7 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=203304 public void test674() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); @@ -9500,6 +9596,64 @@ new Region(276, 54) }; runTest(codeFormatter, "test674", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + " \n" + + " \n" + + " private class Inner1 {[#\n" + + " \n" + + " \n" + + " void bar () { }\n" + + " \n" + + " void i()\n" + + " {\n" + + " \n" + + " }\n" + + " #]}\n" + + " \n" + + " \n" + + " private class Inner2 {\n" + + " void xy() {\n" + + " \n" + + " }\n" + + " }\n" + + "}\n" + + "class B {[#\n" + + " private void foo() {\n" + + " \n" + + " }\n" + + "#]}\n"; + formatSource(source, + "public class A {\n" + + " \n" + + " \n" + + " private class Inner1 {\n" + + "\n" + + " void bar() {\n" + + " }\n" + + "\n" + + " void i() {\n" + + "\n" + + " }\n" + + " }\n" + + " \n" + + " \n" + + " private class Inner2 {\n" + + " void xy() {\n" + + " \n" + + " }\n" + + " }\n" + + "}\n" + + "class B {\n" + + " private void foo() {\n" + + "\n" + + " }\n" + + "}\n", + CodeFormatter.K_CLASS_BODY_DECLARATIONS, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=203304 @@ -9631,6 +9785,7 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 public void test685() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); @@ -9638,6 +9793,22 @@ new Region(18, 35) }; runTest(codeFormatter, "test685", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + " [# int i=1; #] \n" + + "}\n"; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "public class A {\n" + + " int i = 1; \n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 @@ -9661,6 +9832,7 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 public void test688a() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); @@ -9668,9 +9840,26 @@ new Region(18, 48) }; runTest(codeFormatter, "test688", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + " [# int i=1; \n" + + "}#]\n"; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "public class A {\n" + + " int i = 1;\n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } public void test688b() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); @@ -9678,6 +9867,19 @@ new Region(18, 49) }; runTest(codeFormatter, "test688", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + " [# int i=1; \n" + + "}\n#]"; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "public class A {\n" + + " int i = 1;\n" + + "}\n" + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 @@ -9694,6 +9896,7 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 public void test690() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.line_separator = "\r";//$NON-NLS-1$ @@ -9702,10 +9905,32 @@ new Region(31, 23) }; runTest(codeFormatter, "test689", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\r");//$NON-NLS-1$ //$NON-NLS-2$ + */ + this.formatterPrefs.line_separator = "\r";//$NON-NLS-1$ + String source = + "package pkg1;\n" + + "public class A {\n" + + "[# int i = 1; #]\n" + + "\n" + + "}\n"; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "package pkg1;\n" + + "public class A {\n" + + " int i = 1;\n" + + "\n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 public void test691() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.line_separator = "\r\n";//$NON-NLS-1$ @@ -9714,6 +9939,27 @@ new Region(31, 22) }; runTest(codeFormatter, "test689", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\r\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + this.formatterPrefs.line_separator = "\r\n";//$NON-NLS-1$ + String source = + "package pkg1;\n" + + "public class A {\n" + + "[# int i = 1; #] \n" + + "\n" + + "}\n"; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "package pkg1;\n" + + "public class A {\n" + + " int i = 1; \n" + + "\n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 @@ -9810,6 +10056,7 @@ // variation on bugs 208541, 213283, 213284 public void test697a() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.line_separator = "\n";//$NON-NLS-1$ @@ -9818,10 +10065,38 @@ new Region(17, 55) // end of line selection }; runTest(codeFormatter, "test697", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + "[# \n" + + " \n" + + " \n" + + " int i = 1; #]\n" + + "\n" + + "\n" + + "\n" + + "}\n" + + ""; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "public class A {\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "\n" + + "\n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // variation on bugs 208541, 213283, 213284 public void test697b() { + /* old version final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.line_separator = "\n";//$NON-NLS-1$ @@ -9830,6 +10105,32 @@ new Region(17, 56) // end of line selection + 1 }; runTest(codeFormatter, "test697", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + */ + String source = + "public class A {\n" + + "[# \n" + + " \n" + + " \n" + + " int i = 1; \n" + + "#]\n" + + "\n" + + "\n" + + "}\n" + + ""; + // Note that whitespaces outside the region are kept after the formatting + // This is intentional since fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583 + // The formatter should not touch code outside the given region(s)... + formatSource(source, + "public class A {\n" + + "\n" + + " int i = 1;\n" + + "\n" + + "\n" + + "}\n", + CodeFormatter.K_UNKNOWN, + 0 /*no indentation*/, + true /*repeat formatting twice*/ + ); } // variation on bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 @@ -10128,9 +10429,32 @@ String resourcePath = getResource("test713", "formatter.xml"); Map options = DecodeCodeFormatterPreferences.decodeCodeFormatterOptions(resourcePath, "Dani"); assertNotNull("No preferences", options); + /* old version DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test713", "A.java", CodeFormatter.K_COMPILATION_UNIT, 0, false, 76, 27);//$NON-NLS-1$ //$NON-NLS-2$ + */ + this.formatterPrefs = new DefaultCodeFormatterOptions(options); + String source = + "package pack;\n" + + "\n" + + "public class A {\n" + + " /**\n" + + " * @see A.Inner\n" + + " */\n" + + "[# public class Inner { }\n" + + "#]}"; + formatSource(source, + "package pack;\n" + + "\n" + + "public class A {\n" + + " /**\n" + + " * @see A.Inner\n" + + " */\n" + + " public class Inner {\n" + + " }\n" + + "}" + ); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780 Index: src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsClearBlankLinesTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsClearBlankLinesTests.java,v retrieving revision 1.4 diff -u -r1.4 FormatterCommentsClearBlankLinesTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsClearBlankLinesTests.java 15 May 2008 17:45:45 -0000 1.4 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsClearBlankLinesTests.java 29 Aug 2008 11:33:34 -0000 @@ -51,10 +51,10 @@ } DefaultCodeFormatter codeFormatter() { - this.preferences.comment_clear_blank_lines_in_block_comment = true; - this.preferences.comment_clear_blank_lines_in_javadoc_comment = true; - this.preferences.comment_format_header = true; - DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences); + this.formatterPrefs.comment_clear_blank_lines_in_block_comment = true; + this.formatterPrefs.comment_clear_blank_lines_in_javadoc_comment = true; + this.formatterPrefs.comment_format_header = true; + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs); return codeFormatter; } Index: src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsTests.java,v retrieving revision 1.4 diff -u -r1.4 FormatterJavadocDontIndentTagsTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsTests.java 15 May 2008 17:45:45 -0000 1.4 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsTests.java 29 Aug 2008 11:33:34 -0000 @@ -50,8 +50,8 @@ } DefaultCodeFormatter codeFormatter() { - this.preferences.comment_indent_root_tags = false; // Eclipse default is true - DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences); + this.formatterPrefs.comment_indent_root_tags = false; // Eclipse default is true + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs); return codeFormatter; } Index: src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java,v retrieving revision 1.23 diff -u -r1.23 FormatterCommentsTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java 1 Jul 2008 15:11:02 -0000 1.23 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java 29 Aug 2008 11:33:34 -0000 @@ -38,9 +38,6 @@ import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil; import org.eclipse.jdt.internal.core.util.SimpleDocument; import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; -import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.Region; /** * Javadoc formatter test suite using the Eclipse default settings. @@ -59,8 +56,6 @@ public static List ALL_TEST_SUITES = null; - DefaultCodeFormatterOptions preferences; - public static Test suite() { return buildModelTestSuite(FormatterCommentsTests.class); } @@ -68,10 +63,6 @@ public FormatterCommentsTests(String name) { super(name); } -protected void setUp() throws Exception { - super.setUp(); - this.preferences = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); -} /** * Create project and set the jar placeholder. @@ -97,71 +88,6 @@ } } -DefaultCodeFormatter codeFormatter() { - DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences, JAVA_PROJECT.getOptions(true)); - return codeFormatter; -} - -/* (non-Javadoc) - * @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#assertLineEquals(java.lang.String, java.lang.String, java.lang.String, boolean) - */ -void assertLineEquals(String actualContents, String originalSource, String expectedContents, boolean checkNull) { - String outputSource = expectedContents == null ? originalSource : expectedContents; - super.assertLineEquals(actualContents, originalSource, outputSource, checkNull); -} - -void formatSource(String source, String formattedOutput) { - formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/); -} - -void formatSource(String source, String formattedOutput, boolean repeat) { - formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, repeat); -} - -void formatSource(String source, String formattedOutput, int kind, int indentationLevel, boolean repeat) { - int regionStart = source.indexOf("[#"); - if (regionStart != -1) { - IRegion[] regions = new Region[10]; - int idx = 0; - int start = 0; - int delta = 0; - StringBuffer buffer = new StringBuffer(); - while (regionStart != -1) { - buffer.append(source.substring(start, regionStart)); - int regionEnd = source.indexOf("#]", regionStart+2); - buffer.append(source.substring(regionStart+2, regionEnd)); - regions[idx++] = new Region(regionStart-delta, regionEnd-(regionStart+2)); - delta += 4; - start = regionEnd + 2; - regionStart = source.indexOf("[#", start); - } - buffer.append(source.substring(start, source.length())); - String newSource = buffer.toString(); - String result; - if (idx == 1) { - // Use offset and length until bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=233967 is fixed - result = runFormatter(codeFormatter(), newSource, kind, indentationLevel, regions[0].getOffset(), regions[0].getLength(), Util.LINE_SEPARATOR, repeat); - } else { - System.arraycopy(regions, 0, regions = new Region[idx], 0, idx); - result = runFormatter(codeFormatter(), newSource, kind, indentationLevel, regions, Util.LINE_SEPARATOR); - } - assertLineEquals(result, newSource, formattedOutput, false); - } else { - formatSource(source, formattedOutput, kind, indentationLevel, false, 0, -1, null, repeat); - } -} - -void formatSource(String source, String formattedOutput, int kind, int indentationLevel, boolean checkNull, int offset, int length, String lineSeparator, boolean repeat) { - DefaultCodeFormatter codeFormatter = codeFormatter(); - String result; - if (length == -1) { - result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, source.length(), lineSeparator, repeat); - } else { - result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, length, lineSeparator, repeat); - } - assertLineEquals(result, source, formattedOutput, checkNull); -} - void compareFormattedSource(ICompilationUnit compilationUnit) throws JavaModelException { DefaultCodeFormatter codeFormatter = codeFormatter(); String source = compilationUnit.getSource(); @@ -849,7 +775,7 @@ } // TODO Pass this test public void _testLineComments11() throws JavaModelException { - this.preferences.comment_line_length = 40; + this.formatterPrefs.comment_line_length = 40; formatUnit("comments.line", "X11.java"); } Index: src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsDescriptionTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsDescriptionTests.java,v retrieving revision 1.4 diff -u -r1.4 FormatterJavadocDontIndentTagsDescriptionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsDescriptionTests.java 15 May 2008 17:45:44 -0000 1.4 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterJavadocDontIndentTagsDescriptionTests.java 29 Aug 2008 11:33:34 -0000 @@ -50,8 +50,8 @@ } DefaultCodeFormatter codeFormatter() { - this.preferences.comment_indent_parameter_description = false; // Eclipse default is true - DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences); + this.formatterPrefs.comment_indent_parameter_description = false; // Eclipse default is true + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs); return codeFormatter; } #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v retrieving revision 1.149 diff -u -r1.149 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 21 Aug 2008 08:53:23 -0000 1.149 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 29 Aug 2008 11:33:39 -0000 @@ -165,26 +165,31 @@ if (index >= 0) { // the offset of the region is inside a comment => restart the region from the comment start adaptedOffset = this.commentPositions[index][0]; - if (adaptedOffset < 0) adaptedOffset = -adaptedOffset; - adaptedLength = length + offset - adaptedOffset; - commentIndex = index; - // include also the indentation edit just before the comment if any - for (int j=0; j 0 && this.edits[j].replacement.trim().length() == 0) { - adaptedLength += adaptedOffset - this.edits[j].offset; - adaptedOffset = editOffset; + if (adaptedOffset >= 0) { + // adapt only javadoc or block commments. Since fix for bug + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=238210 + // edits in line comments only concerns whitespaces hence can be + // treated as edits in code + adaptedLength = length + offset - adaptedOffset; + commentIndex = index; + // include also the indentation edit just before the comment if any + for (int j=0; j 0 && this.edits[j].replacement.trim().length() == 0) { + adaptedLength += adaptedOffset - this.edits[j].offset; + adaptedOffset = editOffset; + break; + } + } else if (editEnd > adaptedOffset) { break; } - } else if (editEnd > adaptedOffset) { - break; } } } index = getCommentIndex(commentIndex, offset+length-1); - if (index >= 0) { + if (index >= 0 && this.commentPositions[index][0] >= 0) { // only javadoc or block comment // the region end is inside a comment => set the region end at the comment end int commentEnd = this.commentPositions[index][1]; if (commentEnd < 0) commentEnd = -commentEnd; @@ -194,68 +199,207 @@ if (adaptedLength != length) { // adapt the region and jump to next one this.adaptedRegions[i] = new Region(adaptedOffset, adaptedLength); - continue; + } else { + this.adaptedRegions[i] = aRegion; } + } + } - if (offset > 0) { - if (isAdaptableRegion(offset, length)) { - // if we have a selection, search for overlapping edits - int upperBound = offset; - int lowerBound = 0; - boolean upperFound = false; - int regionEnd = offset + length; - for (int j = 0, max2 = this.editsIndex - 1; j <= max2; j++) { - // search for lower bound - int editOffset = this.edits[j].offset; - if (upperFound && lowerBound == 0) { - int editLength = this.edits[j].length; - if (editOffset == regionEnd) { // matching edit found - lowerBound = regionEnd; - break; - } else if (editOffset + editLength < regionEnd) { - continue; - } else { - lowerBound = editOffset + editLength; // upper and lower bounds found - break; - } - // search for upper bound - } else { - int next = j+1; - if (next == max2) { - // https://bugs.eclipse.org/bugs/show_bug.cgi?id=213284 - // checked all edits, no upper bound found: leave the loop - break; - } - if (this.edits[next].offset < offset) { - continue; + /* + * Adapt edits to regions. + * + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583" + * for more details + */ + private void adaptEdits() { + + // See if adapting edits is really necessary + int max = this.regions.length; + if (max == 1) { + if (this.regions[0].getOffset() == 0 && this.regions[0].getLength() == this.scannerEndPosition) { + // No need to adapt as the regions covers the whole source + return; + } + } + + // Sort edits + OptimizedReplaceEdit[] sortedEdits = new OptimizedReplaceEdit[this.editsIndex]; + System.arraycopy(this.edits, 0, sortedEdits, 0, this.editsIndex); + Arrays.sort(sortedEdits, new Comparator() { + public int compare(Object o1, Object o2) { + OptimizedReplaceEdit edit1 = (OptimizedReplaceEdit) o1; + OptimizedReplaceEdit edit2 = (OptimizedReplaceEdit) o2; + return edit1.offset - edit2.offset; + } + }); + + // Adapt overlapping edits + int currentEdit = -1; + for (int i = 0; i < max; i++) { + IRegion region = this.adaptedRegions[i]; + int offset = region.getOffset(); + int length = region.getLength(); + + // modify overlapping edits on the region (if any) + int index = adaptEdit(sortedEdits, currentEdit, offset, offset+length); + if (index != -1) { + currentEdit = index; + } + } + } + + /* + * Search whether a region overlap edit(s) at its start and/or at its end. + * If so, modify the concerned edits to keep only the modifications which are + * inside the given region. + * + * The edit modification is done as follow: + * 1) start it from the region start if it overlaps the region's start + * 2) end it at the region end if it overlaps the region's end + * 3) remove from the replacement string the number of lines which are outside + * the region: before when overlapping region's start and after when overlapping + * region's end. Note that the trailing indentation of the replacement string is not + * kept when the region's end is overlapped because it's always outside the + * region. + */ + private int adaptEdit(OptimizedReplaceEdit[] sortedEdits, int start, int regionStart, int regionEnd) { + int bottom = start==-1?0:start, top = sortedEdits.length - 1; + int topEnd = top; + int i = 0; + OptimizedReplaceEdit edit = null; + int overlapIndex = -1; + int linesOutside= -1; + + // Look for an edit overlapping the region start + while (bottom <= top) { + i = bottom + (top - bottom) /2; + edit = sortedEdits[i]; + int editStart = edit.offset; + int editEnd = editStart + edit.length; + if (regionStart < editStart) { // the edit starts after the region's start => no possible overlap of region's start + top = i-1; + if (regionEnd < editStart) { // the edit starts after the region's end => no possible overlap of region's end + topEnd = top; + } + } else { + if (regionStart >= editEnd) { // the edit ends before the region's start => no possible overlap of region's start + bottom = i+1; + } else { + // Count the lines of the edit which are outside the region + linesOutside = 0; + this.scanner.resetTo(editStart, editEnd-1); + while (!this.scanner.atEnd()) { + boolean before = this.scanner.currentPosition < regionStart; + char ch = (char) this.scanner.getNextChar(); + if (ch == '\n' ) { + if (before) linesOutside++; + } + } + + // Restart the edit at the beginning of the line where the region start + edit.offset = regionStart; + edit.length -= edit.offset - editStart; + + // Cut replacement string if necessary + int length = edit.replacement.length(); + if (length > 0) { + + // Count the lines in replacement string + int linesReplaced = 0; + for (int idx=0; idx < length; idx++) { + if (edit.replacement.charAt(idx) == '\n') linesReplaced++; + } + + // As the edit starts outside the region, remove first lines from edit string if any + if (linesReplaced > 0) { + int linesCount = linesOutside >= linesReplaced ? linesReplaced : linesOutside; + if (linesCount > 0) { + int idx=0; + while (idx < length) { + char ch = edit.replacement.charAt(idx); + if (ch == '\n') { + linesCount--; + if (linesCount == 0) { + idx++; + break; + } + } + else if (ch != '\r') { + break; + } + idx++; + } + if (idx >= length) { + edit.replacement = ""; //$NON-NLS-1$ + } else { + edit.replacement = edit.replacement.substring(idx); + } + } + } + } + overlapIndex = i; + break; + } + } + } + + // Look for an edit overlapping the region end + if (overlapIndex != -1) bottom = overlapIndex; + while (bottom <= topEnd) { + i = bottom + (topEnd - bottom) /2; + edit = sortedEdits[i]; + int editStart = edit.offset; + int editEnd = editStart + edit.length; + if (regionEnd < editStart) { // the edit starts after the region's end => no possible overlap of region's end + topEnd = i-1; + } else { + if (regionEnd >= editEnd) { // the edit ends before the region's end => no possible overlap of region's end + bottom = i+1; + } else { + // Count the lines of the edit which are outside the region + linesOutside = 0; + this.scanner.resetTo(editStart, editEnd-1); + while (!this.scanner.atEnd()) { + boolean after = this.scanner.currentPosition >= regionEnd; + char ch = (char) this.scanner.getNextChar(); + if (ch == '\n' ) { + if (after) linesOutside++; + } + } + + // Cut replacement string if necessary + int length = edit.replacement.length(); + if (length > 0) { + + // Count the lines in replacement string + int linesReplaced = 0; + for (int idx=0; idx < length; idx++) { + if (edit.replacement.charAt(idx) == '\n') linesReplaced++; + } + + // Set the replacement string to the number of missing new lines + // As the end of the edit is out of the region, the possible trailing + // indentation should not be added... + if (linesReplaced > 0) { + int linesCount = linesReplaced > linesOutside ? linesReplaced - linesOutside : 0; + if (linesCount == 0) { + edit.replacement = ""; //$NON-NLS-1$ } else { - upperBound = editOffset; - upperFound = true; - // verify if region end is at EOF - if (this.scannerEndPosition == regionEnd) { - lowerBound = this.scannerEndPosition - 1; - break; + StringBuffer buffer = new StringBuffer(); + for (int j=0; j this.pageWidth) { - return false; - } - - int numberOfLineEnds = this.lineEnds != null && this.lineEnds.length > 0 ? this.lineEnds.length : 0; - if (this.line == numberOfLineEnds + 1) { - // https://bugs.eclipse.org/bugs/show_bug.cgi?id=213283 - return true; // last line of the CU - } - - if (this.line > 1 && numberOfLineEnds > 0) { // CU has more than one line - // https://bugs.eclipse.org/bugs/show_bug.cgi?id=222182 - // take the max number of line ends as right bound when searching for line number - int lineNumber = Util.getLineNumber(offset, this.lineEnds, 0, numberOfLineEnds); - int lineEnd = getLineEnd(lineNumber); - if (regionEnd > lineEnd) { - // if more than one line selected, check whether selection is at line end - for (int i = lineNumber + 1 ; i <= numberOfLineEnds ; i++) { - int nextLineEnd = getLineEnd(i); - // accept both line ends and line starts - if (regionEnd == nextLineEnd) { - return length > 1; // except when formatting a single character - } else if (regionEnd == lineEnd + 1 || regionEnd == nextLineEnd + 1) { - return true; - } - } - return false; // more than one line selected, no need to adapt region - } else { - if (this.scannerEndPosition - 1 == lineEnd) { // EOF reached? - return false; - } - return true; // a single line was selected - } - } - return false; - } - private boolean isOnFirstColumn(int start) { if (this.lineEnds == null) return start == 0; int index = Arrays.binarySearch(this.lineEnds, start);