### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core 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.17 diff -u -r1.17 Location.java --- formatter/org/eclipse/jdt/internal/formatter/Location.java 7 Dec 2009 18:17:36 -0000 1.17 +++ formatter/org/eclipse/jdt/internal/formatter/Location.java 8 Dec 2009 16:28:01 -0000 @@ -18,6 +18,7 @@ public class Location { public int inputOffset; + /** deprecated */ public int inputColumn; public int outputLine; public int outputColumn; @@ -43,7 +44,7 @@ this.outputColumn = scribe.column; this.outputLine = scribe.line; this.inputOffset = sourceRestart; - this.inputColumn = scribe.getCurrentColumn(sourceRestart); + this.inputColumn = scribe.getCurrentIndentation(sourceRestart) + 1; this.outputIndentationLevel = scribe.indentationLevel; this.lastNumberOfNewLines = scribe.lastNumberOfNewLines; this.needSpace = scribe.needSpace; Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v retrieving revision 1.183 diff -u -r1.183 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 7 Dec 2009 18:17:36 -0000 1.183 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 8 Dec 2009 16:28:01 -0000 @@ -117,7 +117,7 @@ // Class to store previous line comment information class LineComment { boolean contiguous = false; - int currentColumn, indentation; + int currentIndentation, indentation; int lines; char[] leadingSpaces; } @@ -798,9 +798,9 @@ return null; } - private int getCurrentCommentColumn(int start) { + private int getCurrentCommentIndentation(int start) { int linePtr = -Arrays.binarySearch(this.lineEnds, start); - int commentColumn = 1; + int indentation = 0; int beginningOfLine = getLineEnd(linePtr - 1)+1; if (beginningOfLine == -1) { beginningOfLine = 0; @@ -822,59 +822,59 @@ switch (currentCharacter) { case '\t' : if (this.tabLength != 0) { - int reminder = commentColumn % this.tabLength; + int reminder = indentation % this.tabLength; if (reminder == 0) { - commentColumn += this.tabLength; + indentation += this.tabLength; } else { - commentColumn = ((commentColumn / this.tabLength) + 1) * this.tabLength + 1; + indentation = ((indentation / this.tabLength) + 1) * this.tabLength; } } break; case '\r' : case '\n' : - commentColumn = 0; + indentation = 0; break; default: - commentColumn++; + indentation++; break; } } - return commentColumn; + return indentation; } - int getCurrentColumn(char[] whitespaces) { + int getCurrentIndentation(char[] whitespaces) { int length = whitespaces.length; if (this.tabLength == 0) return length; - int currentColumn = 1; + int indentation = 0; for (int i=0; i this.indentationLevel) { this.indentationLevel = commentColumn-1; } - int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentColumn(start) - 1; + int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentIndentation(start); boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0); try { @@ -2177,16 +2177,16 @@ // when following comment column (after having been rounded) is below the preceding one, // then it becomes not a good idea to change the trailing flag if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) { - int currentCommentColumn = getCurrentColumn(whiteSpaces); - int lastCommentColumn = this.lastLineComment.currentColumn; + int currentCommentIndentation = getCurrentIndentation(whiteSpaces); + int lastCommentIndentation = this.lastLineComment.currentIndentation; if (this.tabLength > 0) { - if ((currentCommentColumn % this.tabLength) == 0) { - lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength; + if ((currentCommentIndentation % this.tabLength) == 0) { + lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength; } else { - currentCommentColumn = ((currentCommentColumn / this.tabLength) + 1) * this.tabLength; + currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength; } } - canChangeTrailing = currentCommentColumn >= lastCommentColumn; + canChangeTrailing = currentCommentIndentation >= lastCommentIndentation; } // if the trailing can be change, then look at the following tokens if (canChangeTrailing) { @@ -2409,18 +2409,18 @@ // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300 if (this.lastLineComment.contiguous) { // The leading spaces have been set while looping in the printComment(int) method - int currentCommentIndentation = getCurrentColumn(this.lastLineComment.leadingSpaces); + int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces); // Keep the current comment indentation when over the previous contiguous line comment // and the previous comment has not been reindented - int lastCommentColumn = this.lastLineComment.currentColumn; + int lastCommentIndentation = this.lastLineComment.currentIndentation; if (this.tabLength > 0) { if ((currentCommentIndentation % this.tabLength) == 0) { - lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength; + lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength; } else { currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength; } } - if (currentCommentIndentation >= lastCommentColumn && this.lastLineComment.indentation != this.indentationLevel) { + if (currentCommentIndentation >= lastCommentIndentation && this.lastLineComment.indentation != this.indentationLevel) { int currentIndentationLevel = this.indentationLevel; this.indentationLevel = this.lastLineComment.indentation ; printIndentationIfNecessary(); @@ -2450,7 +2450,7 @@ // Store line comment information this.lastLineComment.contiguous = true; - this.lastLineComment.currentColumn = getCurrentCommentColumn(currentTokenStartPosition); + this.lastLineComment.currentIndentation = getCurrentCommentIndentation(currentTokenStartPosition); this.lastLineComment.indentation = commentIndentationLevel; // Add pending space if necessary #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.11 diff -u -r1.11 FormatterBugsTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 7 Dec 2009 18:17:32 -0000 1.11 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 8 Dec 2009 16:28:03 -0000 @@ -3465,4 +3465,34 @@ "}\n" ); } + +/** + * @bug 297225: [formatter] Indentation may be still wrong in certain circumstances after formatting + * @test Verify that comment indentation is correct when there's a mix of tab and spaces in + * existing indentation and all comments formatting is off. + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=297225" + */ +public void testBug297225() { + this.formatterPrefs.comment_format_line_comment = false; + this.formatterPrefs.comment_format_block_comment = false; + this.formatterPrefs.comment_format_javadoc_comment = false; + String source = + "public class X01 {\n" + + " \n" + + " /**\n" + + " * The foo method\n" + + " */\n" + + " void foo() {}\n" + + "}\n"; + formatSource(source, + "public class X01 {\n" + + "\n" + + " /**\n" + + " * The foo method\n" + + " */\n" + + " void foo() {\n" + + " }\n" + + "}\n" + ); +} }