### Eclipse Workspace Patch 1.0 #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.158 diff -u -r1.158 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 14 Jan 2009 10:48:41 -0000 1.158 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 26 Feb 2009 10:35:12 -0000 @@ -797,14 +797,12 @@ case '\t' : offset += this.tabLength; break; - case ' ' : - offset++; - break; case '\r' : case '\n' : break; default: - return offset; + offset++; + break; } } return offset; @@ -1250,51 +1248,89 @@ this.needSpace = false; this.pendingSpace = false; + int commentColumn = this.column; if (includesBlockComments) { if (printBlockComment(currentTokenStartPosition, currentTokenEndPosition)) { return; } } + int currentIndentationLevel = this.indentationLevel; + if ((commentColumn-1) > this.indentationLevel) { + this.indentationLevel = commentColumn-1; + } int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0); - while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { - nextCharacterStart = this.scanner.currentPosition; - - switch(currentCharacter) { - case '\r' : - start = previousStart; - isNewLine = true; - if (this.scanner.getNextChar('\n')) { - currentCharacter = '\n'; + try { + while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { + nextCharacterStart = this.scanner.currentPosition; + + switch(currentCharacter) { + case '\r' : + start = previousStart; + isNewLine = true; + if (this.scanner.getNextChar('\n')) { + currentCharacter = '\n'; + nextCharacterStart = this.scanner.currentPosition; + } + break; + case '\n' : + start = previousStart; + isNewLine = true; nextCharacterStart = this.scanner.currentPosition; - } - break; - case '\n' : - start = previousStart; - isNewLine = true; - nextCharacterStart = this.scanner.currentPosition; - break; - default: - if (isNewLine) { - this.column = 1; - this.line++; - isNewLine = false; - - StringBuffer buffer = new StringBuffer(); - if (onFirstColumn) { - // simply insert indentation if necessary - buffer.append(this.lineSeparator); - if (indentComment) { - printIndentationIfNecessary(buffer); - } - if (formatComment) { + break; + default: + if (isNewLine) { + this.column = 1; + this.line++; + isNewLine = false; + + StringBuffer buffer = new StringBuffer(); + if (onFirstColumn) { + // simply insert indentation if necessary + buffer.append(this.lineSeparator); + if (indentComment) { + printIndentationIfNecessary(buffer); + } + if (formatComment) { + if (ScannerHelper.isWhitespace((char) currentCharacter)) { + int previousStartPosition = this.scanner.currentPosition; + while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { + previousStart = nextCharacterStart; + previousStartPosition = this.scanner.currentPosition; + currentCharacter = this.scanner.getNextChar(); + nextCharacterStart = this.scanner.currentPosition; + } + if (currentCharacter == '\r' || currentCharacter == '\n') { + nextCharacterStart = previousStartPosition; + } + } + if (currentCharacter != '\r' && currentCharacter != '\n') { + buffer.append(' '); + } + } else { + if (currentCharacter == '*') { + buffer.append(' '); + } + } + } else { if (ScannerHelper.isWhitespace((char) currentCharacter)) { int previousStartPosition = this.scanner.currentPosition; - while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { + int count = 0; + loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { + if (count >= currentCommentOffset) { + break loop; + } previousStart = nextCharacterStart; previousStartPosition = this.scanner.currentPosition; + switch(currentCharacter) { + case '\t' : + count += this.tabLength; + break; + default : + count ++; + } currentCharacter = this.scanner.getNextChar(); nextCharacterStart = this.scanner.currentPosition; } @@ -1302,62 +1338,41 @@ nextCharacterStart = previousStartPosition; } } - if (currentCharacter != '\r' && currentCharacter != '\n') { - buffer.append(' '); + buffer.append(this.lineSeparator); + if (indentComment) { + printIndentationIfNecessary(buffer); } - } - } else { - if (ScannerHelper.isWhitespace((char) currentCharacter)) { - int previousStartPosition = this.scanner.currentPosition; - int count = 0; - loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { - if (count >= currentCommentOffset) { - break loop; + if (formatComment) { + int previousStartTemp = previousStart; + int nextCharacterStartTemp = nextCharacterStart; + while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { + previousStart = nextCharacterStart; + currentCharacter = this.scanner.getNextChar(); + nextCharacterStart = this.scanner.currentPosition; } - previousStart = nextCharacterStart; - previousStartPosition = this.scanner.currentPosition; - switch(currentCharacter) { - case '\t' : - count += this.tabLength; - break; - default : - count ++; + if (currentCharacter == '*') { + buffer.append(' '); + } else { + previousStart = previousStartTemp; + nextCharacterStart = nextCharacterStartTemp; } - currentCharacter = this.scanner.getNextChar(); - nextCharacterStart = this.scanner.currentPosition; - } - if (currentCharacter == '\r' || currentCharacter == '\n') { - nextCharacterStart = previousStartPosition; - } - } - buffer.append(this.lineSeparator); - if (indentComment) { - printIndentationIfNecessary(buffer); - } - if (formatComment) { - int previousStartTemp = previousStart; - int nextCharacterStartTemp = nextCharacterStart; - while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { - previousStart = nextCharacterStart; - currentCharacter = this.scanner.getNextChar(); - nextCharacterStart = this.scanner.currentPosition; - } - if (currentCharacter == '*') { - buffer.append(' '); + this.scanner.currentPosition = nextCharacterStart; } else { - previousStart = previousStartTemp; - nextCharacterStart = nextCharacterStartTemp; + if (currentCharacter == '*') { + buffer.append(' '); + } } - this.scanner.currentPosition = nextCharacterStart; } + addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); + } else { + this.column += (nextCharacterStart - previousStart); } - addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); - } else { - this.column += (nextCharacterStart - previousStart); - } + } + previousStart = nextCharacterStart; + this.scanner.currentPosition = nextCharacterStart; } - previousStart = nextCharacterStart; - this.scanner.currentPosition = nextCharacterStart; + } finally { + this.indentationLevel = currentIndentationLevel; } this.lastNumberOfNewLines = 0; this.needSpace = false; @@ -1366,7 +1381,6 @@ private boolean printBlockComment(int currentTokenStartPosition, int currentTokenEndPosition) { - // Compute indentation int maxColumn = this.formatter.preferences.comment_line_length + 1; int indentLevel = this.indentationLevel; #P org.eclipse.jdt.core.tests.model Index: workspace/Formatter/test714/A_out.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/workspace/Formatter/test714/A_out.java,v retrieving revision 1.2 diff -u -r1.2 A_out.java --- workspace/Formatter/test714/A_out.java 16 Apr 2008 21:30:57 -0000 1.2 +++ workspace/Formatter/test714/A_out.java 26 Feb 2009 10:35:18 -0000 @@ -7,8 +7,8 @@ Cloneable, Serializable { /** - * Method javadoc comment to test that javadoc will be indented though no formatting should occur, as the javadoc formatting is disabled - */ + * Method javadoc comment to test that javadoc will be indented though no formatting should occur, as the javadoc formatting is disabled + */ public static void foo() throws IOException { } } 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.34 diff -u -r1.34 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 14 Jan 2009 10:48:43 -0000 1.34 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 26 Feb 2009 10:35:15 -0000 @@ -38,6 +38,366 @@ } /** + * @bug 198963: [formatter] 3.3 Code Formatter repeatedly indents block comment + * @test Ensure that no the formatter indents the block comment only once + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=198963" + */ +public void testBug198963_Tabs01() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Tabs02() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Tabs03() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Tabs04() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Tabs05() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} +public void testBug198963_Tabs06() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.TAB; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} +public void testBug198963_Spaces01() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Spaces02() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Spaces03() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Spaces04() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Spaces05() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} +public void testBug198963_Spaces06() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} +public void testBug198963_Mixed01() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Mixed02() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 10; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Mixed03() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 100; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Mixed04() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " int x = 0; /*\n" + + " * XXXX\n" + + " */\n" + + "}" + ); +} +public void testBug198963_Mixed05() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} +public void testBug198963_Mixed06() { + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.MIXED; + String source = + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}"; + formatSource(source, + "public class Test {\n" + + "\n" + + " /*\n" + + " * XXXX\n" + + " */\n" + + " int x = 0;\n" + + "}" + ); +} + +/** * @bug 204091: [formatter] format region in comment introduces comment start/end tokens * @test Ensure that a region inside a javadoc comment is well formatted * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=204091" Index: workspace/Formatter/test666/A_out.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/workspace/Formatter/test666/A_out.java,v retrieving revision 1.1 diff -u -r1.1 A_out.java --- workspace/Formatter/test666/A_out.java 24 Apr 2007 15:46:08 -0000 1.1 +++ workspace/Formatter/test666/A_out.java 26 Feb 2009 10:35:18 -0000 @@ -1,6 +1,6 @@ /* My comment. Blah blah blah. -*/ + */ class A { } \ No newline at end of file Index: workspace/Formatter/test665/A_out.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/workspace/Formatter/test665/A_out.java,v retrieving revision 1.1 diff -u -r1.1 A_out.java --- workspace/Formatter/test665/A_out.java 24 Apr 2007 15:42:07 -0000 1.1 +++ workspace/Formatter/test665/A_out.java 26 Feb 2009 10:35:18 -0000 @@ -3,6 +3,6 @@ This is some more text and some more -*/ + */ class A { } \ No newline at end of file