### 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.111 diff -u -r1.111 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 26 Nov 2007 16:05:56 -0000 1.111 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 29 Nov 2007 15:09:56 -0000 @@ -15,6 +15,7 @@ import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.core.compiler.InvalidInputException; +import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.internal.compiler.ASTVisitor; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; @@ -26,7 +27,10 @@ import org.eclipse.jdt.internal.core.util.RecordedParsingInformation; import org.eclipse.jdt.internal.formatter.align.Alignment; import org.eclipse.jdt.internal.formatter.align.AlignmentException; +import org.eclipse.jdt.internal.formatter.comment.CommentRegion; +import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; import org.eclipse.text.edits.MultiTextEdit; import org.eclipse.text.edits.ReplaceEdit; @@ -942,45 +946,97 @@ this.needSpace = false; this.pendingSpace = false; - int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); - boolean formatComment = (isJavadoc && formatJavadocComment) || (!isJavadoc && formatBlockComment); - - 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'; + if (isJavadoc) { + if (this.formatter.preferences.comment_format_javadoc_comment) { + if (this.pendingSpace) { + this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ + } + CommentRegion region = DefaultCodeFormatter.createRegion( + CodeFormatter.K_JAVA_DOC, + new Document(new String(this.scanner.source, currentTokenStartPosition, currentTokenEndPosition - currentTokenStartPosition + 1)), + new Position(currentTokenStartPosition, currentTokenEndPosition), + this.formatter); + region.format(); + } + } else if (this.formatter.preferences.comment_format_block_comment && false) { + if (this.pendingSpace) { + this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ + } + CommentRegion region = DefaultCodeFormatter.createRegion( + CodeFormatter.K_MULTI_LINE_COMMENT, + new Document(new String(this.scanner.source, currentTokenStartPosition, currentTokenEndPosition - currentTokenStartPosition + 1)), + new Position(currentTokenStartPosition, currentTokenEndPosition), + this.formatter); + region.format(); + } else { + + int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); + boolean formatComment = (isJavadoc && formatJavadocComment) || (!isJavadoc && formatBlockComment); + + 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 (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; } @@ -988,62 +1044,35 @@ 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(' '); - } else { - previousStart = previousStartTemp; - nextCharacterStart = nextCharacterStartTemp; + this.scanner.currentPosition = nextCharacterStart; } - 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; } this.lastNumberOfNewLines = 0; needSpace = false; @@ -1052,7 +1081,7 @@ printNewLine(); } } - + public void printEndOfCompilationUnit() { try { // if we have a space between two tokens we ensure it will be dumped in the formatted string @@ -1214,7 +1243,7 @@ } else { addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); } - currentTokenStartPosition = this.scanner.currentPosition; + currentTokenStartPosition = this.scanner.currentPosition; break; case TerminalTokens.TokenNameCOMMENT_LINE : if (count >= 1) { @@ -1229,7 +1258,7 @@ hasWhitespace = false; this.printLineComment(this.scanner.getRawTokenSource()); currentTokenStartPosition = this.scanner.currentPosition; - hasLineComment = true; + hasLineComment = true; count = 0; break; case TerminalTokens.TokenNameCOMMENT_BLOCK : @@ -1321,6 +1350,14 @@ this.column = 1; this.lastNumberOfNewLines = 1; } + if (false && this.formatter.preferences.comment_format_line_comment) { + CommentRegion region = DefaultCodeFormatter.createRegion( + CodeFormatter.K_SINGLE_LINE_COMMENT, + new Document(new String(this.scanner.source, currentTokenStartPosition, currentTokenEndPosition - currentTokenStartPosition)), + new Position(currentTokenStartPosition, currentTokenEndPosition), + this.formatter); + region.format(); + } this.needSpace = false; this.pendingSpace = false; // realign to the proper value Index: formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java,v retrieving revision 1.4 diff -u -r1.4 CommentLine.java --- formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java 29 Mar 2006 03:00:04 -0000 1.4 +++ formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java 29 Nov 2007 15:09:56 -0000 @@ -20,7 +20,7 @@ * * @since 3.0 */ -public abstract class CommentLine implements IBorderAttributes { +public abstract class CommentLine implements IBorderAttributes, ICommentAttributes { /** Prefix of non-formattable comment lines */ protected static final String NON_FORMAT_START_PREFIX= "/*-"; //$NON-NLS-1$ @@ -172,7 +172,11 @@ buffer.append(fParent.getDelimiter()); buffer.append(indentation); - buffer.append(content); + if (range.hasAttribute(COMMENT_BLANKLINE)) { // handle blank lines in upper border + buffer.append(this.getTrimmedContentPrefix()); + } else { + buffer.append(content); + } } fParent.logEdit(buffer.toString(), 0, range.getOffset()); } @@ -183,6 +187,13 @@ * @return line prefix of content lines */ protected abstract String getContentPrefix(); + + /** + * Returns the line prefix of content lines, without trailing blanks. + * + * @return line prefix of content lines, without trailing blanks + */ + protected abstract String getTrimmedContentPrefix(); /** * Returns the line prefix of end lines. Index: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java,v retrieving revision 1.5 diff -u -r1.5 MultiCommentLine.java --- formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java 6 Mar 2007 02:38:50 -0000 1.5 +++ formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java 29 Nov 2007 15:09:56 -0000 @@ -27,6 +27,9 @@ /** Line prefix of multi-line comment content lines */ public static final String MULTI_COMMENT_CONTENT_PREFIX= " * "; //$NON-NLS-1$ + + /** Line prefix of multi-line comment content lines without trailing blank */ + public static final String MULTI_COMMENT_CONTENT_PREFIX_TRIMMED= " *"; //$NON-NLS-1$ /** Line prefix of multi-line comment end lines */ public static final String MULTI_COMMENT_END_PREFIX= " */"; //$NON-NLS-1$ @@ -121,6 +124,10 @@ protected String getContentPrefix() { return MULTI_COMMENT_CONTENT_PREFIX; } + + protected String getTrimmedContentPrefix() { + return MULTI_COMMENT_CONTENT_PREFIX_TRIMMED; + } /* * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getEndLinePrefix() @@ -160,8 +167,9 @@ int offset= 0; int postfix= 0; - - String text= parent.getText(range.getOffset(), range.getLength()); + + int initialLength = range.getLength(); + String text= parent.getText(range.getOffset(), initialLength); if (line == 0) { offset= text.indexOf(start); @@ -240,7 +248,6 @@ offset= -1; if (offset >= 0) { - offset += content.length(); range.trimBegin(offset); } Index: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java,v retrieving revision 1.7 diff -u -r1.7 MultiCommentRegion.java --- formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java 6 Mar 2007 02:38:50 -0000 1.7 +++ formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java 29 Nov 2007 15:09:56 -0000 @@ -126,7 +126,9 @@ final String delimiter= super.getDelimiter(predecessor, successor, previous, next, indentation); if (previous != null) { - + if (previous.hasAttribute(COMMENT_BLANKLINE) && next.hasAttribute(COMMENT_FIRST_TOKEN)) { + return delimiter.substring(0, delimiter.length() - 1); + } // Blank line before
 tag
 			if (previous.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !next.hasAttribute(COMMENT_CODE) && !successor.hasAttribute(COMMENT_BLANKLINE))
 				return delimiter + delimiter;
