### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/core/formatter/CodeFormatter.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/CodeFormatter.java,v retrieving revision 1.26 diff -u -r1.26 CodeFormatter.java --- formatter/org/eclipse/jdt/core/formatter/CodeFormatter.java 27 Aug 2009 15:27:02 -0000 1.26 +++ formatter/org/eclipse/jdt/core/formatter/CodeFormatter.java 18 Feb 2010 12:14:06 -0000 @@ -24,25 +24,53 @@ /** * Unknown kind - * - * Note that since 3.4, the {@link #F_INCLUDE_COMMENTS} flag can be added - * to this constant in order to get the comments formatted if a compilation unit - * is processed. + *
+ * Since 3.6, if the corresponding comment options are set to
+ * true
then it is also possible to format the comments on the fly
+ * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format.
+ *
+ * Note that using this constant, the comments encountered while formatting + * the expression may be shifted to match the correct indentation but are not + * formatted. + *
+ * Since 3.6, if the corresponding comment options are set to
+ * true
then it is also possible to format the comments on the fly
+ * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format.
+ *
+ * Note that using this constant, the comments encountered while formatting + * the statements may be shifted to match the correct indentation but are not + * formatted. + *
+ * Since 3.6, if the corresponding comment options are set to
+ * true
then it is also possible to format the comments on the fly
+ * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format.
+ *
+ * Note that using this constant, the comments encountered while formatting + * the body declarations may be shifted to match the correct indentation but + * are not formatted. + *
+ * Since 3.6, if the corresponding comment options are set to
+ * true
then it is also possible to format the comments on the fly
+ * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format.
+ *
* Note that using this constant, the comments are only indented while - * processing the compilation unit. + * formatting the compilation unit. *
* Since 3.4, if the corresponding comment option is set to
* true
then it is also possible to format the comments on the fly
* by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format.
+ *
- * This flag can only be combined with {@link #K_COMPILATION_UNIT} and - * {@link #K_UNKNOWN} kinds for now but might be extended to other ones - * in future versions. + * This flag can be combined with the following kinds: + *
* Note also that it has an effect only when one or several format comments
* options for
Index: formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java,v
retrieving revision 1.226
diff -u -r1.226 CodeFormatterVisitor.java
--- formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 18 Feb 2010 10:41:32 -0000 1.226
+++ formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 18 Feb 2010 12:14:08 -0000
@@ -118,7 +118,6 @@
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil;
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.IRegion;
import org.eclipse.text.edits.TextEdit;
@@ -887,38 +886,6 @@
}
/**
- * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String)
- */
- public TextEdit format(String string, CommentRegion region) {
- // reset the scribe
- this.scribe.reset();
-
- if (region == null) {
- return failedToFormat();
- }
-
- long startTime = 0;
- if (DEBUG){
- startTime = System.currentTimeMillis();
- }
-
- final char[] compilationUnitSource = string.toCharArray();
-
- this.scribe.initializeScanner(compilationUnitSource);
-
- TextEdit result = null;
- try {
- result = region.format(this.preferences.initial_indentation_level, true);
- } catch(AbortFormatting e){
- return failedToFormat();
- }
- if (DEBUG){
- System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$
- }
- return result;
- }
-
- /**
* @param source the source of the comment to format
*/
public void formatComment(int kind, String source, int start, int end, int indentationLevel) {
Index: formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java,v
retrieving revision 1.79
diff -u -r1.79 DefaultCodeFormatter.java
--- formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java 13 Jan 2010 15:13:44 -0000 1.79
+++ formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java 18 Feb 2010 12:14:08 -0000
@@ -28,15 +28,8 @@
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
import org.eclipse.jdt.internal.compiler.util.Util;
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil;
-import org.eclipse.jdt.internal.formatter.comment.CommentRegion;
-import org.eclipse.jdt.internal.formatter.comment.JavaDocRegion;
-import org.eclipse.jdt.internal.formatter.comment.MultiCommentRegion;
-import org.eclipse.jface.text.Document;
-import org.eclipse.jface.text.IDocument;
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.TextEdit;
public class DefaultCodeFormatter extends CodeFormatter {
@@ -56,40 +49,9 @@
| K_MULTI_LINE_COMMENT
| K_JAVA_DOC;
- /*
- * Temporary internal statics to enable new comments formatter
- * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
- * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=227043
- * TODO (frederic) remove in 3.5
- */
- private final static String NEW_COMMENTS_FORMAT = System.getProperty("org.eclipse.jdt.core.formatter.comments.new"); //$NON-NLS-1$
- public static boolean ENABLE_NEW_COMMENTS_FORMAT = !JavaCore.DISABLED.equals(NEW_COMMENTS_FORMAT);
-
// Scanner use to probe the kind of the source given to the formatter
private static Scanner PROBING_SCANNER;
- /**
- * Creates a comment region for a specific document partition type.
- *
- * @param kind the comment snippet kind
- * @param document the document which contains the comment region
- * @param range range of the comment region in the document
- * @return a new comment region for the comment region range in the
- * document
- * @since 3.1
- */
- public static CommentRegion createRegion(int kind, IDocument document, Position range, CodeFormatterVisitor formatter) {
- switch (kind & K_MASK) {
- case K_SINGLE_LINE_COMMENT:
- return new CommentRegion(document, range, formatter);
- case K_MULTI_LINE_COMMENT:
- return new MultiCommentRegion(document, range, formatter);
- case K_JAVA_DOC:
- return new JavaDocRegion(document, range, formatter);
- }
- return null;
- }
-
private CodeSnippetParsingUtil codeSnippetParsingUtil;
private Map defaultCompilerOptions;
@@ -174,17 +136,11 @@
case K_JAVA_DOC :
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
// use the integrated comment formatter to format comment
- if (ENABLE_NEW_COMMENTS_FORMAT) {
- return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
- }
+ return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
// $FALL-THROUGH$ - fall through next case when old comment formatter is activated
case K_MULTI_LINE_COMMENT :
case K_SINGLE_LINE_COMMENT :
- if (ENABLE_NEW_COMMENTS_FORMAT) {
- return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
- }
- this.codeSnippetParsingUtil = new CodeSnippetParsingUtil();
- return formatComment(kind, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}, false);
+ return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
}
return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator);
@@ -199,18 +155,17 @@
}
this.codeSnippetParsingUtil = new CodeSnippetParsingUtil();
+ boolean includeComments = (kind & F_INCLUDE_COMMENTS) != 0;
switch(kind & K_MASK) {
case K_CLASS_BODY_DECLARATIONS :
- return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions);
+ return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions, includeComments);
case K_COMPILATION_UNIT :
- boolean includeComments = (kind & F_INCLUDE_COMMENTS) != 0; // || FORCE_NEW_COMMENTS_FORMAT;
return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
case K_EXPRESSION :
- return formatExpression(source, indentationLevel, lineSeparator, regions);
+ return formatExpression(source, indentationLevel, lineSeparator, regions, includeComments);
case K_STATEMENTS :
- return formatStatements(source, indentationLevel, lineSeparator, regions);
+ return formatStatements(source, indentationLevel, lineSeparator, regions, includeComments);
case K_UNKNOWN :
- includeComments = (kind & F_INCLUDE_COMMENTS) != 0; // || FORCE_NEW_COMMENTS_FORMAT;
return probeFormatting(source, indentationLevel, lineSeparator, regions, includeComments);
case K_JAVA_DOC :
case K_MULTI_LINE_COMMENT :
@@ -221,14 +176,14 @@
return null;
}
- private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
+ private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
if (bodyDeclarations == null) {
// a problem occurred while parsing the source
return null;
}
- return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions);
+ return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments);
}
/*
@@ -271,55 +226,6 @@
return null;
}
- /**
- * Returns the resulting text edit after formatting the given comment.
- *
- * @param kind the given kind
- * @param source the given source
- * @param indentationLevel the given indentation level
- * @param lineSeparator the given line separator
- * @param regions the given regions
- * @param includeComments TODO
- * @return the resulting text edit
- */
- private TextEdit formatComment(int kind, String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
- Object oldOption = oldCommentFormatOption();
- boolean isFormattingComments = false;
- if (oldOption == null) {
- switch (kind & K_MASK) {
- case K_SINGLE_LINE_COMMENT:
- isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT));
- break;
- case K_MULTI_LINE_COMMENT:
- isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT));
- break;
- case K_JAVA_DOC:
- isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT));
- }
- } else {
- isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(oldOption);
- }
- if (isFormattingComments) {
- if (lineSeparator != null) {
- this.preferences.line_separator = lineSeparator;
- } else {
- this.preferences.line_separator = Util.LINE_SEPARATOR;
- }
- this.preferences.initial_indentation_level = indentationLevel;
- this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, null, includeComments);
-
- IRegion coveredRegion = getCoveredRegion(regions);
- int offset = coveredRegion.getOffset();
- int length = coveredRegion.getLength();
-
- final CommentRegion region = createRegion(kind, new Document(source), new Position(offset, length), this.newCodeFormatter);
- if (region != null) {
- return this.newCodeFormatter.format(source, region);
- }
- }
- return new MultiTextEdit();
- }
-
private TextEdit formatCompilationUnit(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
CompilationUnitDeclaration compilationUnitDeclaration = this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true);
@@ -335,24 +241,24 @@
return this.newCodeFormatter.format(source, compilationUnitDeclaration);
}
- private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
+ private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
if (expression == null) {
// a problem occurred while parsing the source
return null;
}
- return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions);
+ return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments);
}
- private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
+ private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
if (constructorDeclaration.statements == null) {
// a problem occured while parsing the source
return null;
}
- return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions);
+ return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments);
}
private IRegion getCoveredRegion(IRegion[] regions) {
@@ -443,7 +349,7 @@
return this.defaultCompilerOptions;
}
- private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, IRegion[] regions) {
+ private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, IRegion[] regions, boolean includeComments) {
if (lineSeparator != null) {
this.preferences.line_separator = lineSeparator;
} else {
@@ -451,11 +357,11 @@
}
this.preferences.initial_indentation_level = indentationLevel;
- this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
+ this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
return this.newCodeFormatter.format(source, bodyDeclarations);
}
- private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, IRegion[] regions) {
+ private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, IRegion[] regions, boolean includeComments) {
if (lineSeparator != null) {
this.preferences.line_separator = lineSeparator;
} else {
@@ -463,13 +369,13 @@
}
this.preferences.initial_indentation_level = indentationLevel;
- this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
+ this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
TextEdit textEdit = this.newCodeFormatter.format(source, expression);
return textEdit;
}
- private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions) {
+ private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions, boolean includeComments) {
if (lineSeparator != null) {
this.preferences.line_separator = lineSeparator;
} else {
@@ -477,7 +383,7 @@
}
this.preferences.initial_indentation_level = indentationLevel;
- this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
+ this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
return this.newCodeFormatter.format(source, constructorDeclaration);
}
@@ -523,13 +429,7 @@
break;
}
if (kind != -1) {
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=227043
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
- // use the integrated comment formatter to format comment
- if (ENABLE_NEW_COMMENTS_FORMAT /*|| FORCE_NEW_COMMENTS_FORMAT*/) {
- return formatComment(kind, source, indentationLevel, lineSeparator, regions);
- }
- return formatComment(kind, source, indentationLevel, lineSeparator, regions, includeComments);
+ return formatComment(kind, source, indentationLevel, lineSeparator, regions);
}
} catch (InvalidInputException e) {
// ignore
@@ -539,19 +439,19 @@
// probe for expression
Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
if (expression != null) {
- return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions);
+ return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments);
}
// probe for body declarations (fields, methods, constructors)
ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
if (bodyDeclarations != null) {
- return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions);
+ return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments);
}
// probe for statements
ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
if (constructorDeclaration.statements != null) {
- return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions);
+ return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments);
}
// this has to be a compilation unit
Index: formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/CommentLine.java 7 Mar 2009 01:08:08 -0000 1.7
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,315 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.util.LinkedList;
-
-import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
-
-/**
- * General comment line in a comment region.
- *
- * @since 3.0
- */
-public abstract class CommentLine implements IBorderAttributes {
-
- /** Prefix of non-formattable comment lines */
- protected static final String NON_FORMAT_START_PREFIX= "/*-"; //$NON-NLS-1$
-
- /** The attributes of this line */
- private int fAttributes= 0;
-
- /** The parent region of this line */
- private final CommentRegion fParent;
-
- /** The comment ranges in this line */
- private final LinkedList fRanges= new LinkedList();
-
- /**
- * Creates a new comment line.
- *
- * @param parent comment region to create the comment line for
- */
- protected CommentLine(final CommentRegion parent) {
- this.fParent= parent;
- }
-
- /**
- * Adapts the line attributes from the previous line in the comment
- * region.
- *
- * @param previous the previous comment line in the comment region
- */
- protected abstract void adapt(final CommentLine previous);
-
- /**
- * Appends the specified comment range to this comment line.
- *
- * @param range comment range to append to this line
- */
- protected void append(final CommentRange range) {
- this.fRanges.add(range);
- }
-
- /**
- * Formats this comment line as content line.
- *
- * @param predecessor the predecessor comment line in the comment region
- * @param last the most recently processed comment range
- * @param indentation the indentation of the comment region
- * @param line the index of this comment line in the comment region
- * @return the first comment range in this comment line
- */
- protected CommentRange formatLine(final CommentLine predecessor, final CommentRange last, final String indentation, final int line) {
-
- int offset= 0;
- int length= 0;
-
- CommentRange next= last;
- CommentRange previous= null;
-
- final int stop= this.fRanges.size() - 1;
- final int end= this.fParent.getSize() - 1;
-
- for (int index= stop; index >= 0; index--) {
-
- previous= next;
- next= (CommentRange)this.fRanges.get(index);
-
- if (this.fParent.canFormat(previous, next)) {
-
- offset= next.getOffset() + next.getLength();
- length= previous.getOffset() - offset;
-
- if (index == stop && line != end)
- this.fParent.logEdit(this.fParent.getDelimiter(predecessor, this, previous, next, indentation), offset, length);
- else
- this.fParent.logEdit(this.fParent.getDelimiter(previous, next), offset, length);
- }
- }
- return next;
- }
-
- /**
- * Formats this comment line as end line having a lower border
- * consisting of content line prefixes.
- *
- * @param range last comment range of the last comment line in the
- * comment region
- * @param indentation the indentation of the comment region
- * @param length the maximal length of text in this comment region
- * measured in average character widths
- */
- protected void formatLowerBorder(final CommentRange range, final String indentation, final int length) {
-
- final int offset= range.getOffset() + range.getLength();
-
- final StringBuffer buffer= new StringBuffer(length);
- final String end= getEndingPrefix();
- final String delimiter= this.fParent.getDelimiter();
-
- if (this.fParent.isSingleLine() && this.fParent.getSize() == 1)
- buffer.append(end);
- else {
-
- final String filler= getContentPrefix().trim();
-
- buffer.append(delimiter);
- buffer.append(indentation);
-
- if (this.fParent.hasBorder(BORDER_LOWER)) {
-
- buffer.append(' ');
- for (int character= 0; character < length; character++)
- buffer.append(filler);
-
- buffer.append(end.trim());
-
- } else
- buffer.append(end);
- }
- this.fParent.logEdit(buffer.toString(), offset, this.fParent.getLength() - offset);
- }
-
- /**
- * Formats this comment line as start line having an upper border
- * consisting of content line prefixes.
- *
- * @param range the first comment range in the comment region
- * @param indentation the indentation of the comment region
- * @param length the maximal length of text in this comment region
- * measured in average character widths
- */
- protected void formatUpperBorder(final CommentRange range, final String indentation, final int length) {
-
- final StringBuffer buffer= new StringBuffer(length);
- final String start= getStartingPrefix();
- final String content= getContentPrefix();
-
- if (this.fParent.isSingleLine() && this.fParent.getSize() == 1)
- buffer.append(start);
- else {
-
- final String trimmed= start.trim();
- final String filler= content.trim();
-
- buffer.append(trimmed);
-
- if (this.fParent.hasBorder(BORDER_UPPER)) {
-
- for (int character= 0; character < length - trimmed.length() + start.length(); character++)
- buffer.append(filler);
- }
-
- buffer.append(this.fParent.getDelimiter());
- buffer.append(indentation);
- buffer.append(content);
- }
- this.fParent.logEdit(buffer.toString(), 0, range.getOffset());
- }
-
- /**
- * Returns the line prefix of content lines.
- *
- * @return line prefix of content lines
- */
- protected abstract String getContentPrefix();
-
- /**
- * Returns the line prefix of end lines.
- *
- * @return line prefix of end lines
- */
- protected abstract String getEndingPrefix();
-
- /**
- * Returns the first comment range in this comment line.
- *
- * @return the first comment range
- */
- protected final CommentRange getFirst() {
- return (CommentRange)this.fRanges.getFirst();
- }
-
- /**
- * Returns the indentation reference string for this line.
- *
- * @return the indentation reference string for this line
- */
- protected String getIndentationReference() {
- return ""; //$NON-NLS-1$
- }
-
- /**
- * Returns the last comment range in this comment line.
- *
- * @return the last comment range
- */
- protected final CommentRange getLast() {
- return (CommentRange)this.fRanges.getLast();
- }
-
- /**
- * Returns the parent comment region of this comment line.
- *
- * @return the parent comment region
- */
- protected final CommentRegion getParent() {
- return this.fParent;
- }
-
- /**
- * Returns the number of comment ranges in this comment line.
- *
- * @return the number of ranges in this line
- */
- protected final int getSize() {
- return this.fRanges.size();
- }
-
- /**
- * Returns the line prefix of start lines.
- *
- * @return line prefix of start lines
- */
- protected abstract String getStartingPrefix();
-
- /**
- * Is the attribute attribute
true?
- *
- * @param attribute the attribute to get.
- * @return true
iff this attribute is true
,
- * false
otherwise.
- */
- protected final boolean hasAttribute(final int attribute) {
- return (this.fAttributes & attribute) == attribute;
- }
-
- /**
- * Scans this comment line for comment range boundaries.
- *
- * @param line the index of this line in the comment region
- */
- protected abstract void scanLine(final int line);
-
- /**
- * Set the attribute attribute
to true.
- *
- * @param attribute the attribute to set.
- */
- protected final void setAttribute(final int attribute) {
- this.fAttributes |= attribute;
- }
-
- /**
- * Tokenizes this comment line into comment ranges
- *
- * @param line the index of this line in the comment region
- */
- protected void tokenizeLine(final int line) {
-
- int offset= 0;
- int index= offset;
-
- final CommentRange range= (CommentRange)this.fRanges.get(0);
- final int begin= range.getOffset();
-
- final String content= this.fParent.getText(begin, range.getLength());
- final int length= content.length();
-
- while (offset < length) {
-
- while (offset < length && ScannerHelper.isWhitespace(content.charAt(offset)))
- offset++;
-
- index= offset;
-
- while (index < length && !ScannerHelper.isWhitespace(content.charAt(index)))
- index++;
-
- if (index - offset > 0) {
- this.fParent.append(new CommentRange(begin + offset, index - offset));
-
- offset= index;
- }
- }
- }
-
- public String toString() {
- final StringBuffer buffer = new StringBuffer();
- final int size = this.fRanges.size();
- for (int i = 0; i < size; i++) {
- buffer.append(this.fRanges.get(i)).append("\n"); //$NON-NLS-1$
- }
- return String.valueOf(buffer);
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/CommentRange.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/CommentRange.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/CommentRange.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/CommentRange.java 7 Mar 2009 01:08:08 -0000 1.8
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,262 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eclipse.jdt.core.compiler.CharOperation;
-import org.eclipse.jface.text.Position;
-
-/**
- * Range in a comment region in comment region coordinates.
- *
- * @since 3.0
- */
-public class CommentRange extends Position implements ICommentAttributes, IHtmlTagDelimiters {
-
- /** The attributes of this range */
- private int fAttributes= 0;
-
- /**
- * Creates a new comment range.
- *
- * @param position offset of the range
- * @param count length of the range
- */
- public CommentRange(final int position, final int count) {
- super(position, count);
- }
-
- /**
- * Is the attribute attribute
true?
- *
- * @param attribute the attribute to get
- * @return true
iff this attribute is true
,
- * false
otherwise
- */
- protected final boolean hasAttribute(final int attribute) {
- return (this.fAttributes & attribute) == attribute;
- }
-
- /**
- * Does this comment range contain a closing HTML tag?
- *
- * @param token token belonging to the comment range
- * @param tag the HTML tag to check
- * @return true
iff this comment range contains a closing
- * html tag, false
otherwise
- */
- protected final boolean isClosingTag(final char[] token, final char[] tag) {
-
- boolean result= (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) == 0)
- && token[token.length - 1] == HTML_TAG_POSTFIX;
- if (result) {
-
- setAttribute(COMMENT_CLOSE);
- result= CharOperation.equals(tag, token, HTML_CLOSE_PREFIX.length, token.length - 1, false);
- }
- return result;
- }
-
- /**
- * Does this comment range contain an opening HTML tag?
- *
- * @param token token belonging to the comment range
- * @param tag the HTML tag to check
- * @return true
iff this comment range contains an
- * opening html tag, false
otherwise
- */
- protected final boolean isOpeningTag(final char[] token, final char[] tag) {
-
- boolean result= token.length > 0
- && token[0] == HTML_TAG_PREFIX
- && (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) != 0)
- && token[token.length - 1] == HTML_TAG_POSTFIX;
- if (result) {
-
- setAttribute(COMMENT_OPEN);
- result= CharOperation.indexOf(tag, token, false) == 1;
- }
- return result;
- }
-
- /**
- * Mark the comment range with the occurred HTML tags.
- *
- * @param tags the HTML tags to test for their occurrence
- * @param token token belonging to the comment range
- * @param attribute attribute to set if a HTML tag is present
- * @param open true
iff opening tags should be marked,
- * false
otherwise
- * @param close true
iff closing tags should be marked,
- * false
otherwise
- */
- protected final void markHtmlTag(final char[][] tags, final char[] token, final int attribute, final boolean open, final boolean close) {
- if (token[0] == HTML_TAG_PREFIX && token[token.length - 1] == HTML_TAG_POSTFIX) {
-
- char[] tag= null;
- boolean isOpen= false;
- boolean isClose= false;
-
- for (int index= 0; index < tags.length; index++) {
-
- tag= tags[index];
-
- isOpen= isOpeningTag(token, tag);
- isClose= isClosingTag(token, tag);
-
- if ((open && isOpen) || (close && isClose)) {
-
- setAttribute(attribute);
- break;
- }
- }
- }
- }
-
- /**
- * Mark the comment range with the occurred tags.
- *
- * @param tags the tags to test for their occurrence
- * @param prefix the prefix which is common to all the tags to test
- * @param token the token belonging to the comment range
- * @param attribute attribute to set if a tag is present
- */
- protected final void markPrefixTag(final char[][] tags, final char prefix, final char[] token, final int attribute) {
-
- if (token[0] == prefix) {
-
- char[] tag= null;
- for (int index= 0; index < tags.length; index++) {
-
- tag= tags[index];
- if (CharOperation.equals(token, tag)) {
-
- setAttribute(attribute);
- break;
- }
- }
- }
- }
-
- /**
- * Marks the comment range with the HTML range tag.
- *
- * @param token the token belonging to the comment range
- * @param tag the HTML tag which confines the HTML range
- * @param level the nesting level of the current HTML range
- * @param key the key of the attribute to set if the comment range is in
- * the HTML range
- * @param html true
iff the HTML tags in this HTML range
- * should be marked too, false
otherwise
- * @return the new nesting level of the HTML range
- */
- protected final int markTagRange(final char[] token, final char[] tag, int level, final int key, final boolean html) {
-
- if (isOpeningTag(token, tag)) {
- if (level++ > 0)
- setAttribute(key);
- } else if (isClosingTag(token, tag)) {
- if (--level > 0)
- setAttribute(key);
- } else if (level > 0) {
- if (html || !hasAttribute(COMMENT_HTML))
- setAttribute(key);
- }
- return level;
- }
-
- /**
- * Moves this comment range.
- *
- * @param delta the delta to move the range
- */
- public final void move(final int delta) {
- this.offset += delta;
- }
-
- /**
- * Set the attribute attribute
to true.
- *
- * @param attribute the attribute to set.
- */
- protected final void setAttribute(final int attribute) {
- this.fAttributes |= attribute;
- }
-
- /**
- * Trims this comment range at the beginning.
- *
- * @param delta amount to trim the range
- */
- public final void trimBegin(final int delta) {
- this.offset += delta;
- this.length -= delta;
- }
-
- /**
- * Trims this comment range at the end.
- *
- * @param delta amount to trim the range
- */
- public final void trimEnd(final int delta) {
- this.length += delta;
- }
-
- /*
- * @see java.lang.Object#toString()
- * @since 3.1
- */
- public String toString() {
- List attributes= new ArrayList();
- if (hasAttribute(COMMENT_BLANKLINE))
- attributes.add("COMMENT_BLANKLINE"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_BREAK))
- attributes.add("COMMENT_BREAK"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_CLOSE))
- attributes.add("COMMENT_CLOSE"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_CODE))
- attributes.add("COMMENT_CODE"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_HTML))
- attributes.add("COMMENT_HTML"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_IMMUTABLE))
- attributes.add("COMMENT_IMMUTABLE"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_NEWLINE))
- attributes.add("COMMENT_NEWLINE"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_OPEN))
- attributes.add("COMMENT_OPEN"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_PARAGRAPH))
- attributes.add("COMMENT_PARAGRAPH"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_PARAMETER))
- attributes.add("COMMENT_PARAMETER"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_ROOT))
- attributes.add("COMMENT_ROOT"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_SEPARATOR))
- attributes.add("COMMENT_SEPARATOR"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_FIRST_TOKEN))
- attributes.add("COMMENT_FIRST_TOKEN"); //$NON-NLS-1$
- if (hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER))
- attributes.add("COMMENT_STARTS_WITH_RANGE_DELIMITER"); //$NON-NLS-1$
-
- StringBuffer buf= new StringBuffer("CommentRange [" + this.offset + "+" + this.length + "] {"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- for (Iterator it= attributes.iterator(); it.hasNext();) {
- String string= (String) it.next();
- buf.append(string);
- if (it.hasNext())
- buf.append(", "); //$NON-NLS-1$
- }
-
- return buf.toString() + "}"; //$NON-NLS-1$
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/CommentRegion.java 7 Mar 2009 01:08:08 -0000 1.10
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,576 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-
-import org.eclipse.core.runtime.Assert;
-
-import org.eclipse.text.edits.MalformedTreeException;
-import org.eclipse.text.edits.TextEdit;
-
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.DefaultLineTracker;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.ILineTracker;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.Position;
-
-import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
-import org.eclipse.jdt.internal.formatter.CodeFormatterVisitor;
-import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
-import org.eclipse.jdt.internal.formatter.Scribe;
-
-/**
- * Comment region in a source code document.
- *
- * @since 3.0
- */
-public class CommentRegion extends Position implements IHtmlTagDelimiters, IBorderAttributes, ICommentAttributes {
-
- /** Default comment range delimiter */
- protected static final String COMMENT_RANGE_DELIMITER= " "; //$NON-NLS-1$
-
- /** Default line prefix length */
- private static final int COMMENT_PREFIX_LENGTH= 3;
-
- /** The borders of this region */
- private int fBorders= 0;
-
- /** Should all blank lines be cleared during formatting? */
- protected boolean fClear;
-
- /** The line delimiter used in this comment region */
- private final String fDelimiter;
-
- /** The document to format */
- private final IDocument fDocument;
-
- /** The lines in this comment region */
- private final LinkedList fLines= new LinkedList();
-
- /** The formatting preferences */
- protected final DefaultCodeFormatterOptions preferences;
-
- /** The comment ranges in this comment region */
- private final LinkedList fRanges= new LinkedList();
-
- /** Is this comment region a single line region? */
- private final boolean fSingleLine;
-
- /** Number of spaces representing tabulator */
- private int fTabSize;
-
- /** the scribe used to create edits */
- protected Scribe scribe;
-
- /**
- * Creates a new comment region.
- *
- * @param document the document which contains the comment region
- * @param position the position of this comment region in the document
- * @param formatter the given code formatter
- */
- public CommentRegion(final IDocument document, final Position position, final CodeFormatterVisitor formatter) {
- super(position.getOffset(), position.getLength());
-
- this.preferences = formatter.preferences;
- this.fDelimiter = this.preferences.line_separator;
- this.fDocument= document;
-
- this.fTabSize= DefaultCodeFormatterOptions.SPACE == this.preferences.tab_char ? this.preferences.indentation_size : this.preferences.tab_size;
-
- this.scribe = formatter.scribe;
-
- final ILineTracker tracker= new DefaultLineTracker();
-
- IRegion range= null;
- CommentLine line= null;
-
- tracker.set(getText(0, getLength()));
- final int lines= tracker.getNumberOfLines();
-
- this.fSingleLine= lines == 1;
-
- try {
-
- for (int index= 0; index < lines; index++) {
-
- range= tracker.getLineInformation(index);
- line= createLine();
- line.append(new CommentRange(range.getOffset(), range.getLength()));
-
- this.fLines.add(line);
- }
-
- } catch (BadLocationException exception) {
- // Should not happen
- }
- }
-
- /**
- * Appends the comment range to this comment region.
- *
- * @param range comment range to append to this comment region
- */
- protected final void append(final CommentRange range) {
- this.fRanges.addLast(range);
- }
-
- /**
- * Can the comment range be appended to the comment line?
- *
- * @param line comment line where to append the comment range
- * @param previous comment range which is the predecessor of the current
- * comment range
- * @param next comment range to test whether it can be appended to the
- * comment line
- * @param index amount of space in the comment line used by already
- * inserted comment ranges
- * @param width the maximal width of text in this comment region
- * measured in average character widths
- * @return true
iff the comment range can be added to the
- * line, false
otherwise
- */
- protected boolean canAppend(final CommentLine line, final CommentRange previous, final CommentRange next, final int index, final int width) {
- return index == 0 || index + next.getLength() <= width;
- }
-
- /**
- * Can the whitespace between the two comment ranges be formatted?
- *
- * @param previous previous comment range which was already formatted,
- * can be null
- * @param next next comment range to be formatted
- * @return true
iff the next comment range can be
- * formatted, false
otherwise.
- */
- protected boolean canFormat(final CommentRange previous, final CommentRange next) {
- return previous != null;
- }
-
- /**
- * Formats the comment region with the given indentation level.
- *
- * @param indentationLevel the indentation level
- * @return the resulting text edit of the formatting process
- * @since 3.1
- */
- public final TextEdit format(int indentationLevel, boolean returnEdit) {
- 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(indentationLevel);
- margin= Math.max(COMMENT_PREFIX_LENGTH + 1, margin - stringToLength(indentation) - COMMENT_PREFIX_LENGTH);
-
- tokenizeRegion();
- markRegion();
- wrapRegion(margin);
- formatRegion(indentation, margin);
-
- }
- if (returnEdit) {
- return this.scribe.getRootEdit();
- }
- return null;
- }
-
- /**
- * Formats this comment region.
- *
- * @param indentation the indentation of this comment region
- * @param width the maximal width of text in this comment region
- * measured in average character widths
- */
- protected void formatRegion(final String indentation, final int width) {
-
- final int last= this.fLines.size() - 1;
- if (last >= 0) {
-
- CommentLine lastLine= (CommentLine)this.fLines.get(last);
- CommentRange lastRange= lastLine.getLast();
- lastLine.formatLowerBorder(lastRange, indentation, width);
-
- CommentLine previous;
- CommentLine next= null;
- CommentRange range= null;
- for (int line= last; line >= 0; line--) {
-
- previous= next;
- next= (CommentLine)this.fLines.get(line);
-
- range= next.formatLine(previous, range, indentation, line);
- }
- next.formatUpperBorder(range, indentation, width);
- }
- }
-
- /**
- * Returns the line delimiter used in this comment region.
- *
- * @return the line delimiter for this comment region
- */
- protected final String getDelimiter() {
- return this.fDelimiter;
- }
-
- /**
- * Returns the line delimiter used in this comment line break.
- *
- * @param predecessor the predecessor comment line after the line break
- * @param successor the successor comment line before the line break
- * @param previous the comment range after the line break
- * @param next the comment range before the line break
- * @param indentation indentation of the formatted line break
- * @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 this.fDelimiter + indentation + successor.getContentPrefix();
- }
-
- /**
- * Returns the range delimiter for this comment range break.
- *
- * @param previous the previous comment range to the right of the range
- * delimiter
- * @param next the next comment range to the left of the range delimiter
- * @return the delimiter for this comment range break
- */
- protected String getDelimiter(final CommentRange previous, final CommentRange next) {
- return COMMENT_RANGE_DELIMITER;
- }
-
- /**
- * Returns the document of this comment region.
- *
- * @return the document of this region
- */
- protected final IDocument getDocument() {
- return this.fDocument;
- }
-
- /**
- * Returns the comment ranges in this comment region
- *
- * @return the comment ranges in this region
- */
- protected final LinkedList getRanges() {
- return this.fRanges;
- }
-
- /**
- * Returns the number of comment lines in this comment region.
- *
- * @return the number of lines in this comment region
- */
- protected final int getSize() {
- return this.fLines.size();
- }
-
- /**
- * Returns the text of this comment region in the indicated range.
- *
- * @param position the offset of the comment range to retrieve in
- * comment region coordinates
- * @param count the length of the comment range to retrieve
- * @return the content of this comment region in the indicated range
- */
- protected final String getText(final int position, final int count) {
-
- String content= ""; //$NON-NLS-1$
- try {
- content= this.fDocument.get(getOffset() + position, count);
- } catch (BadLocationException exception) {
- // Should not happen
- }
- return content;
- }
-
- /**
- * Does the border border
exist?
- *
- * @param border the type of the border, must be a border attribute of
- * CommentRegion
- * @return true
iff this border exists,
- * false
otherwise
- */
- protected final boolean hasBorder(final int border) {
- return (this.fBorders & border) == border;
- }
-
- /**
- * Does the comment range consist of letters and digits only?
- *
- * @param range the comment range to text
- * @return true
iff the comment range consists of letters
- * and digits only, false
otherwise
- */
- protected final boolean isAlphaNumeric(final CommentRange range) {
-
- final String token= getText(range.getOffset(), range.getLength());
-
- for (int index= 0; index < token.length(); index++) {
- if (!ScannerHelper.isLetterOrDigit(token.charAt(index)))
- return false;
- }
- return true;
- }
-
- /**
- * Does the comment range contain no letters and digits?
- *
- * @param range the comment range to text
- * @return true
iff the comment range contains no letters
- * and digits, false
otherwise
- */
- protected final boolean isNonAlphaNumeric(final CommentRange range) {
-
- final String token= getText(range.getOffset(), range.getLength());
-
- for (int index= 0; index < token.length(); index++) {
- if (ScannerHelper.isLetterOrDigit(token.charAt(index)))
- return false;
- }
- return true;
- }
-
- /**
- * Should blank lines be cleared during formatting?
- *
- * @return true
iff blank lines should be cleared,
- * false
otherwise
- */
- protected final boolean isClearLines() {
- return this.fClear;
- }
-
- /**
- * Is this comment region a single line region?
- *
- * @return true
iff this region is single line,
- * false
otherwise
- */
- protected final boolean isSingleLine() {
- return this.fSingleLine;
- }
-
- /**
- * Logs a text edit operation occurred during the formatting process
- *
- * @param change the changed text
- * @param position offset measured in comment region coordinates where
- * to apply the changed text
- * @param count length of the range where to apply the changed text
- */
- protected final void logEdit(final String change, final int position, final int count) {
- try {
- final int base= getOffset() + position;
- final String content= this.fDocument.get(base, count);
-
- if (!change.equals(content)) {
- if (count > 0) {
- this.scribe.addReplaceEdit(base, base + count - 1, change);
- } else {
- this.scribe.addInsertEdit(base, change);
- }
- }
- } catch (BadLocationException exception) {
- // Should not happen
- CommentFormatterUtil.log(exception);
- } catch (MalformedTreeException exception) {
- // Do nothing
- CommentFormatterUtil.log(exception);
- }
- }
-
- /**
- * Marks the comment ranges in this comment region.
- */
- protected void markRegion() {
- // Do nothing
- }
-
- /**
- * Set the border type border
to true.
- *
- * @param border the type of the border. Must be a border attribute of
- * CommentRegion
- */
- protected final void setBorder(final int border) {
- this.fBorders |= border;
- }
-
- /**
- * Returns the indentation of the given indentation level.
- *
- * @param indentationLevel the indentation level
- * @return the indentation of the given indentation level
- * @since 3.1
- */
- private String computeIndentation(int indentationLevel) {
- if (DefaultCodeFormatterOptions.TAB == this.preferences.tab_char)
- return replicate("\t", indentationLevel); //$NON-NLS-1$
-
- if (DefaultCodeFormatterOptions.SPACE == this.preferences.tab_char)
- return replicate(" ", indentationLevel * this.preferences.tab_size); //$NON-NLS-1$
-
- if (DefaultCodeFormatterOptions.MIXED == this.preferences.tab_char) {
- int tabSize= this.preferences.tab_size;
- int indentSize= this.preferences.indentation_size;
- int spaceEquivalents= indentationLevel * indentSize;
- return replicate("\t", spaceEquivalents / tabSize) + replicate(" ", spaceEquivalents % tabSize); //$NON-NLS-1$ //$NON-NLS-2$
- }
-
- Assert.isTrue(false);
- return null;
- }
-
- /**
- * Returns the given string n-times replicated.
- *
- * @param string the string
- * @param n n
- * @return the given string n-times replicated
- * @since 3.1
- */
- private String replicate(String string, int n) {
- StringBuffer buffer= new StringBuffer(n*string.length());
- for (int i= 0; i < n; i++)
- buffer.append(string);
- return buffer.toString();
- }
-
- /**
- * Computes the equivalent indentation for a string
- *
- * @param reference the string to compute the indentation for
- * @return the indentation string
- */
- protected final String stringToIndent(final String reference) {
- return replicate(" ", stringToLength(reference)); //$NON-NLS-1$
- }
-
- /**
- * Returns the length of the string in expanded characters.
- *
- * @param reference the string to get the length for
- * @return the length of the string in expanded characters
- */
- protected final int stringToLength(final String reference) {
- return expandTabs(reference).length();
- }
-
- /**
- * Expands the given string's tabs according to the given tab size.
- *
- * @param string the string
- * @return the expanded string
- * @since 3.1
- */
- private String expandTabs(String string) {
- StringBuffer expanded= new StringBuffer();
- for (int i= 0, n= string.length(), chars= 0; i < n; i++) {
- char ch= string.charAt(i);
- if (ch == '\t') {
- for (; chars < this.fTabSize; chars++)
- expanded.append(' ');
- chars= 0;
- } else {
- expanded.append(ch);
- chars++;
- if (chars >= this.fTabSize)
- chars= 0;
- }
-
- }
- return expanded.toString();
- }
-
- /**
- * Tokenizes the comment region.
- */
- protected void tokenizeRegion() {
-
- int index= 0;
- CommentLine line= null;
-
- for (final Iterator iterator= this.fLines.iterator(); iterator.hasNext(); index++) {
-
- line= (CommentLine)iterator.next();
-
- line.scanLine(index);
- line.tokenizeLine(index);
- }
- }
-
- /**
- * Wraps the comment ranges in this comment region into comment lines.
- *
- * @param width the maximal width of text in this comment region
- * measured in average character widths
- */
- protected void wrapRegion(final int width) {
-
- this.fLines.clear();
-
- int index= 0;
- boolean adapted= false;
-
- CommentLine successor= null;
- CommentLine predecessor= null;
-
- CommentRange previous= null;
- CommentRange next= null;
-
- while (!this.fRanges.isEmpty()) {
-
- index= 0;
- adapted= false;
-
- predecessor= successor;
- successor= createLine();
- this.fLines.add(successor);
-
- while (!this.fRanges.isEmpty()) {
- next= (CommentRange)this.fRanges.getFirst();
-
- if (canAppend(successor, previous, next, index, width)) {
-
- if (!adapted && predecessor != null) {
-
- successor.adapt(predecessor);
- adapted= true;
- }
-
- this.fRanges.removeFirst();
- successor.append(next);
-
- index += (next.getLength() + 1);
- previous= next;
- } else
- break;
- }
- }
- }
-
- /**
- * Creates a new line for this region.
- *
- * @return a new line for this region
- * @since 3.1
- */
- protected CommentLine createLine() {
- return new SingleCommentLine(this);
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/IBorderAttributes.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/IBorderAttributes.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/IBorderAttributes.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/IBorderAttributes.java 7 Mar 2009 01:08:08 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,26 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-/**
- * Comment region border attributes.
- *
- * @since 3.0
- */
-public interface IBorderAttributes {
-
- /** Region has lower border attribute */
- public static final int BORDER_LOWER= 1 << 0;
-
- /** Region has upper border attribute */
- public static final int BORDER_UPPER= 1 << 1;
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/ICommentAttributes.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/ICommentAttributes.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/ICommentAttributes.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/ICommentAttributes.java 7 Mar 2009 01:08:08 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,65 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-/**
- * General comment range attributes.
- *
- * @since 3.0
- */
-public interface ICommentAttributes {
-
- /** Range has blank line attribute */
- public static final int COMMENT_BLANKLINE= 1 << 1;
-
- /** Range has line break attribute */
- public static final int COMMENT_BREAK= 1 << 2;
-
- /** Range has close tag attribute */
- public static final int COMMENT_CLOSE= 1 << 3;
-
- /** Range has source code attribute */
- public static final int COMMENT_CODE= 1 << 4;
-
- /** Range has html tag attribute */
- public static final int COMMENT_HTML= 1 << 5;
-
- /** Range has the immutable region attribute */
- public static final int COMMENT_IMMUTABLE= 1 << 6;
-
- /** Range has new line attribute */
- public static final int COMMENT_NEWLINE= 1 << 7;
-
- /** Range has open tag attribute */
- public static final int COMMENT_OPEN= 1 << 8;
-
- /** Range has paragraph attribute */
- public static final int COMMENT_PARAGRAPH= 1 << 9;
-
- /** Range has parameter tag attribute */
- public static final int COMMENT_PARAMETER= 1 << 10;
-
- /** Range has root tag attribute */
- public static final int COMMENT_ROOT= 1 << 11;
-
- /** Range has paragraph separator attribute */
- public static final int COMMENT_SEPARATOR= 1 << 12;
-
- /** Range is the first token on the line in the original source */
- public static final int COMMENT_FIRST_TOKEN= 1 << 13;
-
- /**
- * Range was preceded by whitespace / line delimiters
- * @since 3.1
- */
- public static final int COMMENT_STARTS_WITH_RANGE_DELIMITER= 1 << 14;
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/IHtmlTagDelimiters.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/IHtmlTagDelimiters.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/IHtmlTagDelimiters.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/IHtmlTagDelimiters.java 7 Mar 2009 01:08:08 -0000 1.8
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,29 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-/**
- * Html tag constants.
- *
- * @since 3.0
- */
-public interface IHtmlTagDelimiters {
-
- /** Html tag close prefix */
- public static final char[] HTML_CLOSE_PREFIX= "".toCharArray(); //$NON-NLS-1$
-
- /** Html tag postfix */
- public static final char HTML_TAG_POSTFIX= '>';
-
- /** Html tag prefix */
- public static final char HTML_TAG_PREFIX= '<';
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocLine.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocLine.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocLine.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocLine.java 7 Mar 2009 01:08:08 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,52 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-/**
- * Javadoc comment line in a comment region.
- *
- * @since 3.0
- */
-public class JavaDocLine extends MultiCommentLine {
-
- /** Line prefix of javadoc start lines */
- public static final String JAVADOC_START_PREFIX= "/**"; //$NON-NLS-1$
-
- /**
- * Creates a new javadoc line.
- *
- * @param region comment region to create the line for
- */
- protected JavaDocLine(final CommentRegion region) {
- super(region);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#formatUpperBorder(org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String, int)
- */
- protected void formatUpperBorder(final CommentRange range, final String indentation, final int length) {
-
- final CommentRegion parent= getParent();
-
- if (parent.isSingleLine() && parent.getSize() == 1) {
- parent.logEdit(getStartingPrefix() + CommentRegion.COMMENT_RANGE_DELIMITER, 0, range.getOffset());
- } else
- super.formatUpperBorder(range, indentation, length);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getStartingPrefix()
- */
- protected String getStartingPrefix() {
- return JAVADOC_START_PREFIX;
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocRegion.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocRegion.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocRegion.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/JavaDocRegion.java 27 Jun 2008 16:03:57 -0000 1.11
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,365 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2008 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.eclipse.text.edits.TextEdit;
-
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.DefaultLineTracker;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.ILineTracker;
-import org.eclipse.jface.text.Position;
-import org.eclipse.jface.text.TextUtilities;
-
-import org.eclipse.jdt.core.formatter.CodeFormatter;
-import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
-import org.eclipse.jdt.internal.formatter.CodeFormatterVisitor;
-
-
-/**
- * Javadoc region in a source code document.
- *
- * @since 3.0
- */
-public class JavaDocRegion extends MultiCommentRegion {
-
- /** The positions of code ranges */
- private final ArrayList fCodePositions= new ArrayList();
-
- /** Should HTML tags be formatted? */
- private final boolean fFormatHtml;
-
- /** Should source code regions be formatted? */
- private final boolean fFormatSource;
-
- /**
- * Creates a new Javadoc region.
- *
- * @param document the document which contains the comment region
- * @param position the position of this comment region in the document
- * @param formatter the given formatter
- */
- public JavaDocRegion(final IDocument document, final Position position, final CodeFormatterVisitor formatter) {
- super(document, position, formatter);
-
- this.fFormatSource = this.preferences.comment_format_source;
- this.fFormatHtml = this.preferences.comment_format_html;
- this.fClear = this.preferences.comment_clear_blank_lines_in_javadoc_comment;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#canFormat(org.eclipse.jdt.internal.corext.text.comment.CommentRange, org.eclipse.jdt.internal.corext.text.comment.CommentRange)
- */
- protected boolean canFormat(final CommentRange previous, final CommentRange next) {
-
- if (previous != null) {
-
- final boolean isCurrentCode= next.hasAttribute(COMMENT_CODE);
- final boolean isLastCode= previous.hasAttribute(COMMENT_CODE);
-
- final int base= getOffset();
-
- if (!isLastCode && isCurrentCode)
- this.fCodePositions.add(new Position(base + previous.getOffset()));
- else if (isLastCode && !isCurrentCode)
- this.fCodePositions.add(new Position(base + next.getOffset() + next.getLength()));
-
- if (previous.hasAttribute(COMMENT_IMMUTABLE) && next.hasAttribute(COMMENT_IMMUTABLE))
- return false;
-
- return true;
- }
- return false;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#formatRegion(java.lang.String, int)
- */
- protected final void formatRegion(final String indentation, final int width) {
-
- super.formatRegion(indentation, width);
-
- if (this.fFormatSource) {
-
- try {
-
- if (this.fCodePositions.size() > 0) {
-
- int begin= 0;
- int end= 0;
-
- Position position= null;
-
- final IDocument document= getDocument();
-
- for (int index= this.fCodePositions.size() - 1; index >= 0;) {
-
- position= (Position)this.fCodePositions.get(index--);
- begin= position.getOffset();
-
- if (index >= 0) {
- position= (Position)this.fCodePositions.get(index--);
- end= position.getOffset();
- } else {
- /*
- * Handle missing closing tag
- * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=57011
- */
- position= null;
- end= getOffset() + getLength() - MultiCommentLine.MULTI_COMMENT_END_PREFIX.trim().length();
- while (end > begin && ScannerHelper.isWhitespace(document.getChar(end - 1)))
- end--;
- }
-
- String snippet= document.get(begin, end - begin);
- snippet= preprocessCodeSnippet(snippet);
- snippet= formatCodeSnippet(snippet);
- snippet= postprocessCodeSnippet(snippet, indentation);
-
- logEdit(snippet, begin - getOffset(), end - begin);
- }
- }
- } catch (BadLocationException e) {
- // Can not happen
- CommentFormatterUtil.log(e);
- }
- }
- }
-
- /**
- * Preprocess a given code snippet.
- *
- * @param snippet the code snippet
- * @return the preprocessed code snippet
- */
- private String preprocessCodeSnippet(String snippet) {
- // strip content prefix
- StringBuffer buffer= new StringBuffer();
- ILineTracker tracker= new DefaultLineTracker();
- String contentPrefix= MultiCommentLine.MULTI_COMMENT_CONTENT_PREFIX.trim();
-
- buffer.setLength(0);
- buffer.append(snippet);
- tracker.set(snippet);
- for (int line= tracker.getNumberOfLines() - 1; line > 0; line--) {
- int lineOffset;
- try {
- lineOffset= tracker.getLineOffset(line);
- } catch (BadLocationException e) {
- // Can not happen
- CommentFormatterUtil.log(e);
- return snippet;
- }
- int prefixOffset= buffer.indexOf(contentPrefix, lineOffset);
- if (prefixOffset >= 0 && buffer.substring(lineOffset, prefixOffset).trim().length() == 0)
- buffer.delete(lineOffset, prefixOffset + contentPrefix.length() + 1);
- }
-
- return convertHtml2Java(buffer.toString());
- }
-
- /**
- * Format the given code snippet
- *
- * @param snippet the code snippet
- * @return the formatted code snippet
- */
- private String formatCodeSnippet(String snippet) {
- String lineDelimiter= TextUtilities.getDefaultLineDelimiter(getDocument());
- TextEdit edit= CommentFormatterUtil.format2(CodeFormatter.K_UNKNOWN, snippet, 0, lineDelimiter, this.preferences.getMap());
- if (edit != null)
- snippet= CommentFormatterUtil.evaluateFormatterEdit(snippet, edit, null);
- return snippet;
- }
-
- /**
- * Postprocesses the given code snippet with the given indentation.
- *
- * @param snippet the code snippet
- * @param indentation the indentation
- * @return the postprocessed code snippet
- */
- private String postprocessCodeSnippet(String snippet, String indentation) {
- // patch content prefix
- StringBuffer buffer= new StringBuffer();
- ILineTracker tracker= new DefaultLineTracker();
- String patch= indentation + MultiCommentLine.MULTI_COMMENT_CONTENT_PREFIX;
-
- // remove trailing spaces
- int i= snippet.length();
- while (i > 0 && ' ' == snippet.charAt(i-1))
- i--;
- snippet= snippet.substring(0, i);
-
- buffer.setLength(0);
- String lineDelimiter= getDelimiter();
- if (lineDelimiter != null && snippet.indexOf(lineDelimiter) != 0)
- buffer.append(lineDelimiter);
- buffer.append(convertJava2Html(snippet));
- if (lineDelimiter != null && snippet.lastIndexOf(lineDelimiter) != snippet.length() - lineDelimiter.length())
- buffer.append(lineDelimiter);
- tracker.set(buffer.toString());
-
- for (int line= tracker.getNumberOfLines() - 1; line > 0; line--)
- try {
- buffer.insert(tracker.getLineOffset(line), patch);
- } catch (BadLocationException e) {
- // Can not happen
- CommentFormatterUtil.log(e);
- return snippet;
- }
-
- return buffer.toString();
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.MultiCommentRegion#markHtmlRanges()
- */
- protected final void markHtmlRanges() {
-
- markTagRanges(JAVADOC_IMMUTABLE_TAGS, COMMENT_IMMUTABLE, true);
-
- if (this.fFormatSource)
- markTagRanges(JAVADOC_CODE_TAGS, COMMENT_CODE, false);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.MultiCommentRegion#markHtmlTag(org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String)
- */
- protected final void markHtmlTag(final CommentRange range, final char[] token) {
-
- if (range.hasAttribute(COMMENT_HTML)) {
-
- range.markHtmlTag(JAVADOC_IMMUTABLE_TAGS, token, COMMENT_IMMUTABLE, true, true);
- if (this.fFormatHtml) {
-
- range.markHtmlTag(JAVADOC_SEPARATOR_TAGS, token, COMMENT_SEPARATOR, true, true);
- range.markHtmlTag(JAVADOC_BREAK_TAGS, token, COMMENT_BREAK, false, true);
- range.markHtmlTag(JAVADOC_SINGLE_BREAK_TAG, token, COMMENT_BREAK, true, false);
- range.markHtmlTag(JAVADOC_NEWLINE_TAGS, token, COMMENT_NEWLINE, true, false);
-
- } else
- range.markHtmlTag(JAVADOC_CODE_TAGS, token, COMMENT_SEPARATOR, true, true);
- }
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.MultiCommentRegion#markJavadocTag(org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String)
- */
- protected final void markJavadocTag(final CommentRange range, final char[] token) {
-
- range.markPrefixTag(JAVADOC_PARAM_TAGS, COMMENT_TAG_PREFIX, token, COMMENT_PARAMETER);
-
- if (token[0] == JAVADOC_TAG_PREFIX && !range.hasAttribute(COMMENT_PARAMETER))
- range.setAttribute(COMMENT_ROOT);
- }
-
- /**
- * Marks the comment region with the HTML range tag.
- *
- * @param tags the HTML tag which confines the HTML range
- * @param attribute the attribute to set if the comment range is in the
- * HTML range
- * @param html true
iff the HTML tags in this HTML range
- * should be marked too, false
otherwise
- */
- protected final void markTagRanges(final char[][] tags, final int attribute, final boolean html) {
-
- int level= 0;
- int count= 0;
- char[] token= null;
- CommentRange current= null;
-
- for (int index= 0; index < tags.length; index++) {
-
- level= 0;
- for (final Iterator iterator= getRanges().iterator(); iterator.hasNext();) {
-
- current= (CommentRange)iterator.next();
- count= current.getLength();
-
- if (count > 0 || level > 0) { // PR44035: when inside a tag, mark blank lines as well to get proper snippet formatting
-
- token= getText(current.getOffset(), current.getLength()).toCharArray();
- level= current.markTagRange(token, tags[index], level, attribute, html);
- }
- }
- }
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#canAppend(org.eclipse.jdt.internal.corext.text.comment.CommentLine, org.eclipse.jdt.internal.corext.text.comment.CommentRange, org.eclipse.jdt.internal.corext.text.comment.CommentRange, int, int)
- */
- protected boolean canAppend(CommentLine line, CommentRange previous, CommentRange next, int index, int count) {
- // don't append code sections
- if (next.hasAttribute(COMMENT_CODE | COMMENT_FIRST_TOKEN) && line.getSize() != 0)
- return false;
- return super.canAppend(line, previous, next, index, count);
- }
-
- /**
- * Converts formatted
into valid html code suitable to be
- * put inside <pre></pre> tags by replacing any html symbols
- * by the relevant entities.
- *
- * @param formatted the formatted java code
- * @return html version of the formatted code
- */
- private String convertJava2Html(String formatted) {
- Java2HTMLEntityReader reader= new Java2HTMLEntityReader(new StringReader(formatted));
- char[] buf= new char[256];
- StringBuffer buffer= new StringBuffer();
- int l;
- try {
- do {
- l= reader.read(buf);
- if (l != -1)
- buffer.append(buf, 0, l);
- } while (l > 0);
- return buffer.toString();
- } catch (IOException e) {
- return formatted;
- }
- }
-
- /**
- * Converts html
into java code suitable for formatting
- * by replacing any html entities by their plain text representation.
- *
- * @param html html code, may contain html entities
- * @return plain textified version of html
- */
- private String convertHtml2Java(String html) {
- HTMLEntity2JavaReader reader= new HTMLEntity2JavaReader(new StringReader(html));
- char[] buf= new char[html.length()]; // html2text never gets longer, only shorter!
-
- try {
- int read= reader.read(buf);
- return new String(buf, 0, read);
- } catch (IOException e) {
- return html;
- }
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#createLine()
- * @since 3.1
- */
- protected CommentLine createLine() {
- return new JavaDocLine(this);
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentLine.java 7 Mar 2009 01:08:08 -0000 1.8
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,409 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.util.HashSet;
-import java.util.Set;
-
-import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.Region;
-
-/**
- * Multi-line comment line in a comment region.
- *
- * @since 3.0
- */
-public class MultiCommentLine extends CommentLine implements ICommentAttributes, IHtmlTagDelimiters, IJavaDocTagConstants {
-
- /** 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 end lines */
- public static final String MULTI_COMMENT_END_PREFIX= " */"; //$NON-NLS-1$
-
- /** Line prefix of multi-line comment content lines */
- public static final String MULTI_COMMENT_START_PREFIX= "/* "; //$NON-NLS-1$
-
- /** The indentation reference of this line */
- private String fReferenceIndentation= ""; //$NON-NLS-1$
-
- /** The javadoc tag lookup. */
- private static final Set fgTagLookup;
-
- static {
- fgTagLookup= new HashSet();
- for (int i= 0; i < JAVADOC_BREAK_TAGS.length; i++) {
- fgTagLookup.add(new String(JAVADOC_BREAK_TAGS[i]));
- }
- for (int i= 0; i < JAVADOC_SINGLE_BREAK_TAG.length; i++) {
- fgTagLookup.add(new String(JAVADOC_SINGLE_BREAK_TAG[i]));
- }
- for (int i= 0; i < JAVADOC_CODE_TAGS.length; i++) {
- fgTagLookup.add(new String(JAVADOC_CODE_TAGS[i]));
- }
- for (int i= 0; i < JAVADOC_IMMUTABLE_TAGS.length; i++) {
- fgTagLookup.add(new String(JAVADOC_IMMUTABLE_TAGS[i]));
- }
- for (int i= 0; i < JAVADOC_NEWLINE_TAGS.length; i++) {
- fgTagLookup.add(new String(JAVADOC_NEWLINE_TAGS[i]));
- }
- for (int i= 0; i < JAVADOC_SEPARATOR_TAGS.length; i++) {
- fgTagLookup.add(new String(JAVADOC_SEPARATOR_TAGS[i]));
- }
- }
-
- /**
- * Creates a new multi-line comment line.
- *
- * @param region comment region to create the line for
- */
- protected MultiCommentLine(final CommentRegion region) {
- super(region);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#adapt(org.eclipse.jdt.internal.corext.text.comment.CommentLine)
- */
- protected void adapt(final CommentLine previous) {
-
- if (!hasAttribute(COMMENT_ROOT) && !hasAttribute(COMMENT_PARAMETER) && !previous.hasAttribute(COMMENT_BLANKLINE))
- this.fReferenceIndentation= previous.getIndentationReference();
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#append(org.eclipse.jdt.internal.corext.text.comment.CommentRange)
- */
- protected void append(final CommentRange range) {
-
- final MultiCommentRegion parent= (MultiCommentRegion)getParent();
-
- if (range.hasAttribute(COMMENT_PARAMETER))
- setAttribute(COMMENT_PARAMETER);
- else if (range.hasAttribute(COMMENT_ROOT))
- setAttribute(COMMENT_ROOT);
- else if (range.hasAttribute(COMMENT_BLANKLINE))
- setAttribute(COMMENT_BLANKLINE);
-
- final int ranges= getSize();
- if (ranges == 1) {
-
- if (parent.isIndentRoots()) {
-
- final CommentRange first= getFirst();
- final String common= parent.getText(first.getOffset(), first.getLength()) + CommentRegion.COMMENT_RANGE_DELIMITER;
-
- if (hasAttribute(COMMENT_ROOT))
- this.fReferenceIndentation= common;
- else if (hasAttribute(COMMENT_PARAMETER)) {
- if (parent.isIndentDescriptions())
- this.fReferenceIndentation= "\t" + common; //$NON-NLS-1$
- else
- this.fReferenceIndentation= common;
- }
- }
- }
- super.append(range);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getContentLinePrefix()
- */
- protected String getContentPrefix() {
- return MULTI_COMMENT_CONTENT_PREFIX;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getEndLinePrefix()
- */
- protected String getEndingPrefix() {
- return MULTI_COMMENT_END_PREFIX;
- }
-
- /**
- * Returns the reference indentation to use for this line.
- *
- * @return the reference indentation for this line
- */
- protected final String getIndentationReference() {
- return this.fReferenceIndentation;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getStartLinePrefix()
- */
- protected String getStartingPrefix() {
- return MULTI_COMMENT_START_PREFIX;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#scanLine(int)
- */
- protected void scanLine(final int line) {
-
- final CommentRegion parent= getParent();
- final String start= getStartingPrefix().trim();
- final String end= getEndingPrefix().trim();
- final String content= getContentPrefix().trim();
-
- final int lines= parent.getSize();
- final CommentRange range= getFirst();
-
- int offset= 0;
- int postfix= 0;
-
- String text= parent.getText(range.getOffset(), range.getLength());
- if (line == 0) {
-
- offset= text.indexOf(start);
- if (offset >= 0 && text.substring(0, offset).trim().length() != 0)
- offset= -1;
-
- if (offset >= 0) {
-
- offset += start.length();
- range.trimBegin(offset);
-
- postfix= text.lastIndexOf(end);
- if (postfix >= 0 && text.substring(postfix + end.length()).trim().length() != 0)
- postfix= -1;
-
- if (postfix >= offset)
- // comment ends on same line
- range.setLength(postfix - offset);
- else {
- postfix= text.lastIndexOf(content);
- if (postfix >= 0 && text.substring(postfix + content.length()).trim().length() != 0)
- postfix= -1;
-
- if (postfix >= offset) {
-
- range.setLength(postfix - offset);
- parent.setBorder(BORDER_UPPER);
-
- if (postfix > offset) {
-
- text= parent.getText(range.getOffset(), range.getLength());
- final IRegion region= trimLine(text, content);
-
- range.move(region.getOffset());
- range.setLength(region.getLength());
- }
- }
- }
- }
- } else if (line == lines - 1) {
-
- offset= text.indexOf(content);
- if (offset >= 0 && text.substring(0, offset).trim().length() != 0)
- offset= -1;
- postfix= text.lastIndexOf(end);
- if (postfix >= 0 && text.substring(postfix + end.length()).trim().length() != 0)
- postfix= -1;
-
- if (offset >= 0 && offset == postfix)
- // no content on line, only the comment postfix
- range.setLength(0);
- else {
- if (offset >= 0)
- // omit the content prefix
- range.trimBegin(offset + content.length());
-
- if (postfix >= 0)
- // omit the comment postfix
- range.trimEnd(-end.length());
-
- text= parent.getText(range.getOffset(), range.getLength());
- final IRegion region= trimLine(text, content);
- if (region.getOffset() != 0 || region.getLength() != text.length()) {
-
- range.move(region.getOffset());
- range.setLength(region.getLength());
-
- parent.setBorder(BORDER_UPPER);
- parent.setBorder(BORDER_LOWER);
- }
- }
- } else {
-
- offset= text.indexOf(content);
- if (offset >= 0 && text.substring(0, offset).trim().length() != 0)
- offset= -1;
-
- if (offset >= 0) {
-
- offset += content.length();
- range.trimBegin(offset);
- }
- }
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#tokenizeLine(int)
- */
- protected void tokenizeLine(int line) {
-
- int offset= 0;
- int index= offset;
-
- final CommentRegion parent= getParent();
- final CommentRange range= getFirst();
- final int begin= range.getOffset();
-
- final String content= parent.getText(begin, range.getLength());
- final int length= content.length();
-
- while (offset < length && ScannerHelper.isWhitespace(content.charAt(offset)))
- offset++;
-
- CommentRange result= null;
- if (offset >= length && !parent.isClearLines() && (line > 0 && line < parent.getSize() - 1)) {
-
- result= new CommentRange(begin, 0);
- result.setAttribute(COMMENT_BLANKLINE);
- result.setAttribute(COMMENT_FIRST_TOKEN);
-
- parent.append(result);
- }
-
- int attribute= COMMENT_FIRST_TOKEN | COMMENT_STARTS_WITH_RANGE_DELIMITER;
- while (offset < length) {
-
- while (offset < length && ScannerHelper.isWhitespace(content.charAt(offset))) {
- offset++;
- attribute |= COMMENT_STARTS_WITH_RANGE_DELIMITER;
- }
-
- index= offset;
-
- if (index < length) {
-
- if (content.charAt(index) == HTML_TAG_PREFIX) {
-
- // in order to avoid recognizing any < in a comment, even those which are part of e.g.
- // java source code, we validate the tag content to be one of the recognized
- // tags (structural, breaks, pre, code).
- int tag= ++index;
- while (index < length && content.charAt(index) != HTML_TAG_POSTFIX && content.charAt(index) != HTML_TAG_PREFIX)
- index++;
-
- if (index < length && content.charAt(index) == HTML_TAG_POSTFIX && isValidTag(content.substring(tag, index))) {
- index++;
- attribute |= COMMENT_HTML; // only set html attribute if postfix found
- } else {
- // no tag - do the usual thing from the original offset
- index= tag;
- while (index < length
- && !ScannerHelper.isWhitespace(content.charAt(index))
- && content.charAt(index) != HTML_TAG_PREFIX
- && !content.startsWith(LINK_TAG_PREFIX_STRING, index))
- index++;
- }
-
-
- } else if (content.startsWith(LINK_TAG_PREFIX_STRING, index)) {
-
- while (index < length && content.charAt(index) != LINK_TAG_POSTFIX)
- index++;
-
- if (index < length && content.charAt(index) == LINK_TAG_POSTFIX)
- index++;
-
- attribute |= COMMENT_OPEN | COMMENT_CLOSE;
-
- } else {
-
- while (index < length
- && !ScannerHelper.isWhitespace(content.charAt(index))
- && content.charAt(index) != HTML_TAG_PREFIX
- && !content.startsWith(LINK_TAG_PREFIX_STRING, index))
- index++;
- }
- }
-
- if (index - offset > 0) {
-
- result= new CommentRange(begin + offset, index - offset);
- result.setAttribute(attribute);
-
- parent.append(result);
- offset= index;
- }
-
- attribute= 0;
- }
- }
-
- /**
- * Checks whether tag
is a valid tag content (text inside
- * the angular brackets <, >).
- *
- * The algorithm is to see if the tag trimmed of whitespace and an
- * optional slash starts with one of our recognized tags.
- *
- * @param tag the tag to check
- * @return true
if tag
is a valid tag
- * content
- */
- private boolean isValidTag(String tag) {
- // strip the slash
- if (tag.startsWith("/")) //$NON-NLS-1$
- tag= tag.substring(1, tag.length());
-
- // strip ws
- tag= tag.trim();
-
- // extract first token
- int i= 0;
- while (i < tag.length() && !ScannerHelper.isWhitespace(tag.charAt(i)))
- i++;
- tag= tag.substring(0, i);
-
- // see if it's a tag
- return isTagName(tag.toLowerCase());
- }
-
- /**
- * Checks whether tag
is one of the configured tags.
- *
- * @param tag the tag to check
- * @return true
if tag
is a configured tag
- * name
- */
- private boolean isTagName(String tag) {
- return fgTagLookup.contains(tag);
- }
-
- /**
- * Removes all leading and trailing occurrences from line
.
- *
- * @param line the string to remove the occurrences of
- * trimmable
- * @param trimmable the string to remove from line
- * @return the region of the trimmed substring within line
- */
- protected final IRegion trimLine(final String line, final String trimmable) {
-
- final int trim= trimmable.length();
-
- int offset= 0;
- int length= line.length() - trim;
-
- while (line.startsWith(trimmable, offset))
- offset += trim;
-
- while (line.startsWith(trimmable, length))
- length -= trim;
-
- return new Region(offset, length + trim);
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/MultiCommentRegion.java 7 Mar 2009 01:08:08 -0000 1.10
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,245 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-import java.util.Iterator;
-import java.util.ListIterator;
-
-import org.eclipse.jdt.internal.formatter.CodeFormatterVisitor;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.Position;
-
-/**
- * Multi-comment region in a source code document.
- *
- * @since 3.0
- */
-public class MultiCommentRegion extends CommentRegion implements IJavaDocTagConstants {
-
- /** Should root tag parameter descriptions be indented after the tag? */
- private final boolean fIndentDescriptions;
-
- /** Should root tag parameter descriptions be indented? */
- private final boolean fIndentRoots;
-
- /** Should description of parameters go to the next line? */
- private final boolean fParameterNewLine;
-
- /** Should root tags be separated from description? */
- private boolean fSeparateRoots;
-
- /**
- * Creates a new multi-comment region.
- *
- * @param document the document which contains the comment region
- * @param position the position of this comment region in the document
- * @param formatter the given formatter
- */
- public MultiCommentRegion(final IDocument document, final Position position, final CodeFormatterVisitor formatter) {
- super(document, position, formatter);
-
- this.fIndentRoots= this.preferences.comment_indent_root_tags;
- this.fIndentDescriptions= this.preferences.comment_indent_parameter_description;
- this.fSeparateRoots= this.preferences.comment_insert_empty_line_before_root_tags;
- this.fParameterNewLine= this.preferences.comment_insert_new_line_for_parameter;
- this.fClear = this.preferences.comment_clear_blank_lines_in_block_comment;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#canAppend(org.eclipse.jdt.internal.corext.text.comment.CommentLine, org.eclipse.jdt.internal.corext.text.comment.CommentRange, org.eclipse.jdt.internal.corext.text.comment.CommentRange, int, int)
- */
- protected boolean canAppend(final CommentLine line, final CommentRange previous, final CommentRange next, final int index, int count) {
-
- final boolean blank= next.hasAttribute(COMMENT_BLANKLINE);
-
- // Avoid wrapping punctuation
- if (next.getLength() <= 2 && !blank && isNonAlphaNumeric(next))
- return true;
-
- if (this.fParameterNewLine && line.hasAttribute(COMMENT_PARAMETER) && line.getSize() > 1)
- return false;
-
- if (previous != null) {
-
- if (index != 0 && (blank || previous.hasAttribute(COMMENT_BLANKLINE) || next.hasAttribute(COMMENT_PARAMETER) || next.hasAttribute(COMMENT_ROOT) || next.hasAttribute(COMMENT_SEPARATOR) || next.hasAttribute(COMMENT_NEWLINE) || previous.hasAttribute(COMMENT_BREAK) || previous.hasAttribute(COMMENT_SEPARATOR)))
- return false;
-
- if (previous.hasAttribute(COMMENT_ROOT))
- return true;
-
- if (next.hasAttribute(COMMENT_IMMUTABLE) && previous.hasAttribute(COMMENT_IMMUTABLE))
- return true;
- }
-
- // always append elements that did not have any range separators
- if (!next.hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER)) {
- return true;
- }
-
- if (this.fIndentRoots && !line.hasAttribute(COMMENT_ROOT) && !line.hasAttribute(COMMENT_PARAMETER))
- count -= stringToLength(line.getIndentationReference());
-
- // Avoid appending consecutive immutable ranges, which together exceed the line width
- if (next.hasAttribute(COMMENT_IMMUTABLE) && (previous == null || !previous.hasAttribute(COMMENT_IMMUTABLE))) {
- // Breaking the abstraction by directly accessing the list of ranges for looking ahead
- Iterator iter= getRanges().iterator();
- CommentRange current= null;
- while (iter.hasNext() && current != next)
- current= (CommentRange) iter.next();
-
- if (current != null && iter.hasNext()) {
- try {
- int lineNumber= getDocument().getLineOfOffset(getOffset() + current.getOffset());
- CommentRange last= current;
- while (iter.hasNext()) {
- current= (CommentRange) iter.next();
- if (current.hasAttribute(COMMENT_IMMUTABLE) && getDocument().getLineOfOffset(getOffset() + current.getOffset()) == lineNumber)
- last= current;
- else
- break;
- }
- count -= last.getOffset() + last.getLength() - (next.getOffset() + next.getLength());
- } catch (BadLocationException e) {
- // Should not happen
- }
- }
- }
-
- return super.canAppend(line, previous, next, index, count);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#getDelimiter(org.eclipse.jdt.internal.corext.text.comment.CommentLine, org.eclipse.jdt.internal.corext.text.comment.CommentLine, org.eclipse.jdt.internal.corext.text.comment.CommentRange, org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String)
- */
- protected String getDelimiter(CommentLine predecessor, CommentLine successor, CommentRange previous, CommentRange next, String indentation) {
-
- final String delimiter= super.getDelimiter(predecessor, successor, previous, next, indentation);
-
- if (previous != null) {
-
- // Blank line before
tag - if (previous.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !next.hasAttribute(COMMENT_CODE) && !successor.hasAttribute(COMMENT_BLANKLINE)) - return delimiter + delimiter; - - // Blank line aftertag - else if (next.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) - return delimiter + delimiter; - - // Add blank line before first root/parameter tag, if "Blank line before Javadoc tags" - else if (this.fSeparateRoots && previous.hasAttribute(COMMENT_PARAGRAPH) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) - return delimiter + delimiter; - - else if (this.fIndentRoots && !predecessor.hasAttribute(COMMENT_ROOT) && !predecessor.hasAttribute(COMMENT_PARAMETER) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) - return delimiter + stringToIndent(predecessor.getIndentationReference()); - } - return delimiter; - } - - /* - * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#getDelimiter(org.eclipse.jdt.internal.corext.text.comment.CommentRange, org.eclipse.jdt.internal.corext.text.comment.CommentRange) - */ - protected String getDelimiter(final CommentRange previous, final CommentRange next) { - // simply preserve range (~ word) breaks - if (previous != null && !previous.hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER)) { - return ""; //$NON-NLS-1$ - } else { - return super.getDelimiter(previous, next); - } - } - - /** - * Should root tag parameter descriptions be indented after the tag? - * - * @return
true
iff the descriptions should be indented
- * after, false
otherwise.
- */
- protected final boolean isIndentDescriptions() {
- return this.fIndentDescriptions;
- }
-
- /**
- * Should root tag parameter descriptions be indented?
- *
- * @return true
iff the root tags should be indented,
- * false
otherwise.
- */
- protected final boolean isIndentRoots() {
- return this.fIndentRoots;
- }
-
- /**
- * Marks the comment ranges confined by HTML ranges.
- */
- protected void markHtmlRanges() {
- // Do nothing
- }
-
- /**
- * Marks the comment range with its HTML tag attributes.
- *
- * @param range the comment range to mark
- * @param token token associated with the comment range
- */
- protected void markHtmlTag(final CommentRange range, final char[] token) {
- // Do nothing
- }
-
- /**
- * Marks the comment range with its javadoc tag attributes.
- *
- * @param range the comment range to mark
- * @param token token associated with the comment range
- */
- protected void markJavadocTag(final CommentRange range, final char[] token) {
- range.markPrefixTag(COMMENT_ROOT_TAGS, COMMENT_TAG_PREFIX, token, COMMENT_ROOT);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#markRegion()
- */
- protected void markRegion() {
-
- int count= 0;
- boolean paragraph= false;
-
- char[] token= null;
- CommentRange range= null;
-
- for (final ListIterator iterator= getRanges().listIterator(); iterator.hasNext();) {
-
- range= (CommentRange)iterator.next();
- count= range.getLength();
-
- if (count > 0) {
-
- token= getText(range.getOffset(), count).toLowerCase().toCharArray();
-
- markJavadocTag(range, token);
- if (!paragraph && (range.hasAttribute(COMMENT_ROOT) || range.hasAttribute(COMMENT_PARAMETER))) {
- range.setAttribute(COMMENT_PARAGRAPH);
- paragraph= true;
- }
- markHtmlTag(range, token);
- }
- }
- markHtmlRanges();
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentRegion#createLine()
- * @since 3.1
- */
- protected CommentLine createLine() {
- return new MultiCommentLine(this);
- }
-}
Index: formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java
===================================================================
RCS file: formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java
diff -N formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java
--- formatter/org/eclipse/jdt/internal/formatter/comment/SingleCommentLine.java 7 Mar 2009 01:08:08 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,115 +0,0 @@
-/*******************************************************************************
- * 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.jdt.internal.formatter.comment;
-
-/**
- * Single-line comment line in a comment region.
- *
- * @since 3.0
- */
-public class SingleCommentLine extends CommentLine {
-
- /** Line prefix for single line comments */
- public static final String SINGLE_COMMENT_PREFIX= "// "; //$NON-NLS-1$
-
- /** NLS tag prefix */
- private static final String NLS_TAG_PREFIX= "//$NON-NLS-"; //$NON-NLS-1$
-
- /** Is the comment a NLS locale tag sequence? */
- private boolean fLocaleSequence= false;
-
- /**
- * Creates a new single-line comment line.
- *
- * @param region comment region to create the line for
- */
- protected SingleCommentLine(final CommentRegion region) {
- super(region);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#adapt(org.eclipse.jdt.internal.corext.text.comment.CommentLine)
- */
- protected void adapt(final CommentLine previous) {
- // Do nothing
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#formatLowerBorder(org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String, int)
- */
- protected void formatLowerBorder(final CommentRange range, final String indentation, final int length) {
-
- final int offset= range.getOffset() + range.getLength();
- final CommentRegion parent= getParent();
-
- parent.logEdit(parent.getDelimiter(), offset, parent.getLength() - offset);
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#formatUpperBorder(org.eclipse.jdt.internal.corext.text.comment.CommentRange, java.lang.String, int)
- */
- protected void formatUpperBorder(final CommentRange range, final String indentation, final int length) {
-
- final CommentRegion parent= getParent();
-
- parent.logEdit(getContentPrefix(), 0, range.getOffset());
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getContentPrefix()
- */
- protected String getContentPrefix() {
- return SINGLE_COMMENT_PREFIX;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getEndingPrefix()
- */
- protected String getEndingPrefix() {
- return SINGLE_COMMENT_PREFIX;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#getStartingPrefix()
- */
- protected String getStartingPrefix() {
- return SINGLE_COMMENT_PREFIX;
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#scanLine(int)
- */
- protected void scanLine(final int line) {
-
- final CommentRange range= getFirst();
- final String content= getParent().getText(range.getOffset(), range.getLength());
- final String prefix= getContentPrefix().trim();
-
- final int offset= content.indexOf(prefix);
- if (offset >= 0) {
-
- if (content.startsWith(NLS_TAG_PREFIX))
- this.fLocaleSequence= true;
-
- range.trimBegin(offset + prefix.length());
- }
- }
-
- /*
- * @see org.eclipse.jdt.internal.corext.text.comment.CommentLine#tokenizeLine(int)
- */
- protected void tokenizeLine(final int line) {
-
- if (!this.fLocaleSequence)
- super.tokenizeLine(line);
- }
-}
#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.52
diff -u -r1.52 FormatterCommentsBugsTest.java
--- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 14 Feb 2010 15:57:04 -0000 1.52
+++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 18 Feb 2010 12:14:14 -0000
@@ -2200,8 +2200,6 @@
" return null;\n" +
" }\n" +
"}\n";
- // TODO (frederic) line comment should be formatted when F_INCLUDE_COMMENTS
- // flag will work for all snippet kinds
formatSource(source,
"/**\n" +
" * Need a javadoc comment before to get the exception.\n" +
@@ -2213,7 +2211,7 @@
" * If there is an authority, it is:\n" +
" * \n" +
" * \n" + - " * //class body snippet\n" + + " * // class body snippet\n" + " * public class X {\n" + " * }\n" + " *\n" + @@ -2265,6 +2263,142 @@ } /** + * @bug 236406: [formatter] Formatting qualified invocations can be broken when the Line Wrapping policy forces element to be on a new line + * @test Verify that wrapping policies forcing the first element to be on a new line are working again... + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=236406" + */ +public void testBug236406_CDB1() { + String source = + "/** Javadoc comment */void foo1() {System.out.println();}\n" + + "// Line comment \n" + + "void foo2() {System.out.println();}\n" + + "/* Block comment */\n" + + "void foo3() {\n" + + "/* statement Block comment */\n" + + "System.out.println();}\n"; + formatSource(source, + "/** Javadoc comment */\n" + + "void foo1() {\n" + + " System.out.println();\n" + + "}\n" + + "\n" + + "// Line comment \n" + + "void foo2() {\n" + + " System.out.println();\n" + + "}\n" + + "\n" + + "/* Block comment */\n" + + "void foo3() {\n" + + " /* statement Block comment */\n" + + " System.out.println();\n" + + "}", + CodeFormatter.K_CLASS_BODY_DECLARATIONS + ); +} +public void testBug236406_CDB2() { + String source = + "/** Javadoc comment */void foo1() {System.out.println();}\n" + + "// Line comment \n" + + "void foo2() {System.out.println();}\n" + + "/* Block comment */\n" + + "void foo3() {\n" + + "/* statement Block comment */\n" + + "System.out.println();}\n"; + formatSource(source, + "/** Javadoc comment */\n" + + "void foo1() {\n" + + " System.out.println();\n" + + "}\n" + + "\n" + + "// Line comment\n" + + "void foo2() {\n" + + " System.out.println();\n" + + "}\n" + + "\n" + + "/* Block comment */\n" + + "void foo3() {\n" + + " /* statement Block comment */\n" + + " System.out.println();\n" + + "}", + CodeFormatter.K_CLASS_BODY_DECLARATIONS | CodeFormatter.F_INCLUDE_COMMENTS + ); +} +public void testBug236406_EX1() { + String source = + "// Line comment \n" + + "i = \n" + + "/** Javadoc comment */\n" + + "1 + (/* Block comment*/++a)\n"; + formatSource(source, + "// Line comment \n" + + "i =\n" + + "/** Javadoc comment */\n" + + "1 + (/* Block comment*/++a)", + CodeFormatter.K_EXPRESSION + ); +} +public void testBug236406_EX2() { + String source = + "// Line comment \n" + + "i = \n" + + "/** Javadoc comment */\n" + + "1 + (/* Block comment*/++a)\n"; + formatSource(source, + "// Line comment\n" + + "i =\n" + + "/** Javadoc comment */\n" + + "1 + (/* Block comment */++a)", + CodeFormatter.K_EXPRESSION | CodeFormatter.F_INCLUDE_COMMENTS + ); +} +public void testBug236406_ST1() { + String source = + "/** Javadoc comment */foo1();\n" + + "// Line comment \n" + + "foo2();\n" + + "/* Block comment */\n" + + "foo3(); {\n" + + "/* indented Block comment */\n" + + "System.out.println();}\n"; + formatSource(source, + "/** Javadoc comment */\n" + + "foo1();\n" + + "// Line comment \n" + + "foo2();\n" + + "/* Block comment */\n" + + "foo3();\n" + + "{\n" + + " /* indented Block comment */\n" + + " System.out.println();\n" + + "}", + CodeFormatter.K_STATEMENTS + ); +} +public void testBug236406_ST2() { + String source = + "/** Javadoc comment */foo1();\n" + + "// Line comment \n" + + "foo2();\n" + + "/* Block comment */\n" + + "foo3(); {\n" + + "/* indented Block comment */\n" + + "System.out.println();}\n"; + formatSource(source, + "/** Javadoc comment */\n" + + "foo1();\n" + + "// Line comment\n" + + "foo2();\n" + + "/* Block comment */\n" + + "foo3();\n" + + "{\n" + + " /* indented Block comment */\n" + + " System.out.println();\n" + + "}", + CodeFormatter.K_STATEMENTS | CodeFormatter.F_INCLUDE_COMMENTS + ); +} + +/** * @bug 237051: [formatter] Formatter insert blank lines after javadoc if javadoc contains Commons Attributes @@ annotations * @test Ensure that Commons Attributes @@ annotations do not screw up the comment formatter * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=237051" 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.255 diff -u -r1.255 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 18 Feb 2010 10:41:40 -0000 1.255 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 18 Feb 2010 12:14:21 -0000 @@ -240,6 +240,10 @@ void formatSource(String source, String formattedOutput) { formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/); } + + void formatSource(String source, String formattedOutput, int kind) { + formatSource(source, formattedOutput, kind, 0, true /*repeat formatting twice*/); + } void formatSource(String source, String formattedOutput, boolean repeat) { formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, repeat); Index: src/org/eclipse/jdt/core/tests/formatter/comment/CommentTestCase.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/comment/CommentTestCase.java,v retrieving revision 1.11 diff -u -r1.11 CommentTestCase.java --- src/org/eclipse/jdt/core/tests/formatter/comment/CommentTestCase.java 27 Jun 2008 16:02:41 -0000 1.11 +++ src/org/eclipse/jdt/core/tests/formatter/comment/CommentTestCase.java 18 Feb 2010 12:14:21 -0000 @@ -24,6 +24,7 @@ import org.eclipse.jdt.core.tests.model.SuiteOfTestCases; public abstract class CommentTestCase extends SuiteOfTestCases { + public static Test buildTestSuite(Class evaluationTestClass) { return buildTestSuite(evaluationTestClass, null); //$NON-NLS-1$ } Index: src/org/eclipse/jdt/core/tests/formatter/comment/JavaDocTestCase.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/comment/JavaDocTestCase.java,v retrieving revision 1.21 diff -u -r1.21 JavaDocTestCase.java --- src/org/eclipse/jdt/core/tests/formatter/comment/JavaDocTestCase.java 27 Jun 2008 16:02:41 -0000 1.21 +++ src/org/eclipse/jdt/core/tests/formatter/comment/JavaDocTestCase.java 18 Feb 2010 12:14:21 -0000 @@ -19,23 +19,14 @@ import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; -import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; -import org.eclipse.jdt.internal.formatter.comment.JavaDocLine; -import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine; import org.eclipse.text.edits.TextEdit; -public class JavaDocTestCase extends CommentTestCase { +public class JavaDocTestCase extends MultiLineTestCase { static { // TESTS_NAMES = new String[] { "test109636_2" } ; } - protected static final String INFIX= MultiCommentLine.MULTI_COMMENT_CONTENT_PREFIX; - - protected static final String POSTFIX= MultiCommentLine.MULTI_COMMENT_END_PREFIX; - - protected static final String PREFIX= JavaDocLine.JAVADOC_START_PREFIX; - public static Test suite() { return buildTestSuite(JavaDocTestCase.class); } @@ -698,7 +689,7 @@ String expected = "/**" + DELIMITER + " *
" + DELIMITER + - (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT ? " * " + DELIMITER : "") + + " * " + DELIMITER + " *" + DELIMITER + " * " + DELIMITER + " * " + DELIMITER + @@ -876,23 +867,8 @@ " * " + DELIMITER + " */"; - String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT - ? input // do not change as
is an immutable tag
- : "/**" + DELIMITER +
- " * " + DELIMITER +
- " * " + DELIMITER +
- " * setLeadingComment("/* traditional comment */"); // correct" + DELIMITER +
- " * setLeadingComment("missing comment delimiters"); // wrong" + DELIMITER +
- " * setLeadingComment("/* unterminated traditional comment "); // wrong" + DELIMITER +
- " * setLeadingComment("/* broken\\n traditional comment */"); // correct" + DELIMITER +
- " * setLeadingComment("// end-of-line comment\\n"); // correct" + DELIMITER +
- " * setLeadingComment("// end-of-line comment without line terminator"); // correct" + DELIMITER +
- " * setLeadingComment("// broken\\n end-of-line comment\\n"); // wrong" + DELIMITER +
- " *
" + DELIMITER +
- " *
" + DELIMITER +
- " */";
String result=testFormat(input, options);
- assertEquals(expected, result);
+ assertEquals(input, result);
}
public void test109636_2() {
@@ -940,37 +916,35 @@
}
public void test109636_4() {
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) {
- Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
+ Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
- String input =
- "/**" + DELIMITER +
- " * " + DELIMITER +
- " * setLeadingComment(\"/* traditional comment */\"); // correct" + DELIMITER +
- " * setLeadingComment(\"missing comment delimiters\"); // wrong" + DELIMITER +
- " * setLeadingComment(\"/* unterminated traditional comment \"); // wrong" + DELIMITER +
- " * setLeadingComment(\"/* broken\\n traditional comment */\"); // correct" + DELIMITER +
- " * setLeadingComment(\"// end-of-line comment\\n\"); // correct" + DELIMITER +
- " * setLeadingComment(\"// end-of-line comment without line terminator\"); // correct" + DELIMITER +
- " * setLeadingComment(\"// broken\\n end-of-line comment\\n\"); // wrong" + DELIMITER +
- " *
" + DELIMITER +
- " */";
-
- String expected =
- "/**" + DELIMITER +
- " * " + DELIMITER +
- " * setLeadingComment("/* traditional comment */"); // correct" + DELIMITER +
- " * setLeadingComment("missing comment delimiters"); // wrong" + DELIMITER +
- " * setLeadingComment("/* unterminated traditional comment "); // wrong" + DELIMITER +
- " * setLeadingComment("/* broken\\n traditional comment */"); // correct" + DELIMITER +
- " * setLeadingComment("// end-of-line comment\\n"); // correct" + DELIMITER +
- " * setLeadingComment("// end-of-line comment without line terminator"); // correct" + DELIMITER +
- " * setLeadingComment("// broken\\n end-of-line comment\\n"); // wrong" + DELIMITER +
- " *
" + DELIMITER +
- " */";
- String result=testFormat(input, options);
- assertEquals(expected, result);
- }
+ String input =
+ "/**" + DELIMITER +
+ " * " + DELIMITER +
+ " * setLeadingComment(\"/* traditional comment */\"); // correct" + DELIMITER +
+ " * setLeadingComment(\"missing comment delimiters\"); // wrong" + DELIMITER +
+ " * setLeadingComment(\"/* unterminated traditional comment \"); // wrong" + DELIMITER +
+ " * setLeadingComment(\"/* broken\\n traditional comment */\"); // correct" + DELIMITER +
+ " * setLeadingComment(\"// end-of-line comment\\n\"); // correct" + DELIMITER +
+ " * setLeadingComment(\"// end-of-line comment without line terminator\"); // correct" + DELIMITER +
+ " * setLeadingComment(\"// broken\\n end-of-line comment\\n\"); // wrong" + DELIMITER +
+ " *
" + DELIMITER +
+ " */";
+
+ String expected =
+ "/**" + DELIMITER +
+ " * " + DELIMITER +
+ " * setLeadingComment("/* traditional comment */"); // correct" + DELIMITER +
+ " * setLeadingComment("missing comment delimiters"); // wrong" + DELIMITER +
+ " * setLeadingComment("/* unterminated traditional comment "); // wrong" + DELIMITER +
+ " * setLeadingComment("/* broken\\n traditional comment */"); // correct" + DELIMITER +
+ " * setLeadingComment("// end-of-line comment\\n"); // correct" + DELIMITER +
+ " * setLeadingComment("// end-of-line comment without line terminator"); // correct" + DELIMITER +
+ " * setLeadingComment("// broken\\n end-of-line comment\\n"); // wrong" + DELIMITER +
+ " *
" + DELIMITER +
+ " */";
+ String result=testFormat(input, options);
+ assertEquals(expected, result);
}
/**
Index: src/org/eclipse/jdt/core/tests/formatter/comment/MultiLineTestCase.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/comment/MultiLineTestCase.java,v
retrieving revision 1.13
diff -u -r1.13 MultiLineTestCase.java
--- src/org/eclipse/jdt/core/tests/formatter/comment/MultiLineTestCase.java 27 Jun 2008 16:02:41 -0000 1.13
+++ src/org/eclipse/jdt/core/tests/formatter/comment/MultiLineTestCase.java 18 Feb 2010 12:14:21 -0000
@@ -17,18 +17,14 @@
import junit.framework.Test;
-import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
-import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine;
-
public class MultiLineTestCase extends CommentTestCase {
static {
// TESTS_NAMES = new String[] { "test170580" } ;
}
- protected static final String INFIX= MultiCommentLine.MULTI_COMMENT_CONTENT_PREFIX;
-
- protected static final String POSTFIX= MultiCommentLine.MULTI_COMMENT_END_PREFIX;
- protected static final String PREFIX= MultiCommentLine.MULTI_COMMENT_START_PREFIX;
+ protected static final String INFIX= " * "; //$NON-NLS-1$
+ protected static final String POSTFIX= " */"; //$NON-NLS-1$
+ protected static final String PREFIX= "/* "; //$NON-NLS-1$
public static Test suite() {
return buildTestSuite(MultiLineTestCase.class);
@@ -116,13 +112,9 @@
" * Member comment\n" +//$NON-NLS-1$
" */";//$NON-NLS-1$
String result= testFormat(input, 0, input.length(), CodeFormatter.K_MULTI_LINE_COMMENT , 2);
- String expectedOutput = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? "/**\n" +
- " * Member comment\n" +
- " */"
- : "/***********************************************************************\n" +
- " * Member comment\n" +
- " */";
+ String expectedOutput = "/**\n" +
+ " * Member comment\n" +
+ " */";
assertEquals("Different output", expectedOutput, result);
}
Index: src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java,v
retrieving revision 1.9
diff -u -r1.9 SingleLineTestCase.java
--- src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java 27 Jun 2008 16:02:40 -0000 1.9
+++ src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java 18 Feb 2010 12:14:21 -0000
@@ -16,11 +16,9 @@
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
-import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
-import org.eclipse.jdt.internal.formatter.comment.SingleCommentLine;
-
public class SingleLineTestCase extends CommentTestCase {
- protected static final String PREFIX= SingleCommentLine.SINGLE_COMMENT_PREFIX;
+
+ protected static final String PREFIX= "// "; //$NON-NLS-1$
static {
// TESTS_NAMES = new String[] { "test109581" } ;
@@ -42,9 +40,7 @@
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test"
- : PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
}
@@ -52,9 +48,7 @@
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test"
- : PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\t\ttest" + DELIMITER + PREFIX + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
}
@@ -62,23 +56,17 @@
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test"
- : PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + PREFIX + "test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
}
public void testCommentBegin1() {
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test"
- : PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test";
assertEquals(expected, testFormat("//test")); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testCommentBegin2() {
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test"
- : PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test";
assertEquals(expected, testFormat(PREFIX + "test")); //$NON-NLS-1$ //$NON-NLS-2$
}
@@ -87,14 +75,12 @@
}
public void testCommentDelimiter1() {
- String expected = PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected += DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + DELIMITER;
assertEquals(expected, testFormat("//\t\ttest " + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testCommentDelimiter2() {
- String expected = PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected += DELIMITER ;
+ String expected = PREFIX + "test" + DELIMITER + DELIMITER ;
assertEquals(expected, testFormat(PREFIX + "test " + DELIMITER + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
}
@@ -123,47 +109,35 @@
}
public void testCommentSpace1() {
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test test"
- : PREFIX + "test test" + DELIMITER;
+ String expected = PREFIX + "test test";
assertEquals(expected, testFormat("//test\t \t test")); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testCommentSpace2() {
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test test"
- : PREFIX + "test test" + DELIMITER;
+ String expected = PREFIX + "test test";
assertEquals(expected, testFormat("//test test")); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testCommentSpace3() {
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test test"
- : PREFIX + "test test" + DELIMITER;
+ String expected = PREFIX + "test test";
assertEquals(expected, testFormat(PREFIX + "test \t \t test")); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testCommentWrapping1() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test" + DELIMITER + PREFIX + "test"
- : PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void testCommentWrapping2() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test" + DELIMITER + PREFIX + "test"
- : PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void testCommentWrapping3() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "32"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test test"
- : PREFIX + "test test" + DELIMITER;
+ String expected = PREFIX + "test test";
assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
@@ -186,58 +160,48 @@
public void testHeaderComment1() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test"
- : PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER;
+ String expected = PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test";
assertEquals(expected, testFormat("//test\t\t\t\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
public void testHeaderComment2() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "24"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? "// test" + DELIMITER + PREFIX + "test test test test" + DELIMITER
- : PREFIX + "test test test test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = "// test" + DELIMITER + PREFIX + "test test test test" + DELIMITER;
assertEquals(expected, testFormat("//test\t\t\t" + DELIMITER + PREFIX + "test test test test" + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
public void testIllegalLineLength1() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
- String expected =PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void testIllegalLineLength2() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "-16"); //$NON-NLS-1$
- String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat(PREFIX + "\t\t test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void testMultipleComments1() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
- String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("//test test" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
}
public void testMultipleComments2() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
- String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
- ? "// test test" + DELIMITER +
- "// test" + DELIMITER +
- "// " + DELIMITER +
- "// test test" + DELIMITER +
- "// test test"
- : PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test" + DELIMITER;
+ String expected = "// test test" + DELIMITER +
+ "// test" + DELIMITER +
+ "// " + DELIMITER +
+ "// test test" + DELIMITER +
+ "// test test";
assertEquals(expected, testFormat("//test test\ttest" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
}
public void testMultipleComments3() {
setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "11"); //$NON-NLS-1$
- String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
- if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
+ String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
assertEquals(expected, testFormat("// test\t\t\ttest\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
}