### 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.200 diff -u -r1.200 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 11 Mar 2010 11:27:29 -0000 1.200 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 16 Mar 2010 09:42:31 -0000 @@ -2217,15 +2217,12 @@ // then it becomes not a good idea to change the trailing flag if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) { int currentCommentIndentation = getCurrentIndentation(whiteSpaces, 0); - int lastCommentIndentation = this.lastLineComment.currentIndentation; - if (this.tabLength > 0) { - if ((currentCommentIndentation % this.tabLength) == 0) { - lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength; - } else { - currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength; - } + int relativeIndentation = currentCommentIndentation - this.lastLineComment.currentIndentation; + if (this.tabLength == 0) { + canChangeTrailing = relativeIndentation == 0; + } else { + canChangeTrailing = relativeIndentation > -this.tabLength; } - canChangeTrailing = currentCommentIndentation >= lastCommentIndentation; } // if the trailing can be change, then look at the following tokens if (canChangeTrailing) { @@ -2494,15 +2491,15 @@ int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces, 0); // Keep the current comment indentation when over the previous contiguous line comment // and the previous comment has not been reindented - int lastCommentIndentation = this.lastLineComment.currentIndentation; - if (this.tabLength > 0) { - if ((currentCommentIndentation % this.tabLength) == 0) { - lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength; - } else { - currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength; - } + int relativeIndentation = currentCommentIndentation - this.lastLineComment.currentIndentation; + boolean similarCommentsIndentation = false; + if (this.tabLength == 0) { + similarCommentsIndentation = relativeIndentation == 0; + } else if (relativeIndentation > -this.tabLength) { + similarCommentsIndentation = this.formatter.preferences.comment_format_line_comment_starting_on_first_column || + (currentCommentIndentation != 0 && this.lastLineComment.currentIndentation != 0); } - if (currentCommentIndentation >= lastCommentIndentation && this.lastLineComment.indentation != this.indentationLevel) { + if (similarCommentsIndentation && this.lastLineComment.indentation != this.indentationLevel) { int currentIndentationLevel = this.indentationLevel; this.indentationLevel = this.lastLineComment.indentation ; printIndentationIfNecessary(); #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.59 diff -u -r1.59 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 11 Mar 2010 11:27:26 -0000 1.59 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 16 Mar 2010 09:42:33 -0000 @@ -6281,4 +6281,62 @@ "}\n"); } +/** + * @bug 305371: [formatter] Unexpected indentation of line comment + * @test Verify that comments with too different indentation are not considered as contiguous + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=305371" + */ +public void testBug305371() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); + String source = + "class X01 {\n" + + "// unformatted comment !\n" + + " // formatted comment !\n" + + "}\n"; + formatSource(source, + "class X01 {\n" + + "// unformatted comment !\n" + + " // formatted comment !\n" + + "}\n"); +} +public void testBug305371b() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); + String source = + "class X02 {\n" + + " // formatted comment !\n" + + "// unformatted comment !\n" + + "}\n"; + formatSource(source, + "class X02 {\n" + + " // formatted comment !\n" + + "// unformatted comment !\n" + + "}\n"); +} +public void testBug305371c() { + String source = + "class X03 {\n" + + " // formatted comment 1\n" + + " // formatted comment 2\n" + + "}\n"; + formatSource(source, + "class X03 {\n" + + " // formatted comment 1\n" + + " // formatted comment 2\n" + + "}\n"); +} +public void testBug305371d() { + String source = + "class X04 {\n" + + " // formatted comment 1\n" + + " // formatted comment 2\n" + + "}\n"; + formatSource(source, + "class X04 {\n" + + " // formatted comment 1\n" + + " // formatted comment 2\n" + + "}\n"); +} + }