Index: formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java,v
retrieving revision 1.3
diff -u -r1.3 SingleCommentLine.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java	10 May 2006 18:03:41 -0000	1.3
+++ formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java	29 Nov 2007 15:09:56 -0000
@@ -20,6 +20,9 @@
 
 	/** Line prefix for single line comments */
 	public static final String SINGLE_COMMENT_PREFIX= "// "; //$NON-NLS-1$
+	
+	/** Line prefix for single line comments without trailing blank*/
+	public static final String SINGLE_COMMENT_PREFIX_TRIMMED= "//"; //$NON-NLS-1$
 
 	/** NLS tag prefix */
 	private static final String NLS_TAG_PREFIX= "//$NON-NLS-"; //$NON-NLS-1$
@@ -70,6 +73,13 @@
 	protected String getContentPrefix() {
 		return SINGLE_COMMENT_PREFIX;
 	}
+	
+	/*
+	 * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getContentPrefix()
+	 */
+	protected String getTrimmedContentPrefix() {
+		return SINGLE_COMMENT_PREFIX_TRIMMED;
+	}
 
 	/*
 	 * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getEndingPrefix()
Index: formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java,v
retrieving revision 1.7
diff -u -r1.7 CommentRegion.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java	6 Mar 2007 02:38:50 -0000	1.7
+++ formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java	29 Nov 2007 15:09:56 -0000
@@ -161,6 +161,23 @@
 
 	/**
 	 * Formats the comment region with the given indentation level.
+	 * @since 3.4
+	 */
+	public final void format() {
+		final String probe= getText(0, CommentLine.NON_FORMAT_START_PREFIX.length());
+		if (!probe.startsWith(CommentLine.NON_FORMAT_START_PREFIX)) {
+			int margin= this.preferences.comment_line_length;
+			String indentation= computeIndentation(this.scribe.indentationLevel);
+			margin= Math.max(COMMENT_PREFIX_LENGTH + 1, margin - stringToLength(indentation) - COMMENT_PREFIX_LENGTH);
+
+			tokenizeRegion();
+			markRegion();
+			wrapRegion(margin);
+			formatRegion(indentation, margin);		}
+	}
+
+	/**
+	 * Formats the comment region with the given indentation level.
 	 * 
 	 * @param indentationLevel the indentation level
 	 * @return the resulting text edit of the formatting process
@@ -236,7 +253,8 @@
 	 * @return the line delimiter for this comment line break
 	 */
 	protected String getDelimiter(final CommentLine predecessor, final CommentLine successor, final CommentRange previous, final CommentRange next, final String indentation) {
-		return fDelimiter + indentation + successor.getContentPrefix();
+		String contenPrefix = next.hasAttribute(COMMENT_BLANKLINE) ? successor.getTrimmedContentPrefix() : successor.getContentPrefix();
+		return fDelimiter + indentation + contenPrefix;
 	}
 
 	/**