### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/core/ToolFactory.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/ToolFactory.java,v retrieving revision 1.72 diff -u -r1.72 ToolFactory.java --- model/org/eclipse/jdt/core/ToolFactory.java 29 Sep 2006 17:13:57 -0000 1.72 +++ model/org/eclipse/jdt/core/ToolFactory.java 23 May 2007 10:12:09 -0000 @@ -13,14 +13,20 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.Plugin; import org.eclipse.jdt.core.compiler.IScanner; import org.eclipse.jdt.core.formatter.CodeFormatter; +import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; import org.eclipse.jdt.core.util.ClassFormatException; import org.eclipse.jdt.core.util.IClassFileReader; @@ -45,8 +51,42 @@ * @since 2.0 */ public class ToolFactory { + /** + * If set, this mode tells the default code formatter to honor the options that never indent comments on + * first column. Note that if these options are disabled, then they remain disabled even if this mode is set. + * If this mode is not set, these options are ignored, even if they are enabled. + *

See also {@link #M_FORMAT_NEW} and {@link #M_REFORMAT} that combine all flags that are typically used for formatting + * new or existing source

+ * + * @see DefaultCodeFormatterConstants#FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN + * @see DefaultCodeFormatterConstants#FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN + * @see #createCodeFormatter(Map, int) + * @since 3.3 + */ + public static int M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS = 0x01; /** + * This constant combines all modes to be used for formatting new code. In particular, options that preserve the indentation + * of comments are not used. Clients that are formatting new code are recommended to use this flag over the specific ones. + * + * @see #M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS + * @see #createCodeFormatter(Map, int) + * @since 3.3 + */ + public static int M_FORMAT_NEW= new Integer(0).intValue(); + + + /** + * This constant combines all modes to be used for (re-) formatting existing code. In particular, options that preserve the indentation + * of comments are used. Clients that are (re-) formatting existing code are recommended to use this flag over the specific flags. + * + * @see #M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS + * @see #createCodeFormatter(Map, int) + * @since 3.3 + */ + public static int M_REFORMAT = new Integer(M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS).intValue(); + + /** * Create an instance of a code formatter. A code formatter implementation can be contributed via the * extension point "org.eclipse.jdt.core.codeFormatter". If unable to find a registered extension, the factory * will default to using the default code formatter. @@ -89,6 +129,9 @@ * the compiler compliance level ({@link JavaCore#COMPILER_COMPLIANCE}) and the target platform * ({@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}). * Without these options, it is not possible for the code formatter to know what kind of source it needs to format. + *

+ * Note this is equivalent to createCodeFormatter(options, M_FORMAT_NEW). Thus some code formatter options + * may be ignored since the corresponding mode is not set. *

* @param options - the options map to use for formatting with the default code formatter. Recognized options * are documented on JavaCore#getDefaultOptions(). If set to null, then use @@ -99,8 +142,41 @@ * @since 3.0 */ public static CodeFormatter createCodeFormatter(Map options){ + return createCodeFormatter(options, 0); + } + + /** + * Create an instance of the built-in code formatter. + *

The given options should at least provide the source level ({@link JavaCore#COMPILER_SOURCE}), + * the compiler compliance level ({@link JavaCore#COMPILER_COMPLIANCE}) and the target platform + * ({@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}). + * Without these options, it is not possible for the code formatter to know what kind of source it needs to format. + *

+ *

The given mode is a bitwise value. It is used to determine what options should be enabled when + * formatting the code.

+ *

The list of bits used to set the mode includes {@link #M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS}, but + * other bits are left for future use. {@link #M_FORMAT_NEW} and {@link #M_REFORMAT} combine the flags to be used when formatting + * new or when formatting existing code.

+ * + * @param options the options map to use for formatting with the default code formatter. Recognized options + * are documented on JavaCore#getDefaultOptions(). If set to null, then use + * the current settings from JavaCore#getOptions. + * @param mode the given mode to modify the given options. + * + * @return an instance of the built-in code formatter + * @see CodeFormatter + * @see JavaCore#getOptions() + * @since 3.3 + */ + public static CodeFormatter createCodeFormatter(Map options, int mode) { if (options == null) options = JavaCore.getOptions(); - return new DefaultCodeFormatter(options); + Map currentOptions = new HashMap(options); + if ((mode & M_FORMAT_HONOR_NEVER_INDENT_COMMENT_OPTIONS) == 0) { + // disable the option for not indenting comments starting on first column + currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); + currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); + } + return new DefaultCodeFormatter(currentOptions); } /**