### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.model 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.248 diff -u -r1.248 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 18 Aug 2009 09:03:32 -0000 1.248 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 21 Aug 2009 08:17:52 -0000 @@ -10603,12 +10603,35 @@ "\n" + " void foo() {\n" + " String x = \"select x \"\n" + - " + \"from y \"\n" + - " + \"where z=a\";\n" + + " + \"from y \"\n" + + " + \"where z=a\";\n" + " }\n" + "}\n" ); } +public void testBug198074b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + "String x = \"select x \"\n" + + " + \"from y \"\n" + + " + \"where z=a\";\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " String x = \"select x \"\n" + + " + \"from y \"\n" + + " + \"where z=a\";\n" + + " }\n" + + "}\n" + ); +} // another test case put in bug's comment 1 public void testBug198074_c1() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; @@ -10632,13 +10655,43 @@ " // we need x\n" + " // we need a select\n" + " return \"select x \"\n" + - " + \"from X\";\n" + + " + \"from X\";\n" + " }\n" + " return null;\n" + " }\n" + "}\n" ); } +public void testBug198074_c1b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " String foo(boolean enabled) {\n" + + "if (enabled)\n" + + "{\n" + + " // we need x\n" + + " // we need a select\n" + + " return \"select x \"\n" + + " + \"from X\";}\n" + + " return null;}\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " String foo(boolean enabled) {\n" + + " if (enabled) {\n" + + " // we need x\n" + + " // we need a select\n" + + " return \"select x \"\n" + + " + \"from X\";\n" + + " }\n" + + " return null;\n" + + " }\n" + + "}\n" + ); +} // another test case put in bug's comment 3 public void testBug198074_c3() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; @@ -10660,15 +10713,45 @@ "\n" + " public String toString() {\n" + " return \"YAD01: \"\n" + - " + \" nommbr=\'\" + getName() + \"\'\"\n" + - " + \" nomgrp=\'\" + getService().getArgtbl() + \"\'\"\n" + - " + \" typmbr=\'\" + getMemberType().getArgument() + \"\'\"\n" + - " + \" srcpat=\'\" + getPhysicalPath() + \"\'\"\n" + - " + \" nommdl=\'\" + getModel() + \"\'\";\n" + + " + \" nommbr=\'\" + getName() + \"\'\"\n" + + " + \" nomgrp=\'\" + getService().getArgtbl() + \"\'\"\n" + + " + \" typmbr=\'\" + getMemberType().getArgument() + \"\'\"\n" + + " + \" srcpat=\'\" + getPhysicalPath() + \"\'\"\n" + + " + \" nommdl=\'\" + getModel() + \"\'\";\n" + " }\n" + "}\n" ); } +public void testBug198074_c3b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + "public String toString() {\n" + + " return \"YAD01: \"\n" + + " + \" nommbr=\'\"+getName()+\"\'\"\n" + + " + \" nomgrp=\'\"+getService().getArgtbl()+\"\'\"\n" + + " + \" typmbr=\'\"+getMemberType().getArgument()+\"\'\"\n" + + " + \" srcpat=\'\"+getPhysicalPath()+\"\'\"\n" + + " + \" nommdl=\'\"+getModel()+\"\'\"\n" + + " ;\n" + + "}\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " public String toString() {\n" + + " return \"YAD01: \"\n" + + " + \" nommbr=\'\" + getName() + \"\'\"\n" + + " + \" nomgrp=\'\" + getService().getArgtbl() + \"\'\"\n" + + " + \" typmbr=\'\" + getMemberType().getArgument() + \"\'\"\n" + + " + \" srcpat=\'\" + getPhysicalPath() + \"\'\"\n" + + " + \" nommdl=\'\" + getModel() + \"\'\";\n" + + " }\n" + + "}\n" + ); +} public void testBug198074_comments() throws JavaModelException { this.formatterPrefs.join_lines_in_comments = false; String source = @@ -10764,8 +10847,8 @@ " void foo() {\n" + " int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0;\n" + " if ((a == b && b == c) &&\n" + - " (d == e) &&\n" + - " (f == g && h == i)) {\n" + + " (d == e) &&\n" + + " (f == g && h == i)) {\n" + " }\n" + " }\n" + "}\n" @@ -10914,4 +10997,211 @@ "}\n" ); } +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=286668 +public void testBug286608() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\").append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\").append(\"ghi\").append(\"jkl\").append(\n" + + " \"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source); +} +public void testBug286608c() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source); +} +public void testBug286608_40w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 40; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\").append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\n" + + " \"def\").append(\"ghi\")\n" + + " .append(\"jkl\").append(\n" + + " \"mno\")\n" + + " .append(\"pqr\").append(\"stu\")\n" + + " .append(\"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608b_40w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 40; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\n" + + " \"def\")\n" + + " .append(\"ghi\").append(\"jkl\")\n" + + " .append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\")\n" + + " .append(\"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608c_40w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 40; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\n" + + " \"def\")\n" + + " .append(\"ghi\").append(\"jkl\")\n" + + " .append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\")\n" + + " .append(\"vwx\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608_60w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 60; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\").append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\").append(\"ghi\")\n" + + " .append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608b_60w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 60; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286608c_60w() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.page_width = 60; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " StringBuilder builder = new StringBuilder();\n" + + " builder.append(\"abc\").append(\"def\")\n" + + " .append(\"ghi\").append(\"jkl\").append(\"mno\")\n" + + " .append(\"pqr\").append(\"stu\").append(\"vwx\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n"; + formatSource(source); +} } #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java,v retrieving revision 1.34 diff -u -r1.34 Alignment.java --- formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 18 Aug 2009 09:02:25 -0000 1.34 +++ formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 21 Aug 2009 08:17:55 -0000 @@ -45,7 +45,6 @@ public int shiftBreakIndentationLevel; public int[] fragmentBreaks; public boolean wasSplit; - public boolean useBreakIndentation = false; // used while preserving line breaks public Scribe scribe; Index: formatter/org/eclipse/jdt/internal/formatter/Location.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Location.java,v retrieving revision 1.14 diff -u -r1.14 Location.java --- formatter/org/eclipse/jdt/internal/formatter/Location.java 7 Mar 2009 01:08:09 -0000 1.14 +++ formatter/org/eclipse/jdt/internal/formatter/Location.java 21 Aug 2009 08:17:54 -0000 @@ -18,6 +18,7 @@ public class Location { public int inputOffset; + public int inputColumn; public int outputLine; public int outputColumn; public int outputIndentationLevel; @@ -42,6 +43,7 @@ this.outputColumn = scribe.column; this.outputLine = scribe.line; this.inputOffset = sourceRestart; + this.inputColumn = scribe.getCurrentIndentation(sourceRestart); this.outputIndentationLevel = scribe.indentationLevel; this.lastNumberOfNewLines = scribe.lastNumberOfNewLines; this.needSpace = scribe.needSpace; 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.170 diff -u -r1.170 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 18 Aug 2009 09:02:26 -0000 1.170 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 21 Aug 2009 08:17:55 -0000 @@ -817,6 +817,34 @@ return offset; } + int getCurrentIndentation(int start) { + int linePtr = -Arrays.binarySearch(this.lineEnds, start); + int offset = 0; + int beginningOfLine = getLineEnd(linePtr - 1); + if (beginningOfLine == -1) { + beginningOfLine = 0; + } + char[] source = this.scanner.source; + + for (int i=beginningOfLine; i 0) { return Util.EMPTY_STRING; @@ -947,10 +975,21 @@ // insert a new line only if it has not been already done before // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=283476 if (this.lastNumberOfNewLines == 0) { + // Reset indentation level to the location output + this.indentationLevel = this.currentAlignment.location.outputIndentationLevel; + this.numberOfIndentations = this.currentAlignment.location.numberOfIndentations; + this.formatter.lastLocalDeclarationSourceStart = this.currentAlignment.location.lastLocalDeclarationSourceStart; + // Create new line keeping the existing indentation StringBuffer buffer = new StringBuffer(getNewLine()); - if (this.currentAlignment.useBreakIndentation) { + int currentColumn = getCurrentIndentation(this.scanner.currentPosition); + Alignment rootAlignment = this.currentAlignment; + while (rootAlignment.enclosing != null) { + rootAlignment = rootAlignment.enclosing; + } + Location location = rootAlignment.location; + if (currentColumn > location.inputColumn) { int savedIndentation = this.indentationLevel; - this.indentationLevel = this.currentAlignment.breakIndentationLevel; + this.indentationLevel += currentColumn - location.inputColumn; printIndentationIfNecessary(buffer); this.indentationLevel = savedIndentation; } else { Index: formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java,v retrieving revision 1.217 diff -u -r1.217 CodeFormatterVisitor.java --- formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 18 Aug 2009 09:02:26 -0000 1.217 +++ formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 21 Aug 2009 08:17:54 -0000 @@ -427,11 +427,10 @@ BinaryExpressionFragmentBuilder builder = buildFragments(binaryExpression, scope); final int fragmentsSize = builder.size(); - if ((builder.realFragmentsSize() > 1 || fragmentsSize > 2) && numberOfParens == 0) { + if ((builder.realFragmentsSize() > 1 || fragmentsSize > 4) && numberOfParens == 0) { + this.scribe.printComment(); Alignment binaryExpressionAlignment = this.scribe.createAlignment("binaryExpressionAlignment", this.preferences.alignment_for_binary_expression, Alignment.R_OUTERMOST, fragmentsSize, this.scribe.scanner.currentPosition); //$NON-NLS-1$ this.scribe.enterAlignment(binaryExpressionAlignment); - binaryExpressionAlignment.useBreakIndentation = true; - this.scribe.printComment(); boolean ok = false; ASTNode[] fragments = builder.fragments(); int[] operators = builder.operators(); @@ -4862,12 +4861,11 @@ manageOpeningParenthesizedExpression(stringLiteral, numberOfParens); } + this.scribe.printComment(); ASTNode[] fragments = stringLiteral.literals; int fragmentsSize = stringLiteral.counter; Alignment binaryExpressionAlignment = this.scribe.createAlignment("binaryExpressionAlignment", this.preferences.alignment_for_binary_expression, Alignment.R_OUTERMOST, fragmentsSize, this.scribe.scanner.currentPosition); //$NON-NLS-1$ this.scribe.enterAlignment(binaryExpressionAlignment); - binaryExpressionAlignment.useBreakIndentation = true; - this.scribe.printComment(); boolean ok = false; do { try {