diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 53cfe276600..573be87eeee 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,15 @@ +2004-09-07 Alain Magloire + + Part of the uncoming work to supply formatting in CDT + * schema/CodeFormatter.exsd + * src/org/eclipse/cdt/core/CodePreferenceConstants.java + * src/org/eclipse/cdt/core/ToolFactory.java + * src/org/eclipse/cdt/core/formatter/CodeFormatter.java + * src/org/eclipse/cdt/core/formatter/CodeFormatterConstants.java + * src/org/eclipse/cdt/internal/core/CCorePreferenceInitializer.java + * plugin.properties + * plugin.xml + 2004-09-03 Alain Magloire Provide the extension for the Core Plugin Preference Initializer. diff --git a/core/org.eclipse.cdt.core/plugin.properties b/core/org.eclipse.cdt.core/plugin.properties index 1fabec01da0..9fad306050b 100644 --- a/core/org.eclipse.cdt.core/plugin.properties +++ b/core/org.eclipse.cdt.core/plugin.properties @@ -20,6 +20,8 @@ CBuildConsole.name=C Builder Console CProject.name=C Project ProcessList.name=Process List ErrorParser.name=Error Parser +CodeFormatter.name=C/C++ Code Formatter +CodeFormatter.name=C/C++ Code Formatters CTaskName=C/C++ Task @@ -54,3 +56,8 @@ fragmentName.qnx = C/C++ Development Tools Core for QNX fragmentName.solaris = C/C++ Development Tools Core for Solaris fragmentName.win32 = C/C++ Development Tools Core for Windows fragmentName.macosx = C/C++ Development Tools Core for MacOS X + +CodeFormatter.name=C/C++ Code Formatter +CodeFormatter.name=C/C++ Code Formatters +DefaultCodeFormatter.name=Default Formatter +DefaultCodeFormatter.name=Default Formatter \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/plugin.xml b/core/org.eclipse.cdt.core/plugin.xml index 6e02a4f0483..4066259eed9 100644 --- a/core/org.eclipse.cdt.core/plugin.xml +++ b/core/org.eclipse.cdt.core/plugin.xml @@ -19,6 +19,7 @@ + @@ -53,6 +54,7 @@ + @@ -438,6 +440,15 @@ type="org.eclipse.core.resources.problemmarker"> + diff --git a/core/org.eclipse.cdt.core/schema/CodeFormatter.exsd b/core/org.eclipse.cdt.core/schema/CodeFormatter.exsd new file mode 100644 index 00000000000..3dc5cada4d8 --- /dev/null +++ b/core/org.eclipse.cdt.core/schema/CodeFormatter.exsd @@ -0,0 +1,116 @@ + + + + + + + + + [Enter description of this extension point.] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A unique name that will be used to reference this code formatter + + + + + + + A translatable name that will be used for presenting this formatter in the UI. + + + + + + + a fully qualified name of the Java class that implements <samp>org.eclipse.cdt.core.formatter.CodeFormatter</samp> abstract class. + + + + + + + + + + + + + + + [Enter the first release in which this extension point appears.] + + + + + + + + + [Enter extension point usage example here.] + + + + + + + + + [Enter API information here.] + + + + + + + + + [Enter information about supplied implementation of this extension point.] + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePreferenceConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePreferenceConstants.java index 96142a7edff..210838e2e0a 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePreferenceConstants.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePreferenceConstants.java @@ -34,4 +34,15 @@ public class CCorePreferenceConstants { */ public static final String TRANSLATION_TASK_PRIORITIES = CCorePlugin.PLUGIN_ID + ".translation.taskPriorities"; //$NON-NLS-1$ + /** + * Active code formatter ID. + * @see #getDefaultOptions + */ + public static final String CODE_FORMATTER = CCorePlugin.PLUGIN_ID + ".code_formatter"; //$NON-NLS-1$ + + /** + * Default code formatter + */ + public static final String DEFAULT_CODE_FORMATTER = CCorePlugin.PLUGIN_ID + ".defaultCodeFormatter"; //$NON-NLS-1$ + } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/ToolFactory.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/ToolFactory.java new file mode 100644 index 00000000000..4725eaff0a4 --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/ToolFactory.java @@ -0,0 +1,76 @@ +/* + * Created on Aug 17, 2004 + * + * Copyright 2004, QNX Software Systems Ltd. All Rights Reserved. + * + * This source code may contain confidential information of QNX Software + * Systems Ltd. (QSSL) and its licensors. Any use, reproduction, + * modification, disclosure, distribution or transfer of this software, + * or any software which includes or is based upon any of this code, is + * prohibited unless expressly authorized by QSSL by written agreement. For + * more information (including whether this source code file has been + * published) please email licensing@qnx.com. + */ +package org.eclipse.cdt.core; + +import java.util.Map; + +import org.eclipse.cdt.core.formatter.CodeFormatter; +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.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; + +/** + * @author AChapiro + */ +public class ToolFactory { + + /** + * Create an instance of the built-in code formatter. + * @param options - the options map to use for formatting with the default code formatter. Recognized options + * are documented on CCorePlugin#getDefaultOptions(). If set to null, then use + * the current settings from CCorePlugin#getOptions. + * @return an instance of the built-in code formatter + * @see CodeFormatter + * @see CCorePlugin#getOptions() + */ + public static CodeFormatter createCodeFormatter(Map options){ + if (options == null) + options = CCorePlugin.getOptions(); + String formatterID = (String)options.get(CCorePreferenceConstants.CODE_FORMATTER); + IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry(); + IExtensionPoint extension = Platform.getExtensionRegistry(). + getExtensionPoint(CCorePlugin.PLUGIN_ID, "CodeFormatter"); + if (extension != null) { + IExtension[] extensions = extension.getExtensions(); + for(int i = 0; i < extensions.length; i++){ + IConfigurationElement [] configElements = extensions[i].getConfigurationElements(); + for(int j = 0; j < configElements.length; j++){ + String initializerID = configElements[j].getAttribute("id"); //$NON-NLS-1$ + if (initializerID != null && initializerID.equals(formatterID)){ + try { + Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$ + if (execExt instanceof CodeFormatter){ + CodeFormatter formatter = (CodeFormatter)execExt; + formatter.setOptions(options); + return formatter; + } + } catch(CoreException e) { + //TODO: add more reasonable error processing + e.printStackTrace(); + break; + } + } + } + } + } + // TODO: open this code later + // return new DefaultCodeFormatter(options); + return null; + } + + +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatter.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatter.java new file mode 100644 index 00000000000..9add11618bc --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatter.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.formatter; + +import java.util.Map; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.text.edits.TextEdit; + +/** + * Specification for a generic source code formatter. + * + * @since 3.0 + */ +public abstract class CodeFormatter { + + /** + * Unknown kind + */ + public static final int K_UNKNOWN = 0x00; + + /** + * Kind used to format an expression + */ + public static final int K_EXPRESSION = 0x01; + + /** + * Kind used to format a set of statements + */ + public static final int K_STATEMENTS = 0x02; + + /** + * Kind used to format a set of class body declarations + */ + public static final int K_CLASS_BODY_DECLARATIONS = 0x04; + + /** + * Kind used to format a compilation unit + */ + public static final int K_COMPILATION_UNIT = 0x08; + + /** + * Format source, + * and returns a text edit that correspond to the difference between the given string and the formatted string. + * It returns null if the given string cannot be formatted. + * + * If the offset position is matching a whitespace, the result can include whitespaces. It would be up to the + * caller to get rid of preceeding whitespaces. + * + * @param kind Use to specify the kind of the code snippet to format. It can be any of these: + * K_EXPRESSION, K_STATEMENTS, K_CLASS_BODY_DECLARATIONS, K_COMPILATION_UNIT, K_UNKNOWN + * @param file - file associated with this source (null if no file is associated) + * @param document the document to format + * @param offset the given offset to start recording the edits (inclusive). + * @param length the given length to stop recording the edits (exclusive). + * @param indentationLevel the initial indentation level, used + * to shift left/right the entire source fragment. An initial indentation + * level of zero or below has no effect. + * @param lineSeparator the line separator to use in formatted source, + * if set to null, then the platform default one will be used. + * @return the text edit + * @throws IllegalArgumentException if offset is lower than 0, length is lower than 0 or + * length is greater than source length. + */ + public abstract TextEdit format(int kind, IDocument document, int offset, int length, int indentationLevel, String lineSeparator); + + /** + * @param options - general formatter options + */ + public abstract void setOptions(Map options); +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatterConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatterConstants.java new file mode 100644 index 00000000000..e80a775f53c --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/CodeFormatterConstants.java @@ -0,0 +1,42 @@ +/* + * Created on Sep 5, 2004 + * + * TODO To change the template for this generated file go to + * Window - Preferences - Java - Code Style - Code Templates + */ +package org.eclipse.cdt.core.formatter; + +import org.eclipse.cdt.core.CCorePlugin; + +/** + * @author Alex Chapiro + * + * TODO To change the template for this generated type comment go to + * Window - Preferences - Java - Code Style - Code Templates + */ +public class CodeFormatterConstants { + + + /** + *
+	 * FORMATTER / Option for alignment of arguments in allocation expression
+	 *     - option id:         "org.eclipse.jdt.core.formatter.language"
+	 *     - possible values:   values proposed in class ParserLanguage 
+	 *     - default:           ParserLanguage.CPP
+	 * 
+ */ + public static final String FORMATTER_LANGUAGE = CCorePlugin.PLUGIN_ID + ".formatter.language"; //$NON-NLS-1$ + + /** + *
+	 * FORMATTER / Option for alignment of arguments in allocation expression
+	 *     - option id:         "org.eclipse.jdt.core.formatter.current_file"
+	 *     - possible values:   object of class IFile or null 
+	 *     - default:           null
+	 * 
+ */ + public static final String FORMATTER_CURRENT_FILE = CCorePlugin.PLUGIN_ID + ".formatter.current_file"; //$NON-NLS-1$ + + + +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/CCorePreferenceInitializer.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/CCorePreferenceInitializer.java index 5ccc3c473f7..5795d15de3d 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/CCorePreferenceInitializer.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/CCorePreferenceInitializer.java @@ -17,6 +17,8 @@ import org.eclipse.cdt.core.CCorePreferenceConstants; import org.eclipse.cdt.internal.core.model.CModelManager; import org.eclipse.core.runtime.Preferences; import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +// import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager; +// import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore; public class CCorePreferenceInitializer extends AbstractPreferenceInitializer { @@ -34,6 +36,10 @@ public class CCorePreferenceInitializer extends AbstractPreferenceInitializer { preferences.setDefault(CCorePreferenceConstants.TRANSLATION_TASK_PRIORITIES, CCorePreferenceConstants.DEFAULT_TASK_PRIORITY); optionNames.add(CCorePreferenceConstants.TRANSLATION_TASK_PRIORITIES); + + preferences.setDefault(CCorePreferenceConstants.CODE_FORMATTER, CCorePreferenceConstants.DEFAULT_CODE_FORMATTER); + optionNames.add(CCorePreferenceConstants.CODE_FORMATTER); + } }