### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java,v retrieving revision 1.79 diff -u -r1.79 DefaultCodeFormatterConstants.java --- formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 6 Mar 2007 02:38:51 -0000 1.79 +++ formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 16 Mar 2007 17:31:44 -0000 @@ -865,6 +865,18 @@ public static final String FORMATTER_INDENT_EMPTY_LINES = JavaCore.PLUGIN_ID + ".formatter.indent_empty_lines"; //$NON-NLS-1$ /** *
+ * FORMATTER / Option to indent comments that start on the first column + * - option id: "org.eclipse.jdt.core.formatter.indent_comments_on_first_column" + * - possible values: { TRUE, FALSE } + * - default: FALSE + *+ * @see #TRUE + * @see #FALSE + * @since 3.3 + */ + public static final String FORMATTER_INDENT_COMMENTS_ON_FIRST_COLUMN = JavaCore.PLUGIN_ID + ".formatter.indent_comments_on_first_column"; //$NON-NLS-1$ + /** + *
* FORMATTER / Option to indent statements inside a block * - option id: "org.eclipse.jdt.core.formatter.indent_statements_compare_to_block" * - possible values: { TRUE, FALSE } Index: formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java,v retrieving revision 1.7 diff -u -r1.7 CodeFormatterApplication.java --- formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java 11 Jan 2007 15:29:24 -0000 1.7 +++ formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java 16 Mar 2007 17:31:44 -0000 @@ -21,10 +21,12 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Map; import java.util.Properties; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.internal.core.util.Util; import org.eclipse.jface.text.BadLocationException; @@ -158,7 +160,7 @@ private String configName; - private Properties options = null; + private Map options = null; private static final String PDE_LAUNCH = "-pdelaunch"; //$NON-NLS-1$ @@ -374,6 +376,12 @@ } // format the list of files and/or directories + if (this.options == null) { + this.options = JavaCore.getOptions(); + this.options.put( + DefaultCodeFormatterConstants.FORMATTER_INDENT_COMMENTS_ON_FIRST_COLUMN, + DefaultCodeFormatterConstants.TRUE); + } final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(this.options); for (int i = 0, max = filesToFormat.length; i < max; i++) { final File file = filesToFormat[i]; @@ -381,7 +389,7 @@ formatDirTree(file, codeFormatter); } else if (Util.isJavaLikeFileName(file.getPath())) { formatFile(file, codeFormatter); - } + } } if (!this.quiet) { System.out.println(Messages.bind(Messages.CommandLineDone)); Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v retrieving revision 1.105 diff -u -r1.105 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 15 Mar 2007 18:53:55 -0000 1.105 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 16 Mar 2007 17:31:45 -0000 @@ -608,6 +608,14 @@ this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; } + private boolean isOnFirstColumn(int start) { + if (this.lineEnds == null) return start == 0; + int index = Arrays.binarySearch(this.lineEnds, start); + // we want the line end of the previous line + int previousLineEnd = this.getLineEnd(-index - 1); + return previousLineEnd != -1 && previousLineEnd == start - 1; + } + private boolean isValidEdit(OptimizedReplaceEdit edit) { final int editLength= edit.length; final int editReplacementLength= edit.replacement.length(); @@ -663,7 +671,9 @@ handleLineTooLong(); } this.lastNumberOfNewLines = 0; - printIndentationIfNecessary(); + if (this.indentationLevel != 0) { + printIndentationIfNecessary(); + } if (considerSpaceIfAny) { this.space(); } @@ -685,7 +695,16 @@ boolean isNewLine = false; int start = currentTokenStartPosition; int nextCharacterStart = currentTokenStartPosition; - printIndentationIfNecessary(); + boolean indentComment = false; + if (this.indentationLevel != 0) { + if (this.formatter.preferences.indent_comments_on_first_column) { + indentComment = true; + printIndentationIfNecessary(); + } else if (!isOnFirstColumn(start)) { + indentComment = true; + printIndentationIfNecessary(); + } + } if (this.pendingSpace) { this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ } @@ -728,7 +747,9 @@ StringBuffer buffer = new StringBuffer(); buffer.append(this.lineSeparator); - printIndentationIfNecessary(buffer); + if (indentComment) { + printIndentationIfNecessary(buffer); + } buffer.append(' '); addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); @@ -982,7 +1003,13 @@ int currentCharacter; int start = currentTokenStartPosition; int nextCharacterStart = currentTokenStartPosition; - printIndentationIfNecessary(); + if (this.indentationLevel != 0) { + if (this.formatter.preferences.indent_comments_on_first_column) { + printIndentationIfNecessary(); + } else if (!isOnFirstColumn(start)) { + printIndentationIfNecessary(); + } + } if (this.pendingSpace) { this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ } 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.196 diff -u -r1.196 CodeFormatterVisitor.java --- formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 6 Mar 2007 02:38:50 -0000 1.196 +++ formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 16 Mar 2007 17:31:45 -0000 @@ -3157,7 +3157,9 @@ if (constructorDeclaration.ignoreFurtherInvestigation) { this.scribe.printComment(); - this.scribe.printIndentationIfNecessary(); + if (this.scribe.indentationLevel != 0) { + this.scribe.printIndentationIfNecessary(); + } this.scribe.scanner.resetTo(constructorDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1); this.scribe.printTrailingComment(); switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) { @@ -4041,7 +4043,9 @@ if (methodDeclaration.ignoreFurtherInvestigation) { this.scribe.printComment(); - this.scribe.printIndentationIfNecessary(); + if (this.scribe.indentationLevel != 0) { + this.scribe.printIndentationIfNecessary(); + } this.scribe.scanner.resetTo(methodDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1); this.scribe.printTrailingComment(); switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) { Index: formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java,v retrieving revision 1.82 diff -u -r1.82 DefaultCodeFormatterOptions.java --- formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 6 Mar 2007 02:38:50 -0000 1.82 +++ formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 16 Mar 2007 17:31:45 -0000 @@ -117,6 +117,7 @@ public boolean indent_body_declarations_compare_to_enum_declaration_header; public boolean indent_body_declarations_compare_to_type_header; public boolean indent_breaks_compare_to_cases; + public boolean indent_comments_on_first_column; public boolean indent_empty_lines; public boolean indent_switchstatements_compare_to_cases; public boolean indent_switchstatements_compare_to_switch; @@ -390,6 +391,7 @@ options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ENUM_DECLARATION_HEADER, this.indent_body_declarations_compare_to_enum_declaration_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_TYPE_HEADER, this.indent_body_declarations_compare_to_type_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES, this.indent_breaks_compare_to_cases ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); + options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_COMMENTS_ON_FIRST_COLUMN, this.indent_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, this.indent_empty_lines ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, this.indent_switchstatements_compare_to_cases ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, this.indent_switchstatements_compare_to_switch ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); @@ -1100,6 +1102,10 @@ if (indentBreaksCompareToCasesOption != null) { this.indent_breaks_compare_to_cases = DefaultCodeFormatterConstants.TRUE.equals(indentBreaksCompareToCasesOption); } + final Object indentCommentsOnFirstColumnOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_COMMENTS_ON_FIRST_COLUMN); + if (indentCommentsOnFirstColumnOption != null) { + this.indent_comments_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(indentCommentsOnFirstColumnOption); + } final Object indentEmptyLinesOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES); if (indentEmptyLinesOption != null) { this.indent_empty_lines = DefaultCodeFormatterConstants.TRUE.equals(indentEmptyLinesOption); @@ -1961,6 +1967,7 @@ this.indent_body_declarations_compare_to_enum_declaration_header = true; this.indent_body_declarations_compare_to_type_header = true; this.indent_breaks_compare_to_cases = true; + this.indent_comments_on_first_column = true; this.indent_empty_lines = false; this.indent_switchstatements_compare_to_cases = true; this.indent_switchstatements_compare_to_switch = true; @@ -2151,6 +2158,7 @@ setJavaConventionsSettings(); this.tab_char = TAB; this.tab_size = 4; + this.indent_comments_on_first_column = false; } public void setJavaConventionsSettings() { @@ -2220,6 +2228,7 @@ this.indent_body_declarations_compare_to_enum_declaration_header = true; this.indent_body_declarations_compare_to_type_header = true; this.indent_breaks_compare_to_cases = true; + this.indent_comments_on_first_column = true; this.indent_empty_lines = false; this.indent_switchstatements_compare_to_cases = true; this.indent_switchstatements_compare_to_switch = false;