mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 10:16:03 +02:00
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
This commit is contained in:
parent
4e08e102f7
commit
027b23c9e3
6 changed files with 306 additions and 16 deletions
|
@ -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
|
||||
|
|
|
@ -45,16 +45,13 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
|
|||
* <li> The name is not a valid import name (INVALID_NAME)
|
||||
* </ul>
|
||||
*/
|
||||
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
|
||||
*
|
||||
* <p>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, <code>"std"</code>)
|
||||
* @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
|
|||
* <li> The name is not a valid package name (INVALID_NAME)
|
||||
* </ul>
|
||||
*/
|
||||
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:
|
||||
* <ul>
|
||||
* <li>This C element does not exist (ELEMENT_DOES_NOT_EXIST)</li>
|
||||
* <li> A <code>CoreException</code> occurred while updating an underlying resource
|
||||
* <li> The name is not a valid package name (INVALID_NAME)
|
||||
* </ul>
|
||||
*/
|
||||
INamespace createNamespace (String namespace, ICElement sibling, IProgressMonitor monitor) throws CModelException;
|
||||
|
||||
/**
|
||||
* Finds the shared working copy for this element, given a <code>IBuffer</code> 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, <code>"std"</code>)
|
||||
*/
|
||||
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, <code>"std"</code>)
|
||||
*/
|
||||
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.
|
||||
|
|
|
@ -72,9 +72,8 @@ public class CreateIncludeOperation extends CreateElementInTUOperation {
|
|||
/**
|
||||
* Sets the correct position for the new include:<ul>
|
||||
* <li> after the last include
|
||||
* <li> if no include, before the first type
|
||||
* <li> if no type, after the package statement
|
||||
* <li> and if no package statement - first thing in the CU
|
||||
* </ul>
|
||||
* 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$;
|
||||
|
|
|
@ -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:<ul>
|
||||
* <li> after the last namespace
|
||||
* </ul>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>This operation adds a using declaration/directive to an existing translation unit.
|
||||
*
|
||||
* <p>Required Attributes:<ul>
|
||||
* <li>Translation unit
|
||||
* <li>using name - the name of the using to add
|
||||
* </ul>
|
||||
*/
|
||||
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:<ul>
|
||||
* <li> after the last using
|
||||
* </ul>
|
||||
* 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue