### 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.32.2.1 diff -u -r1.32.2.1 Alignment.java --- formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 20 Jul 2009 15:47:05 -0000 1.32.2.1 +++ formatter/org/eclipse/jdt/internal/formatter/align/Alignment.java 8 Jan 2010 10:10:51 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -379,18 +379,26 @@ public String toString() { StringBuffer buffer = new StringBuffer(10); - String className = getClass().getName(); - className = className.substring(className.lastIndexOf('.')+1); + return toString(buffer, -1); + } + + public String toString(StringBuffer buffer, int level) { + + // Compute the indentation at the given level + StringBuffer indentation = new StringBuffer(); + for (int i=0; i"); //$NON-NLS-1$ - int indentLength = className.length()+1; - buffer.append('\n'); - for (int i=0; i\n"); //$NON-NLS-1$ + + // Line for depth and break indentation + buffer.append(indentation); buffer .append("'); - if (this.enclosing != null) { - buffer - .append("'); - } - buffer.append('\n'); + .append(">\n"); //$NON-NLS-1$ + // Line to display the location + buffer.append(indentation); + buffer + .append("\n"); //$NON-NLS-1$ + + // Lines for fragments + buffer + .append(indentation) + .append("\n"); //$NON-NLS-1$ } - buffer.append('\n'); + buffer + .append(indentation) + .append(">\n"); //$NON-NLS-1$ + + // Display enclosing + if (this.enclosing != null && level >= 0) { + buffer + .append(indentation) + .append("\n"); //$NON-NLS-1$ + } + + // Return the result return buffer.toString(); } 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.162.2.7 diff -u -r1.162.2.7 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 8 Oct 2009 11:42:07 -0000 1.162.2.7 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 8 Jan 2010 10:10:51 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -72,6 +72,8 @@ public CodeFormatterVisitor formatter; public int indentationLevel; public int lastNumberOfNewLines; + private boolean preserveLineBreakIndentation = false; + boolean formatBrace; public int line; private int[] lineEnds; @@ -212,7 +214,7 @@ /* * Adapt edits to regions. - * + * * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583" * for more details */ @@ -257,7 +259,7 @@ * 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 @@ -274,8 +276,8 @@ OptimizedReplaceEdit edit = null; int overlapIndex = -1; int linesOutside= -1; - - // Look for an edit overlapping the region start + + // Look for an edit overlapping the region start while (bottom <= top) { i = bottom + (top - bottom) /2; edit = sortedEdits[i]; @@ -300,7 +302,7 @@ if (before) linesOutside++; } } - + // Restart the edit at the beginning of the line where the region start edit.offset = regionStart; edit.length -= edit.offset - editStart; @@ -352,8 +354,8 @@ } } } - - // Look for an edit overlapping the region end + + // Look for an edit overlapping the region end if (overlapIndex != -1) bottom = overlapIndex; while (bottom <= topEnd) { i = bottom + (topEnd - bottom) /2; @@ -817,6 +819,37 @@ return offset; } + int getCurrentIndentation(int start) { + int linePtr = Arrays.binarySearch(this.lineEnds, start); + if (linePtr < 0) { + linePtr = -linePtr - 1; + } + 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; @@ -944,14 +977,86 @@ // 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) { - // 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) { + // 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) + // or when there's no direct member alignment + // (additional fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=286601) + if (this.lastNumberOfNewLines == 0 || this.memberAlignment == null || this.memberAlignment.location.inputOffset < this.currentAlignment.location.inputOffset) { + + // Debug + if (DefaultCodeFormatter.DEBUG) { + System.out.println("Preserve empty lines:"); //$NON-NLS-1$ + System.out.println(" - indentation level = "+this.indentationLevel); //$NON-NLS-1$ + System.out.println(" - current alignment: "); //$NON-NLS-1$ + System.out.print(this.currentAlignment.toString(new StringBuffer(), 1)); + if (this.memberAlignment != null) { + System.out.println(" - member alignment: "); //$NON-NLS-1$ + System.out.print(this.memberAlignment.toString(new StringBuffer(), 1)); + } + } + + // Reset indentation level to the location output + this.indentationLevel = this.currentAlignment.location.outputIndentationLevel; + + // Create new line StringBuffer buffer = new StringBuffer(getNewLine()); - int savedIndentation = this.indentationLevel; - this.indentationLevel = this.currentAlignment.breakIndentationLevel; + + // Look for current indentation + int currentColumn = getCurrentIndentation(this.scanner.currentPosition); + + // Determine whether the alignment indentation can be used or not + // So far, the best algorithm is to use it when + // 1. this is not the opening brace of a local declaration assignment + // 2. this is not the first opening brace + // or this is an array initializer alignment + // or this is an binary expression alignment + // 3. the indentation level is below the alignment break indentation + int currentTokenStartPosition = this.scanner.currentPosition; + int nextToken = -1; + try { + nextToken = this.scanner.getNextToken(); + } catch (InvalidInputException e) { + // skip + } + this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1); + boolean canUseAlignmentIndentation = (nextToken != TerminalTokens.TokenNameLBRACE || !this.currentAlignment.name.equals("localDeclarationAssignmentAlignment")); //$NON-NLS-1$ + if (canUseAlignmentIndentation && + (!this.formatBrace || + this.currentAlignment.name.equals("array_initializer") || //$NON-NLS-1$ + this.currentAlignment.name.equals("binaryExpressionAlignment")) && //$NON-NLS-1$ + this.indentationLevel < this.currentAlignment.breakIndentationLevel) { + this.indentationLevel = this.currentAlignment.breakIndentationLevel; + } + + // Use the current indentation if over the computed indentation + if (this.indentationLevel < currentColumn) { + this.indentationLevel = currentColumn; + } + + // Debug + if (DefaultCodeFormatter.DEBUG) { + System.out.println(" - format brace = "+this.formatBrace); //$NON-NLS-1$ + System.out.println(" - current column = "+currentColumn); //$NON-NLS-1$ + System.out.println(" - current position = "+this.scanner.currentPosition); //$NON-NLS-1$ + System.out.print(" - current line = "); //$NON-NLS-1$ + int linePtr = Arrays.binarySearch(this.lineEnds, this.scanner.currentPosition); + if (linePtr < 0) { + linePtr = -linePtr - 1; + } + int i = getLineEnd(linePtr)+1; + while (this.scanner.source[i] != '\r') { + System.out.print(this.scanner.source[i++]); + } + System.out.println(); + System.out.println(" - indentation level = "+this.indentationLevel); //$NON-NLS-1$ + System.out.println(); + } + + // Set the flag to indicate that a specific indentation is currently in used + this.preserveLineBreakIndentation = true; + + // Print the computed indentation in the buffer printIndentationIfNecessary(buffer); - this.indentationLevel = savedIndentation; return buffer.toString(); } } @@ -1276,7 +1381,7 @@ try { while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { nextCharacterStart = this.scanner.currentPosition; - + switch(currentCharacter) { case '\r' : start = previousStart; @@ -1296,7 +1401,7 @@ this.column = 1; this.line++; isNewLine = false; - + StringBuffer buffer = new StringBuffer(); if (onFirstColumn) { // simply insert indentation if necessary @@ -2325,7 +2430,7 @@ break; } } - + // Delete leading whitespaces if any if (previousToken != -1 && lastTokenEndPosition != commentStart && spaceEndPosition > lastTokenEndPosition) { addDeleteEdit(lastTokenEndPosition, spaceEndPosition-1); @@ -3750,7 +3855,12 @@ return; } if (this.lastNumberOfNewLines >= 1) { - this.column = 1; // ensure that the scribe is at the beginning of a new line + // ensure that the scribe is at the beginning of a new line + // only if no specific indentation has been previously set + if (!this.preserveLineBreakIndentation) { + this.column = 1; + } + this.preserveLineBreakIndentation = false; return; } addInsertEdit(insertPosition, this.lineSeparator); @@ -3759,6 +3869,7 @@ this.column = 1; this.needSpace = false; this.pendingSpace = false; + this.preserveLineBreakIndentation = false; } public void printNextToken(int expectedTokenType){ @@ -3766,15 +3877,31 @@ } public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny){ - printComment(CodeFormatter.K_UNKNOWN); + // Set brace flag, it's useful for the scribe while preserving line breaks + switch (expectedTokenType) { + case TerminalTokens.TokenNameRBRACE: + case TerminalTokens.TokenNameLBRACE: + this.formatBrace = true; + } try { - this.currentToken = this.scanner.getNextToken(); - if (expectedTokenType != this.currentToken) { - throw new AbortFormatting("unexpected token type, expecting:"+expectedTokenType+", actual:"+this.currentToken);//$NON-NLS-1$//$NON-NLS-2$ + printComment(CodeFormatter.K_UNKNOWN); + try { + this.currentToken = this.scanner.getNextToken(); + if (expectedTokenType != this.currentToken) { + throw new AbortFormatting("unexpected token type, expecting:"+expectedTokenType+", actual:"+this.currentToken);//$NON-NLS-1$//$NON-NLS-2$ + } + print(this.scanner.currentPosition - this.scanner.startPosition, considerSpaceIfAny); + } catch (InvalidInputException e) { + throw new AbortFormatting(e); + } + } + finally { + // Flush brace flag + switch (expectedTokenType) { + case TerminalTokens.TokenNameRBRACE: + case TerminalTokens.TokenNameLBRACE: + this.formatBrace = false; } - print(this.scanner.currentPosition - this.scanner.startPosition, considerSpaceIfAny); - } catch (InvalidInputException e) { - throw new AbortFormatting(e); } } 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 8 Jan 2010 10:10:49 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -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/CodeFormatterVisitor.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java,v retrieving revision 1.214.2.1 diff -u -r1.214.2.1 CodeFormatterVisitor.java --- formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 18 Jun 2009 09:34:30 -0000 1.214.2.1 +++ formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 8 Jan 2010 10:10:49 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2009 IBM Corporation and others. + * Copyright (c) 2002, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -1575,15 +1575,21 @@ } private void formatLeftCurlyBrace(final int line, final String bracePosition) { + this.scribe.formatBrace = true; /* * deal with (quite unexpected) comments right before lcurly */ - this.scribe.printComment(); - if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition) - && (this.scribe.line > line || this.scribe.column >= this.preferences.page_width)) - { - this.scribe.printNewLine(); - } + try { + this.scribe.printComment(); + if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition) + && (this.scribe.line > line || this.scribe.column >= this.preferences.page_width)) + { + this.scribe.printNewLine(); + } + } + finally { + this.scribe.formatBrace = false; + } } private void formatLocalDeclaration(LocalDeclaration localDeclaration, BlockScope scope, boolean insertSpaceBeforeComma, boolean insertSpaceAfterComma) { #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 FormatterBugsTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 8 Oct 2009 11:42:11 -0000 1.1.2.2 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 8 Jan 2010 10:10:54 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,6 +11,7 @@ package org.eclipse.jdt.core.tests.formatter; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; import junit.framework.Test; @@ -25,6 +26,30 @@ super(name); } +/* (non-Javadoc) + * @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#setUp() + */ +private void setUpBracesPreferences(String braces) { + if (braces != null) { + assertTrue("Invalid value for braces preferences: "+braces, + braces.equals(DefaultCodeFormatterConstants.END_OF_LINE) || + braces.equals(DefaultCodeFormatterConstants.NEXT_LINE) || + braces.equals(DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP) || + braces.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)); + this.formatterPrefs.brace_position_for_annotation_type_declaration = braces; + this.formatterPrefs.brace_position_for_anonymous_type_declaration = braces; + this.formatterPrefs.brace_position_for_array_initializer = braces; + this.formatterPrefs.brace_position_for_block = braces; + this.formatterPrefs.brace_position_for_block_in_case = braces; + this.formatterPrefs.brace_position_for_constructor_declaration = braces; + this.formatterPrefs.brace_position_for_enum_constant = braces; + this.formatterPrefs.brace_position_for_enum_declaration = braces; + this.formatterPrefs.brace_position_for_method_declaration = braces; + this.formatterPrefs.brace_position_for_switch = braces; + this.formatterPrefs.brace_position_for_type_declaration = braces; + } +} + /** * Create project and set the jar placeholder. */ @@ -34,6 +59,7 @@ } super.setUpSuite(); } + /** * @bug 198074: [formatter] the code formatter doesn't respect my new lines * @test Ensure that the formatter keep line breaks wrapping set by users in the code @@ -41,227 +67,1142 @@ */ public void testBug198074() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; - 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" + + 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" + ); +} +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; - 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" + + 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" + ); +} +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; - 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" + + 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_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 = - "public class Test {\n" + - "\n" + - " void foo() {\n" + - "String x = \"select x \"\n" + - " + \"from y \"\n" + - " + \"where z=a\";\n" + - " }\n" + + 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 \" + \"from y \" + \"where z=a\";\n" + - " }\n" + + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " String x = \"select x \" + \"from y \" + \"where z=a\";\n" + + " }\n" + "}\n" ); } // duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=201022 -public void testBug201022() throws JavaModelException { +// see also bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=287462 +public void testBug198074_dup201022() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; - String source = - "public class Test {\n" + - "\n" + - " void foo() {\n" + - " String sQuery =\n" + - " \"select * \" +\n" + - " \"from person p, address a \" +\n" + - " \"where p.person_id = a.person_id \" +\n" + - " \"and p.person_id = ?\";\n" + - " }\n" + - "}\n"; - formatSource(source, - "public class Test {\n" + - "\n" + - " void foo() {\n" + - " String sQuery =\n" + - " \"select * \" +\n" + - " \"from person p, address a \" +\n" + - " \"where p.person_id = a.person_id \" +\n" + - " \"and p.person_id = ?\";\n" + - " }\n" + + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " String sQuery =\n" + + " \"select * \" +\n" + + " \"from person p, address a \" +\n" + + " \"where p.person_id = a.person_id \" +\n" + + " \"and p.person_id = ?\";\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " String sQuery =\n" + + " \"select * \" +\n" + + " \"from person p, address a \" +\n" + + " \"where p.person_id = a.person_id \" +\n" + + " \"and p.person_id = ?\";\n" + + " }\n" + + "}\n" + ); +} +// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=213700 +public void testBug198074_dup213700() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test {\n" + + "\n" + + " 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" + + " ){\n" + + "}\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " 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" + + " }\n" + + " }\n" + "}\n" ); } -// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + +/** + * @bug 208541: [formatter] Formatter does not format whole region/selection + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541" + */ public void testBug208541() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; - String source = - "public class MyTest {\n" + - "\n" + - " public void testname() throws Exception {\n" + - " int i = 5, j = 6, k = 7;\n" + - " if (new String().length() != 0 &&\n" + - " (i < j && j < k)) {\n" + - "\n" + - " }\n" + - " }\n" + - "}\n"; - formatSource(source, - "public class MyTest {\n" + - "\n" + - " public void testname() throws Exception {\n" + - " int i = 5, j = 6, k = 7;\n" + - " if (new String().length() != 0 &&\n" + - " (i < j && j < k)) {\n" + - "\n" + - " }\n" + - " }\n" + + String source = + "public class MyTest {\n" + + "\n" + + " public void testname() throws Exception {\n" + + " int i = 5, j = 6, k = 7;\n" + + " if (new String().length() != 0 &&\n" + + " (i < j && j < k)) {\n" + + "\n" + + " }\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class MyTest {\n" + + "\n" + + " public void testname() throws Exception {\n" + + " int i = 5, j = 6, k = 7;\n" + + " if (new String().length() != 0 &&\n" + + " (i < j && j < k)) {\n" + + "\n" + + " }\n" + + " }\n" + "}\n" ); } -// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=213700 -public void testBug213700() throws JavaModelException { + +/** + * @bug 279359: [formatter] Formatter with 'never join lines' produces extra level of indent + * @test Ensure that no extra indentation is produced at the end of a body when + * 'never join lines' preference is set. + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=279359" + */ +public void testBug279359() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; - String source = - "public class Test {\n" + - "\n" + - " 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" + - " ){\n" + - "}\n" + - " }\n" + - "}\n"; - formatSource(source, - "public class Test {\n" + - "\n" + - " 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" + - " }\n" + - " }\n" + + String source = + "public class Formatter {\n" + + "\n" + + " public static void main(String[] args) {\n" + + "\n" + + " Executors.newCachedThreadPool().execute(new Runnable() {\n" + + "\n" + + " public void run() {\n" + + " throw new UnsupportedOperationException(\"stub\");\n" + + " }\n" + + "\n" + + " });\n" + + "\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Formatter {\n" + + "\n" + + " public static void main(String[] args) {\n" + + "\n" + + " Executors.newCachedThreadPool().execute(new Runnable() {\n" + + "\n" + + " public void run() {\n" + + " throw new UnsupportedOperationException(\"stub\");\n" + + " }\n" + + "\n" + + " });\n" + + "\n" + + " }\n" + "}\n" ); } -//https://bugs.eclipse.org/bugs/show_bug.cgi?id=283467 + +/** + * @bug 283467: [formatter] wrong indentation with 'Never join lines' selected + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=283467" + */ public void testBug283467() throws JavaModelException { this.formatterPrefs.join_wrapped_lines = false; - String source = - "public class TestFormatter {\n" + - "\n" + - " public static void main(String[] args) {\n" + - " int variable = TestFormatter.doInCallback(new Runnable() {\n" + - " public void run() {\n" + - " // Some comments or code here\n" + - " }\n" + - " });\n" + - " System.out.println(variable);\n" + - " }\n" + - "\n" + - " public static int doInCallback(Runnable r) {\n" + - " return 0;\n" + - " }\n" + - "}\n"; - formatSource(source, - "public class TestFormatter {\n" + - "\n" + - " public static void main(String[] args) {\n" + - " int variable = TestFormatter.doInCallback(new Runnable() {\n" + - " public void run() {\n" + - " // Some comments or code here\n" + - " }\n" + - " });\n" + - " System.out.println(variable);\n" + - " }\n" + - "\n" + - " public static int doInCallback(Runnable r) {\n" + - " return 0;\n" + - " }\n" + + String source = + "public class TestFormatter {\n" + + "\n" + + " public static void main(String[] args) {\n" + + " int variable = TestFormatter.doInCallback(new Runnable() {\n" + + " public void run() {\n" + + " // Some comments or code here\n" + + " }\n" + + " });\n" + + " System.out.println(variable);\n" + + " }\n" + + "\n" + + " public static int doInCallback(Runnable r) {\n" + + " return 0;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class TestFormatter {\n" + + "\n" + + " public static void main(String[] args) {\n" + + " int variable = TestFormatter.doInCallback(new Runnable() {\n" + + " public void run() {\n" + + " // Some comments or code here\n" + + " }\n" + + " });\n" + + " System.out.println(variable);\n" + + " }\n" + + "\n" + + " public static int doInCallback(Runnable r) {\n" + + " return 0;\n" + + " }\n" + "}\n" ); } /** + * @bug 285565: [formatter] wrong indentation with 'Never join lines' selected + * @test Even if the complete fix is not released into R3_5_maintenance stream, + * verify that using an indentation and tab sizes equal to zero does not raise + * any exception. + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565" + */ +public void testBug285565b() { + this.formatterPrefs.indentation_size = 0; + this.formatterPrefs.tab_size = 0; + String source = "public class test {\n" + + " public static void main(String[] args) {\n" + + " int B= 12;\n" + + " int C= B - 1;\n" + + " int K= 99;\n" + + " int f1= K - 1 - C;\n" + + " int f2= K - C - C - C;\n" + + " }\n" + "}\n"; + formatSource(source, "public class test {\n" + + "public static void main(String[] args) {\n" + + "int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;\n" + + "}\n" + + "}\n"); +} + +/** + * @bug 286601: [formatter] Code formatter formats anonymous inner classes wrongly when 'Never join lines' is on + * @test Test to make sure that indentation is correct in anonymous inner class + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=286601" + */ +public void testBug286601() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test\n" + + "{\n" + + " public void aMethod()\n" + + " {\n" + + " Object anObject = new Object()\n" + + " {\n" + + " boolean aVariable;\n" + + " };\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + " public void aMethod() {\n" + + " Object anObject = new Object()\n" + + " {\n" + + " boolean aVariable;\n" + + " };\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601b() throws JavaModelException { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "public class Test {\n" + + "\n" + + " void foo() {\n" + + "long x1 = 100000000\n" + + " + 200000000\n" + + " + 300000000;\n" + + "long x2 = 100000000\n" + + " + 200000000\n" + + " + 300000000\n" + + " + 400000000;\n" + + "long x3 = 100000000\n" + + " + 200000000\n" + + " + 300000000\n" + + " + 400000000\n" + + " + 500000000;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + "\n" + + " void foo() {\n" + + " long x1 = 100000000\n" + + " + 200000000\n" + + " + 300000000;\n" + + " long x2 = 100000000\n" + + " + 200000000\n" + + " + 300000000\n" + + " + 400000000;\n" + + " long x3 = 100000000\n" + + " + 200000000\n" + + " + 300000000\n" + + " + 400000000\n" + + " + 500000000;\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601c() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.brace_position_for_anonymous_type_declaration= DefaultCodeFormatterConstants.NEXT_LINE; + String source = + "public class Test\n" + + "{\n" + + " public void aMethod()\n" + + " {\n" + + " Object anObject = new Object()\n" + + " {\n" + + " boolean aVariable;\n" + + " void foo()\n" + + " {\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + " public void aMethod() {\n" + + " Object anObject = new Object()\n" + + " {\n" + + " boolean aVariable;\n" + + "\n" + + " void foo()\n" + + " {\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601d() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.brace_position_for_anonymous_type_declaration= DefaultCodeFormatterConstants.NEXT_LINE; + String source = + "public class Test\n" + + "{\n" + + " public void aMethod()\n" + + " {\n" + + " Object anObject = new Object() /* comment */\n" + + " {\n" + + " boolean aVariable;\n" + + " void foo() /* comment */ \n" + + " {\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test {\n" + + " public void aMethod() {\n" + + " Object anObject = new Object() /* comment */\n" + + " {\n" + + " boolean aVariable;\n" + + "\n" + + " void foo() /* comment */\n" + + " {\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601e() { + this.formatterPrefs.join_wrapped_lines = false; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "public class Test\n" + + "{\n" + + " public void build(String href) {\n" + + " // set the href on the related topic\n" + + " if (href == null)\n" + + " setHref(\"\"); //$NON-NLS-1$\n" + + " else {\n" + + " if (!href.equals(\"\") // no empty link //$NON-NLS-1$\n" + + " && !href.startsWith(\"/\") // no help url //$NON-NLS-1$\n" + + " && href.indexOf(\':\') == -1) // no other protocols\n" + + " {\n" + + " setHref(\"/test/\" + href); //$NON-NLS-1$ //$NON-NLS-2$\n" + + " }\n" + + " }\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test\n" + + "{\n" + + " public void build(String href)\n" + + " {\n" + + " // set the href on the related topic\n" + + " if (href == null)\n" + + " setHref(\"\"); //$NON-NLS-1$\n" + + " else\n" + + " {\n" + + " if (!href.equals(\"\") // no empty link //$NON-NLS-1$\n" + + " && !href.startsWith(\"/\") // no help url //$NON-NLS-1$\n" + + " && href.indexOf(\':\') == -1) // no other protocols\n" + + " {\n" + + " setHref(\"/test/\" + href); //$NON-NLS-1$ //$NON-NLS-2$\n" + + " }\n" + + " }\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601f() { + this.formatterPrefs.join_wrapped_lines = false; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "public class Test\n" + + "{\n" + + " \n" + + " private AntModel getAntModel(final File buildFile) {\n" + + " AntModel model= new AntModel(XMLCore.getDefault(), doc, null, new LocationProvider(null) {\n" + + " /* (non-Javadoc)\n" + + " * @see org.eclipse.ant.internal.ui.editor.outline.ILocationProvider#getLocation()\n" + + " */\n" + + " public IPath getLocation() {\n" + + " return new Path(buildFile.getAbsolutePath());\n" + + " }\n" + + " });\n" + + " model.reconcile(null);\n" + + " return model;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class Test\n" + + "{\n" + + "\n" + + " private AntModel getAntModel(final File buildFile)\n" + + " {\n" + + " AntModel model = new AntModel(XMLCore.getDefault(), doc, null,\n" + + " new LocationProvider(null)\n" + + " {\n" + + " /*\n" + + " * (non-Javadoc)\n" + + " * \n" + + " * @see\n" + + " * org.eclipse.ant.internal.ui.editor.outline.ILocationProvider\n" + + " * #getLocation()\n" + + " */\n" + + " public IPath getLocation()\n" + + " {\n" + + " return new Path(buildFile.getAbsolutePath());\n" + + " }\n" + + " });\n" + + " model.reconcile(null);\n" + + " return model;\n" + + " }\n" + + "}\n" + ); +} +public void testBug286601g() { + this.formatterPrefs.join_wrapped_lines = false; + String source = + "package massive;\n" + + "\n" + + "public class X05\n" + + "{\n" + + "\n" + + " public void foo() throws NullPointerException {\n" + + "\n" + + " Object body = new Object() {\n" + + " public void run(StringBuffer monitor) throws IllegalArgumentException {\n" + + " IResourceVisitor visitor = new IResourceVisitor() {\n" + + " public boolean visit(String resource) throws IllegalArgumentException {\n" + + " return true;\n" + + " }\n" + + " };\n" + + " }\n" + + " };\n" + + " }\n" + + "\n" + + "}\n" + + "interface IResourceVisitor {\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X05 {\n" + + "\n" + + " public void foo() throws NullPointerException {\n" + + "\n" + + " Object body = new Object() {\n" + + " public void run(StringBuffer monitor)\n" + + " throws IllegalArgumentException {\n" + + " IResourceVisitor visitor = new IResourceVisitor() {\n" + + " public boolean visit(String resource)\n" + + " throws IllegalArgumentException {\n" + + " return true;\n" + + " }\n" + + " };\n" + + " }\n" + + " };\n" + + " }\n" + + "\n" + + "}\n" + + "\n" + + "interface IResourceVisitor {\n" + + "}\n" + ); +} +public void testBug286601h() { + this.formatterPrefs.join_wrapped_lines = false; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X05\n" + + "{\n" + + "\n" + + " public void foo() throws NullPointerException {\n" + + "\n" + + " Object body = new Object() {\n" + + " public void run(StringBuffer monitor) throws IllegalArgumentException {\n" + + " IResourceVisitor visitor = new IResourceVisitor() {\n" + + " public boolean visit(String resource) throws IllegalArgumentException {\n" + + " return true;\n" + + " }\n" + + " };\n" + + " }\n" + + " };\n" + + " }\n" + + "\n" + + "}\n" + + "interface IResourceVisitor {\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X05\n" + + "{\n" + + "\n" + + " public void foo() throws NullPointerException\n" + + " {\n" + + "\n" + + " Object body = new Object()\n" + + " {\n" + + " public void run(StringBuffer monitor)\n" + + " throws IllegalArgumentException\n" + + " {\n" + + " IResourceVisitor visitor = new IResourceVisitor()\n" + + " {\n" + + " public boolean visit(String resource)\n" + + " throws IllegalArgumentException\n" + + " {\n" + + " return true;\n" + + " }\n" + + " };\n" + + " }\n" + + " };\n" + + " }\n" + + "\n" + + "}\n" + + "\n" + + "interface IResourceVisitor\n" + + "{\n" + + "}\n" + ); +} +public void testBug286601i1() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.alignment_for_expressions_in_array_initializer = DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X06a {\n" + + "\n" + + " \n" + + " // Table to merge access modes for condition statements (e.g branch[x] || branch[y]). \n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE= {\n" + + " /* Comment 1 */\n" + + " /* Comment 2 */ { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " /* Comment 3 */ { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X06a\n" + + "{\n" + + "\n" + + " // Table to merge access modes for condition statements (e.g branch[x] ||\n" + + " // branch[y]).\n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE =\n" + + " {\n" + + " /* Comment 1 */\n" + + " /* Comment 2 */{ \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " /* Comment 3 */{ \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n" + ); +} +public void testBug286601i2() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.alignment_for_expressions_in_array_initializer = DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X06a {\n" + + "\n" + + " \n" + + " // Table to merge access modes for condition statements (e.g branch[x] || branch[y]). \n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE= {\n" + + " /* Comment 1 */\n" + + " /* Comment 2 */ { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " /* Comment 3 */ { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X06a\n" + + "{\n" + + "\n" + + " // Table to merge access modes for condition statements (e.g branch[x] ||\n" + + " // branch[y]).\n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE =\n" + + " {\n" + + " /* Comment 1 */\n" + + " /* Comment 2 */{ \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " /* Comment 3 */{ \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n" + ); +} +public void testBug286601j1() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.alignment_for_expressions_in_array_initializer = DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X06b {\n" + + "\n" + + " \n" + + " // Table to merge access modes for condition statements (e.g branch[x] || branch[y]). \n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE= {\n" + + " { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X06b\n" + + "{\n" + + "\n" + + " // Table to merge access modes for condition statements (e.g branch[x] ||\n" + + " // branch[y]).\n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE =\n" + + " {\n" + + " { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n" + ); +} +public void testBug286601j2() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.alignment_for_expressions_in_array_initializer = DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X06b {\n" + + "\n" + + " \n" + + " // Table to merge access modes for condition statements (e.g branch[x] || branch[y]). \n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE= {\n" + + " { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X06b\n" + + "{\n" + + "\n" + + " // Table to merge access modes for condition statements (e.g branch[x] ||\n" + + " // branch[y]).\n" + + " private static final String[][] ACCESS_MODE_CONDITIONAL_TABLE =\n" + + " {\n" + + " { \"1234567890123456789012345678901234567890\", \"1234567890123456789012345678901234567890\" },\n" + + " { \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ______________\" },\n" + + " };\n" + + "\n" + + "}\n" + ); +} +public void testBug286601k() { + this.formatterPrefs.join_wrapped_lines = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + this.formatterPrefs.alignment_for_expressions_in_array_initializer = DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE; + setUpBracesPreferences(DefaultCodeFormatterConstants.NEXT_LINE); + String source = + "package massive;\n" + + "\n" + + "public class X07 {\n" + + " private MinimizedFileSystemElement selectFiles(final Object rootFileSystemObject, final IImportStructureProvider structureProvider) {\n" + + "\n" + + " BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {\n" + + " public void run() {\n" + + " //Create the root element from the supplied file system object\n" + + " }\n" + + " });\n" + + "\n" + + " return null;\n" + + " }\n" + + "}\n"; + formatSource(source, + "package massive;\n" + + "\n" + + "public class X07\n" + + "{\n" + + " private MinimizedFileSystemElement selectFiles(\n" + + " final Object rootFileSystemObject,\n" + + " final IImportStructureProvider structureProvider)\n" + + " {\n" + + "\n" + + " BusyIndicator.showWhile(getShell().getDisplay(), new Runnable()\n" + + " {\n" + + " public void run()\n" + + " {\n" + + " // Create the root element from the supplied file system object\n" + + " }\n" + + " });\n" + + "\n" + + " return null;\n" + + " }\n" + + "}\n" + ); +} + +/** + * @bug 286668: [formatter] 'Never Join Lines' joins lines that are split on method invocation + * @test Test to make sure that lines are joined when using 'Never Join Lines' preference + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=286668" + */ +public void testBug286668() 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 testBug286668b() 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 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" + ); +} +public void testBug286668c() 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 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" + ); +} +public void testBug286668_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(\n" + + " \"stu\").append(\n" + + " \"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286668b_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(\n" + + " \"jkl\").append(\n" + + " \"mno\")\n" + + " .append(\"pqr\").append(\n" + + " \"stu\").append(\n" + + " \"vwx\").append(\n" + + " \"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286668c_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(\n" + + " \"jkl\").append(\n" + + " \"mno\")\n" + + " .append(\"pqr\").append(\n" + + " \"stu\").append(\n" + + " \"vwx\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286668_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\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286668b_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\")\n" + + " .append(\"yz\");\n" + + " }\n" + + "}\n" + ); +} +public void testBug286668c_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); +} + +/** * @bug 290905: [formatter] Certain formatter pref constellation cause endless loop ==> OOME * @test Verify that there's endless loop when setting tab length to zero. * As the fix finalize bug 285565 implementation, added tests address only @@ -273,24 +1214,24 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 2; this.formatterPrefs.use_tabs_only_for_leading_indentations = true; - String source = - "/**\n" + - " * Test mixed, tab size = 0, indent size = 2, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - "void foo() throws Exception { if (true) return; else throw new Exception(); }\n" + - "}\n"; - formatSource(source, - "/**\n" + - " * Test mixed, tab size = 0, indent size = 2, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - " void foo() throws Exception {\n" + - " if (true)\n" + - " return;\n" + - " else\n" + - " throw new Exception();\n" + - " }\n" + + String source = + "/**\n" + + " * Test mixed, tab size = 0, indent size = 2, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + "void foo() throws Exception { if (true) return; else throw new Exception(); }\n" + + "}\n"; + formatSource(source, + "/**\n" + + " * Test mixed, tab size = 0, indent size = 2, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + " void foo() throws Exception {\n" + + " if (true)\n" + + " return;\n" + + " else\n" + + " throw new Exception();\n" + + " }\n" + "}\n" ); } @@ -299,24 +1240,24 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 2; this.formatterPrefs.use_tabs_only_for_leading_indentations = false; - String source = - "/**\n" + - " * Test mixed, tab size = 0, indent size = 2, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - "void foo() throws Exception { if (true) return; else throw new Exception(); }\n" + - "}\n"; - formatSource(source, - "/**\n" + - " * Test mixed, tab size = 0, indent size = 2, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - " void foo() throws Exception {\n" + - " if (true)\n" + - " return;\n" + - " else\n" + - " throw new Exception();\n" + - " }\n" + + String source = + "/**\n" + + " * Test mixed, tab size = 0, indent size = 2, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + "void foo() throws Exception { if (true) return; else throw new Exception(); }\n" + + "}\n"; + formatSource(source, + "/**\n" + + " * Test mixed, tab size = 0, indent size = 2, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + " void foo() throws Exception {\n" + + " if (true)\n" + + " return;\n" + + " else\n" + + " throw new Exception();\n" + + " }\n" + "}\n" ); } @@ -325,20 +1266,20 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 0; this.formatterPrefs.use_tabs_only_for_leading_indentations = true; - String source = - "/**\n" + - " * Test mixed, tab size = 0, indent size = 0, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + + String source = + "/**\n" + + " * Test mixed, tab size = 0, indent size = 0, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + "}\n"; formatSource(source, - "/**\n" + - " * Test mixed, tab size = 0, indent size = 0, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the\n" + - " // format line comment preference is activated\n" + + "/**\n" + + " * Test mixed, tab size = 0, indent size = 0, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the\n" + + " // format line comment preference is activated\n" + "}\n", false /* do not repeat */ ); @@ -348,20 +1289,20 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 0; this.formatterPrefs.use_tabs_only_for_leading_indentations = false; - String source = - "/**\n" + - " * Test mixed, tab size = 0, indent size = 0, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + + String source = + "/**\n" + + " * Test mixed, tab size = 0, indent size = 0, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + "}\n"; formatSource(source, - "/**\n" + - " * Test mixed, tab size = 0, indent size = 0, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the\n" + - " // format line comment preference is activated\n" + + "/**\n" + + " * Test mixed, tab size = 0, indent size = 0, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the\n" + + " // format line comment preference is activated\n" + "}\n", false /* do not repeat */ ); @@ -371,20 +1312,20 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 0; this.formatterPrefs.use_tabs_only_for_leading_indentations = true; - String source = - "/**\n" + - " * Test tab char = TAB, tab size = 0, indent size = 0, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + + String source = + "/**\n" + + " * Test tab char = TAB, tab size = 0, indent size = 0, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + "}\n"; formatSource(source, - "/**\n" + - " * Test tab char = TAB, tab size = 0, indent size = 0, use tabs to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the\n" + - " // format line comment preference is activated\n" + + "/**\n" + + " * Test tab char = TAB, tab size = 0, indent size = 0, use tabs to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the\n" + + " // format line comment preference is activated\n" + "}\n", false /* do not repeat */ ); @@ -394,20 +1335,20 @@ this.formatterPrefs.tab_size = 0; this.formatterPrefs.indentation_size = 0; this.formatterPrefs.use_tabs_only_for_leading_indentations = false; - String source = - "/**\n" + - " * Test tab char = TAB, tab size = 0, indent size = 0, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + + String source = + "/**\n" + + " * Test tab char = TAB, tab size = 0, indent size = 0, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the format line comment preference is activated\n" + "}\n"; formatSource(source, - "/**\n" + - " * Test tab char = TAB, tab size = 0, indent size = 0, use spaces to indent\n" + - " */\n" + - "public class Test {\n" + - "int i; // this is a long comment which should be split into two lines as the\n" + - "// format line comment preference is activated\n" + + "/**\n" + + " * Test tab char = TAB, tab size = 0, indent size = 0, use spaces to indent\n" + + " */\n" + + "public class Test {\n" + + "int i; // this is a long comment which should be split into two lines as the\n" + + "// format line comment preference is activated\n" + "}\n", false /* do not repeat */ );