diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog
index 08558934f9f..62c877c52b2 100644
--- a/core/org.eclipse.cdt.core/ChangeLog
+++ b/core/org.eclipse.cdt.core/ChangeLog
@@ -1,3 +1,11 @@
+2005-01-28 Alain Magloire
+ Operation on translationUnit
+ * model/org/eclipse/cdt/core/model/ITranslationUnit.java
+ * model/org/eclipse/cdt/internal/core/model/CreateIncludeOperation.java
+ * model/org/eclipse/cdt/internal/core/model/CreateNamespaceOperation.java
+ * model/org/eclipse/cdt/internal/core/model/CreateUsingOperation.java
+ * model/org/eclipse/cdt/internal/core/model/TranslationUnit.java
+
2005-01-26 Alain Magloire
The line and offset information should be cache in the SourceManipulationInfo
* model/org/eclipse/cdt/iternal/core/BinaryElement.java
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java
index 8b5bd952f5b..b7b49617d8c 100644
--- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java
@@ -45,16 +45,13 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
*
The name is not a valid import name (INVALID_NAME)
*
*/
- IInclude createInclude(String name, ICElement sibling, IProgressMonitor monitor) throws CModelException;
+ IInclude createInclude(String name, boolean isStd, ICElement sibling, IProgressMonitor monitor) throws CModelException;
/**
- * Creates and returns a namesapce declaration in this translation unit
- * with the given package name.
+ * Creates and returns a using declaration/directive in this translation unit
*
- * If the translation unit already includes the specified package declaration,
- * it is not generated (it does not generate duplicates).
*
- * @param name the name of the namespace declaration to add (For example, "std"
)
+ * @param name the name of the using
* @param monitor the progress monitor to notify
* @return the newly inserted namespace declaration (or the previously existing one in case attempting to create a duplicate)
*
@@ -65,7 +62,23 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
*
The name is not a valid package name (INVALID_NAME)
*
*/
- IUsing createUsing (String name, IProgressMonitor monitor) throws CModelException;
+ IUsing createUsing (String name, boolean isDirective, ICElement sibling, IProgressMonitor monitor) throws CModelException;
+
+ /**
+ * Creates and returns a namespace in this translation unit
+ *
+ * @param name the name of the namespace
+ * @param monitor the progress monitor to notify
+ * @return the newly inserted namespace declaration (or the previously existing one in case attempting to create a duplicate)
+ *
+ * @exception CModelException if the element could not be created. Reasons include:
+ *
+ * - This C element does not exist (ELEMENT_DOES_NOT_EXIST)
+ * - A
CoreException
occurred while updating an underlying resource
+ * - The name is not a valid package name (INVALID_NAME)
+ *
+ */
+ INamespace createNamespace (String namespace, ICElement sibling, IProgressMonitor monitor) throws CModelException;
/**
* Finds the shared working copy for this element, given a IBuffer
factory.
@@ -248,13 +261,32 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
IWorkingCopy getSharedWorkingCopy(IProgressMonitor monitor, IBufferFactory factory, IProblemRequestor requestor) throws CModelException;
/**
- * Returns the first namespace declaration in this translation unit with the given package name
+ * Returns the first using in this translation unit with the name
* This is a handle-only method. The namespace declaration may or may not exist.
*
* @param name the name of the namespace declaration (For example, "std"
)
*/
IUsing getUsing(String name);
+ /**
+ * Returns the usings in this translation unit
+ * in the order in which they appear in the source.
+ *
+ * @return an array of namespace declaration (normally of size one)
+ *
+ * @exception CModelException if this element does not exist or if an
+ * exception occurs while accessing its corresponding resource
+ */
+ IUsing[] getUsings() throws CModelException;
+
+ /**
+ * Returns the first namespace declaration in this translation unit with the given name
+ * This is a handle-only method. The namespace declaration may or may not exist.
+ *
+ * @param name the name of the namespace declaration (For example, "std"
)
+ */
+ INamespace getNamespace(String name);
+
/**
* Returns the namespace declarations in this translation unit
* in the order in which they appear in the source.
@@ -264,7 +296,7 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
* @exception CModelException if this element does not exist or if an
* exception occurs while accessing its corresponding resource
*/
- IUsing[] getUsings() throws CModelException;
+ INamespace[] getNamespaces() throws CModelException;
/**
* True if its a header.
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateIncludeOperation.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateIncludeOperation.java
index 33165ecc058..f71839c9858 100644
--- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateIncludeOperation.java
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateIncludeOperation.java
@@ -72,9 +72,8 @@ public class CreateIncludeOperation extends CreateElementInTUOperation {
/**
* Sets the correct position for the new include:
* - after the last include
- *
- if no include, before the first type
- *
- if no type, after the package statement
- *
- and if no package statement - first thing in the CU
+ *
+ * if no include
*/
protected void initializeDefaultPosition() {
try {
@@ -109,6 +108,9 @@ public class CreateIncludeOperation extends CreateElementInTUOperation {
return CModelStatus.VERIFIED_OK;
}
+ /*
+ * TODO: Use the ASTRewrite once it is available.
+ */
protected String generateElement(ITranslationUnit unit) throws CModelException {
StringBuffer sb = new StringBuffer();
sb.append("#include "); //$NON-NLS-1$;
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateNamespaceOperation.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateNamespaceOperation.java
new file mode 100644
index 00000000000..8207b7e90ae
--- /dev/null
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateNamespaceOperation.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.cdt.internal.core.model;
+
+import org.eclipse.cdt.core.model.CModelException;
+import org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.cdt.core.model.IInclude;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+
+/**
+ * @author User
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class CreateNamespaceOperation extends CreateElementInTUOperation {
+
+
+ /**
+ * The name of the include to be created.
+ */
+ protected String fNamespace;
+
+ /**
+ * When executed, this operation will add an include to the given translation unit.
+ */
+ public CreateNamespaceOperation(String namespace, ITranslationUnit parentElement) {
+ super(parentElement);
+ fNamespace = namespace;
+ }
+
+ /**
+ * @see CreateElementInCUOperation#generateResultHandle
+ */
+ protected ICElement generateResultHandle() {
+ return getTranslationUnit().getNamespace(fNamespace);
+ }
+
+ /**
+ * @see CreateElementInCUOperation#getMainTaskName
+ */
+ public String getMainTaskName(){
+ return "operation.createNamespaceProgress"; //$NON-NLS-1$
+ }
+
+ /**
+ * Sets the correct position for the new namespace:
+ * - after the last namespace
+ *
+ */
+ protected void initializeDefaultPosition() {
+ try {
+ ITranslationUnit cu = getTranslationUnit();
+ IInclude[] includes = cu.getIncludes();
+ if (includes.length > 0) {
+ createAfter(includes[includes.length - 1]);
+ return;
+ }
+ } catch (CModelException npe) {
+ }
+ }
+
+ /*
+ * TODO: Use the ASTRewrite once it is available.
+ */
+ protected String generateElement(ITranslationUnit unit) throws CModelException {
+ StringBuffer sb = new StringBuffer();
+ sb.append("namespace "); //$NON-NLS-1$;
+ sb.append(fNamespace).append(' ').append('{');
+ sb.append(Util.LINE_SEPARATOR);
+ sb.append('}'); //$NON-NLS-1$;
+ sb.append(Util.LINE_SEPARATOR);
+ return sb.toString();
+ }
+}
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateUsingOperation.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateUsingOperation.java
new file mode 100644
index 00000000000..3a51a02c7de
--- /dev/null
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CreateUsingOperation.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.cdt.internal.core.model;
+
+import org.eclipse.cdt.core.model.CModelException;
+import org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.cdt.core.model.IUsing;
+
+/**
+ * This operation adds a using declaration/directive to an existing translation unit.
+ *
+ *
Required Attributes:
+ * - Translation unit
+ *
- using name - the name of the using to add
+ *
+ */
+public class CreateUsingOperation extends CreateElementInTUOperation {
+
+ /**
+ * The name of the using to be created.
+ */
+ protected String fUsingName;
+
+ /**
+ * Whether it is a declaration or a directive.
+ */
+ protected boolean fIsDirective;
+
+ /**
+ * When executed, this operation will add an include to the given translation unit.
+ */
+ public CreateUsingOperation(String usingName, boolean isDirective, ITranslationUnit parentElement) {
+ super(parentElement);
+ fIsDirective = isDirective;
+ fUsingName = usingName;
+ }
+
+ /**
+ * @see CreateElementInCUOperation#generateResultHandle
+ */
+ protected ICElement generateResultHandle() {
+ return getTranslationUnit().getUsing(fUsingName);
+ }
+
+ /**
+ * @see CreateElementInCUOperation#getMainTaskName
+ */
+ public String getMainTaskName(){
+ return "operation.createUsingProgress"; //$NON-NLS-1$
+ }
+
+ /**
+ * Sets the correct position for the new using:
+ * - after the last using
+ *
+ * if no using
+ */
+ protected void initializeDefaultPosition() {
+ try {
+ ITranslationUnit cu = getTranslationUnit();
+ IUsing[] usings = cu.getUsings();
+ if (usings.length > 0) {
+ createAfter(usings[usings.length - 1]);
+ return;
+ }
+ } catch (CModelException npe) {
+ }
+ }
+
+ /*
+ * TODO: Use the ASTRewrite once it is available.
+ */
+ protected String generateElement(ITranslationUnit unit) throws CModelException {
+ StringBuffer sb = new StringBuffer();
+ sb.append("using "); //$NON-NLS-1$;
+ if (fIsDirective) {
+ sb.append("namespace "); //$NON-NLS-1$
+ }
+ sb.append(fUsingName);
+ sb.append(';');
+ sb.append(Util.LINE_SEPARATOR);
+ return sb.toString();
+ }
+}
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnit.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnit.java
index 04bf93703cc..0f7baa5c5a7 100644
--- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnit.java
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnit.java
@@ -18,6 +18,7 @@ import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IInclude;
+import org.eclipse.cdt.core.model.INamespace;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.IProblemRequestor;
import org.eclipse.cdt.core.model.ISourceRange;
@@ -63,13 +64,32 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return this;
}
- public IInclude createInclude(String name, ICElement sibling, IProgressMonitor monitor)
+ public IInclude createInclude(String includeName, boolean isStd, ICElement sibling, IProgressMonitor monitor)
throws CModelException {
- return null;
+ CreateIncludeOperation op = new CreateIncludeOperation(includeName, isStd, this);
+ if (sibling != null) {
+ op.createBefore(sibling);
+ }
+ CModelManager.getDefault().runOperation(op, monitor);
+ return getInclude(includeName);
}
- public IUsing createUsing(String name, IProgressMonitor monitor) throws CModelException {
- return null;
+ public IUsing createUsing(String usingName, boolean isDirective, ICElement sibling, IProgressMonitor monitor) throws CModelException {
+ CreateIncludeOperation op = new CreateIncludeOperation(usingName, isDirective, this);
+ if (sibling != null) {
+ op.createBefore(sibling);
+ }
+ CModelManager.getDefault().runOperation(op, monitor);
+ return getUsing(usingName);
+ }
+
+ public INamespace createNamespace(String namespace, ICElement sibling, IProgressMonitor monitor) throws CModelException {
+ CreateNamespaceOperation op = new CreateNamespaceOperation(namespace, this);
+ if (sibling != null) {
+ op.createBefore(sibling);
+ }
+ CModelManager.getDefault().runOperation(op, monitor);
+ return getNamespace(namespace);
}
public ICElement getElementAtLine(int line) throws CModelException {
@@ -105,6 +125,17 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (name == null || name.length() == 0) {
return null;
}
+ try {
+ ICElement[] celements = getChildren();
+ for (int i = 0; i < celements.length; i++) {
+ if (name.equals(celements[i].getElementName())) {
+ return celements[i];
+ }
+ }
+ } catch (CModelException e) {
+ //
+ }
+
String[] names = name.split("::"); //$NON-NLS-1$
ICElement current = this;
for (int j = 0; j < names.length; ++j) {
@@ -180,6 +211,45 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return (IUsing[])aList.toArray(new IUsing[0]);
}
+ public INamespace getNamespace(String name) {
+ try {
+ String[] names = name.split("::"); //$NON-NLS-1$
+ ICElement current = this;
+ for (int j = 0; j < names.length; ++j) {
+ if (current instanceof IParent) {
+ ICElement[] celements = ((IParent)current).getChildren();
+ current = null;
+ for (int i = 0; i < celements.length; i++) {
+ if (celements[i].getElementType() == ICElement.C_NAMESPACE) {
+ if (name.equals(celements[i].getElementName())) {
+ current = celements[i];
+ break;
+ }
+ }
+ }
+ } else {
+ current = null;
+ }
+ }
+ if (current instanceof INamespace) {
+ return (INamespace)current;
+ }
+ } catch (CModelException e) {
+ }
+ return null;
+ }
+
+ public INamespace[] getNamespaces() throws CModelException {
+ ICElement[] celements = getChildren();
+ ArrayList aList = new ArrayList();
+ for (int i = 0; i < celements.length; i++) {
+ if (celements[i].getElementType() == ICElement.C_NAMESPACE) {
+ aList.add(celements[i]);
+ }
+ }
+ return (INamespace[])aList.toArray(new INamespace[0]);
+ }
+
public void setLocation(IPath loc) {
location = loc;
}