### Eclipse Workspace Patch 1.0 #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.30 diff -u -r1.30 Alignment.java --- formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 3 Sep 2008 13:36:24 -0000 1.30 +++ formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 14 Jan 2009 10:16:28 -0000 @@ -222,6 +222,16 @@ } } + public int depth() { + int depth = 0; + Alignment current = this.enclosing; + while (current != null) { + depth++; + current = current.enclosing; + } + return depth; + } + public boolean couldBreak(){ int i; switch(this.mode & SPLIT_MASK){ 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.157 diff -u -r1.157 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 9 Jan 2009 15:10:20 -0000 1.157 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 14 Jan 2009 10:16:28 -0000 @@ -936,13 +936,15 @@ if (count == 0) { // preserve line breaks in wrapping if specified // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074 - if (this.currentAlignment != null && !this.formatter.preferences.join_wrapped_lines) { - int savedIndentation = this.indentationLevel; - StringBuffer buffer = new StringBuffer(getNewLine()); - this.indentationLevel = this.currentAlignment.breakIndentationLevel; - printIndentationIfNecessary(buffer); - this.indentationLevel = savedIndentation; - return buffer.toString(); + if (this.currentAlignment != null && this.memberAlignment != null && !this.formatter.preferences.join_wrapped_lines) { + if (this.memberAlignment.depth() <= this.currentAlignment.depth()) { + int savedIndentation = this.indentationLevel; + StringBuffer buffer = new StringBuffer(getNewLine()); + this.indentationLevel = this.currentAlignment.breakIndentationLevel; + printIndentationIfNecessary(buffer); + this.indentationLevel = savedIndentation; + return buffer.toString(); + } } return Util.EMPTY_STRING; } #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.33 diff -u -r1.33 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 9 Jan 2009 15:10:18 -0000 1.33 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 14 Jan 2009 10:16:29 -0000 @@ -3428,4 +3428,110 @@ "}\n" ); } + +/** + * @bug 260798: [formatter] Strange behavior of never join lines + * @test Ensure that the formatter indents lines correctly when never join lines pref is activated + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=260798" + */ +public void testBug260798() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "class X {\n" + + " @Override\n" + + " public void addSelectionListener(SelectionListener listener) {\n" + + " super.addSelectionListener(new SelectionListener() {\n" + + " @Override\n" + + " public void widgetSelected(SelectionEvent e) {\n" + + " }\n" + + "\n" + + " @Override\n" + + " public void widgetDefaultSelected(SelectionEvent e) {\n" + + " };\n" + + " });\n" + + " }\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " @Override\n" + + " public void addSelectionListener(SelectionListener listener) {\n" + + " super.addSelectionListener(new SelectionListener() {\n" + + " @Override\n" + + " public void widgetSelected(SelectionEvent e) {\n" + + " }\n" + + "\n" + + " @Override\n" + + " public void widgetDefaultSelected(SelectionEvent e) {\n" + + " };\n" + + " });\n" + + " }\n" + + "}\n" + ); +} +public void testBug260798b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "class X {\n" + + "\n" + + " void foo() {\n" + + " this.bar(new Object() {\n" + + " @Override\n" + + " public String toString() {\n" + + " return \"\";\n" + + " }\n" + + " });\n" + + " }\n" + + "}\n"; + formatSource(source, + "class X {\n" + + "\n" + + " void foo() {\n" + + " this.bar(new Object() {\n" + + " @Override\n" + + " public String toString() {\n" + + " return \"\";\n" + + " }\n" + + " });\n" + + " }\n" + + "}\n" + ); +} +public void testBug260798c() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "class X {\n" + + "\n" + + "{\n" + + " this.bar(new Object() {\n" + + " @Override\n" + + " public String toString() {\n" + + " return \"\";\n" + + " }\n" + + " });\n" + + "}\n" + + " void bar(Object object) {\n" + + " \n" + + " }\n" + + "\n" + + "}\n"; + formatSource(source, + "class X {\n" + + "\n" + + " {\n" + + " this.bar(new Object() {\n" + + " @Override\n" + + " public String toString() {\n" + + " return \"\";\n" + + " }\n" + + " });\n" + + " }\n" + + "\n" + + " void bar(Object object) {\n" + + "\n" + + " }\n" + + "\n" + + "}\n" + ); +} + }