### 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.32 diff -u -r1.32 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 8 Dec 2008 15:53:39 -0000 1.32 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 8 Jan 2009 17:12:55 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2009 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 @@ -3125,4 +3125,307 @@ "}\n" ); } + +/** + * @bug 254998: [formatter] wrong type comment format during code generation + * @test Ensure that the comment formatter works well on the given test case + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=254998" + */ +public void testBug254998() throws JavaModelException { + this.formatterPrefs.comment_format_javadoc_comment = false; + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.comment_format_line_comment = false; + this.formatterPrefs.comment_format_header = true; + String source = + "/**\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "package javadoc;\n" + + "\n" + + "/**\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "public class Test {\n" + + "\n" + + "}\n"; + formatSource(source, + "/**\n" + + " * Test for bug 254998\n" + + " */\n" + + "package javadoc;\n" + + "\n" + + "/**\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "public class Test {\n" + + "\n" + + "}\n" + ); +} +public void testBug254998b() throws JavaModelException { + this.formatterPrefs.comment_format_javadoc_comment = false; + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.comment_format_line_comment = false; + this.formatterPrefs.comment_format_header = true; + String source = + "/*\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "package block;\n" + + "\n" + + "/*\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "public class Test {\n" + + "/*\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "}\n"; + formatSource(source, + "/*\n" + + " * Test for bug 254998\n" + + " */\n" + + "package block;\n" + + "\n" + + "/*\n" + + " * Test for bug 254998\n" + + " */\n" + + "public class Test {\n" + + " /*\n" + + " * Test for\n" + + " * bug 254998\n" + + " */\n" + + "}\n" + ); +} +public void testBug254998c() throws JavaModelException { + this.formatterPrefs.comment_format_javadoc_comment = false; + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.comment_format_line_comment = false; + this.formatterPrefs.comment_format_header = true; + String source = + "// Test for bug 254998\n" + + "package line;\n" + + "\n" + + "// Test for bug 254998\n" + + "public class Test {\n" + + "// Test for bug 254998\n" + + "}\n"; + formatSource(source, + "// Test for bug 254998\n" + + "package line;\n" + + "\n" + + "// Test for bug 254998\n" + + "public class Test {\n" + + " // Test for bug 254998\n" + + "}\n" + ); +} + +/** + * @bug 260274: [formatter] * character is removed while formatting block comments + * @test Ensure that the comment formatter keep '*' characters while formatting block comments + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=260274" + */ +public void testBug260274() throws JavaModelException { + String source = + "class X {\n" + + "/*\n" + + " * The formatter should NOT remove * character\n" + + " * in block comments!\n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * The formatter should NOT remove * character in block comments!\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274b() throws JavaModelException { + String source = + "class X {\n" + + "/*\n" + + " * The formatter should keep \'*\' characters\n" + + " * in block comments!\n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * The formatter should keep \'*\' characters in block comments!\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274c() throws JavaModelException { + this.formatterPrefs.join_lines_in_comments = false; + String source = + "class X {\n" + + "/* *********************************************\n" + + " * Test \n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /* *********************************************\n" + + " * Test\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274d() throws JavaModelException { + String source = + "class X {\n" + + "/* *********************************************\n" + + " * Test \n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /* *********************************************\n" + + " * Test\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274e() throws JavaModelException { + String source = + "class X {\n" + + "/*\n" + + " * **************************************************\n" + + " * ********** Test ********** Test **************\n" + + " * **************************************************\n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * **************************************************\n" + + " * ********** Test ********** Test **************\n" + + " * **************************************************\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274f() throws JavaModelException { + String source = + "class X {\n" + + "/* *****************************************************************************\n" + + " * Action that allows changing the model providers sort order.\n" + + " */\n" + + "void foo() {\n" + + "}\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /* *****************************************************************************\n" + + " * Action that allows changing the model providers sort order.\n" + + " */\n" + + " void foo() {\n" + + " }\n" + + "}\n" + ); +} +public void testBug260274g() throws JavaModelException { + String source = + "class X {\n" + + "/*\n" + + " * **********************************************************************************\n" + + " * **********************************************************************************\n" + + " * **********************************************************************************\n" + + " * The code below was added to track the view with focus\n" + + " * in order to support save actions from a view. Remove this\n" + + " * experimental code if the decision is to not allow views to \n" + + " * participate in save actions (see bug 10234) \n" + + " */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * **********************************************************************************\n" + + " * **********************************************************************************\n" + + " * **********************************************************************************\n" + + " * The code below was added to track the view with focus in order to support\n" + + " * save actions from a view. Remove this experimental code if the decision\n" + + " * is to not allow views to participate in save actions (see bug 10234)\n" + + " */\n" + + "}\n" + ); +} +public void testBug260274h() throws JavaModelException { + String source = + "class X {\n" + + " /**\n" + + " * @see #spacing(Point)\n" + + " * * @see #spacing(int, int)\n" + + " */\n" + + " public void foo() {\n" + + " }\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /**\n" + + " * @see #spacing(Point) * @see #spacing(int, int)\n" + + " */\n" + + " public void foo() {\n" + + " }\n" + + "}\n" + ); +} + +/** + * @bug 260276: [formatter] Inconsistent formatting of one-line block comment + * @test Ensure that the comment formatter has a consistent behavior while formatting one-line block comment + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=260276" + */ +public void testBug260276() throws JavaModelException { + String source = + "class X {\n" + + "/* a\n" + + "comment */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * a comment\n" + + " */\n" + + "}\n" + ); +} +public void testBug260276b() throws JavaModelException { + String source = + "class X {\n" + + "/* a\n" + + " comment */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * a comment\n" + + " */\n" + + "}\n" + ); +} +public void testBug260276c() throws JavaModelException { + String source = + "class X {\n" + + "/* a\n" + + " * comment */\n" + + "}\n"; + formatSource(source, + "class X {\n" + + " /*\n" + + " * a comment\n" + + " */\n" + + "}\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.239 diff -u -r1.239 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 8 Dec 2008 15:53:39 -0000 1.239 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 8 Jan 2009 17:13:02 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2009 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 @@ -222,7 +222,12 @@ String outputSource = expectedContents == null ? originalSource : expectedContents; assertLineEquals(actualContents, originalSource, outputSource, false /* do not check null */); } - + + void formatSource(String source) { + // expect unchanged source after formatting + formatSource(source, source); + } + void formatSource(String source, String formattedOutput) { formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/); } #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.156 diff -u -r1.156 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 8 Dec 2008 15:54:27 -0000 1.156 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 8 Jan 2009 17:13:06 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2009 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 @@ -1223,6 +1223,7 @@ private void printBlockComment(boolean isJavadoc) { int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; + boolean includesBlockComments = includesBlockComments(); this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1); int currentCharacter; @@ -1247,7 +1248,7 @@ this.needSpace = false; this.pendingSpace = false; - if (includesBlockComments()) { + if (includesBlockComments) { if (printBlockComment(currentTokenStartPosition, currentTokenEndPosition)) { return; } @@ -1424,6 +1425,7 @@ boolean hasMultiLines = false; boolean hasTokens = false; boolean bufferHasTokens = false; + boolean lineHasTokens = false; int hasTextOnFirstLine = 0; boolean firstWord = true; boolean clearBlankLines = this.formatter.preferences.comment_clear_blank_lines_in_block_comment; @@ -1445,6 +1447,7 @@ // Look at specific tokens boolean insertSpace = (previousToken == TerminalTokens.TokenNameWHITESPACE) && (!firstWord || !hasTokens); + boolean isTokenStar = false; switch (token) { case TerminalTokens.TokenNameWHITESPACE: if (tokensBuffer.length() > 0) { @@ -1463,7 +1466,7 @@ } else { previousToken = token; } - lineNumber = Util.getLineNumber(this.scanner.getCurrentTokenEndPosition(), this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); + lineNumber = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); if (lineNumber > scannerLine) { hasMultiLines = true; newLine = true; @@ -1471,8 +1474,12 @@ scannerLine = lineNumber; continue; case TerminalTokens.TokenNameMULTIPLY: + isTokenStar = true; + lineNumber = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); + if (lineNumber == firstLine && previousToken == SKIP_FIRST_WHITESPACE_TOKEN) { + editStart = this.scanner.getCurrentTokenStartPosition(); + } previousToken = token; - lineNumber = Util.getLineNumber(this.scanner.getCurrentTokenEndPosition(), this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); if (this.scanner.currentCharacter == '/') { editEnd = this.scanner.startPosition - 1; // Add remaining buffered tokens @@ -1491,14 +1498,18 @@ this.scanner.getNextChar(); // reach the end of scanner continue; } - scannerLine = lineNumber; - continue; + if (newLine) { + scannerLine = lineNumber; + newLine = false; + continue; + } + break; case TerminalTokens.TokenNameMULTIPLY_EQUAL: if (newLine) { this.scanner.resetTo(this.scanner.startPosition, currentTokenEndPosition-1); this.scanner.getNextChar(); // consume the multiply previousToken = TerminalTokens.TokenNameMULTIPLY; - scannerLine = Util.getLineNumber(this.scanner.getCurrentTokenEndPosition(), this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); + scannerLine = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); continue; } break; @@ -1524,7 +1535,7 @@ // Look at gap and insert corresponding lines if necessary int linesGap; int max; - lineNumber = Util.getLineNumber(this.scanner.getCurrentTokenEndPosition(), this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); + lineNumber = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines); if (lastTextLine == -1) { linesGap = lineNumber - firstLine; max = 0; @@ -1534,7 +1545,7 @@ // insert one blank line before root tags linesGap = 2; } - max = joinLines ? 1 : 0; + max = joinLines && lineHasTokens ? 1 : 0; } if (linesGap > max) { if (clearBlankLines) { @@ -1566,12 +1577,14 @@ } insertSpace = insertSpace && linesGap == 0; } + if (newLine) lineHasTokens = false; // Increment column int tokenStart = this.scanner.getCurrentTokenStartPosition(); int tokenLength = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - tokenStart; hasTokens = true; - if (hasTextOnFirstLine == 0) { + if (!isTokenStar) lineHasTokens = true; + if (hasTextOnFirstLine == 0 && !isTokenStar) { if (firstLine == lineNumber) { hasTextOnFirstLine = 1; this.column++; // include first space @@ -1583,7 +1596,7 @@ if (insertSpace) lastColumn++; // Append next token inserting a new line if max line is reached - if (!firstWord && lastColumn > maxColumn) { + if (lineHasTokens && !firstWord && lastColumn > maxColumn) { String tokensString = tokensBuffer.toString().trim(); // not enough space on the line if (hasTextOnFirstLine == 1) { @@ -2045,6 +2058,7 @@ private void printLineComment() { int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; + boolean includesLineComments = includesLineComments(); boolean isNlsTag = false; if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition, currentTokenEndPosition) != -1) { this.nlsTagCounter = 0; @@ -2068,7 +2082,7 @@ this.pendingSpace = false; int previousStart = currentTokenStartPosition; - if (!isNlsTag && includesLineComments()) { + if (!isNlsTag && includesLineComments) { printLineComment(currentTokenStartPosition, currentTokenEndPosition-1); } else { // do nothing!? Index: compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java,v retrieving revision 1.86 diff -u -r1.86 AbstractCommentParser.java --- compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java 24 Oct 2008 15:29:57 -0000 1.86 +++ compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java 8 Jan 2009 17:13:04 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2009 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 @@ -322,9 +322,9 @@ if (previousChar != '*') { this.starPosition = previousPosition; if (isDomParser || isFormatterParser) { - if (lineHasStar && !this.lineStarted) { + if (lineHasStar) { this.lineStarted = true; - this.textStart = previousPosition; + if (this.textStart == -1) this.textStart = previousPosition; } if (!this.lineStarted) { lineHasStar = true;