1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

New method

ICelement.getAncestor(int ancestorType);
Better implementation of ICElement.getElementAtOffset()
This commit is contained in:
Alain Magloire 2004-02-29 04:37:19 +00:00
parent 6f281e37e4
commit a6553e61d1
4 changed files with 77 additions and 11 deletions

View file

@ -1,3 +1,13 @@
2004-02-28 Alain Magloire
New method in ICElement
ICElement.getAncestor(int ancestorType);
Better implementation of ICElement.getElementAtOffset(int)
* model/org/eclipse/cdt/core/model/ICElement.java
* model/org/eclipse/cdt/internal/core/model/CElement.java
* model/org/eclipse/cdt/internal/core/model/TranslationUnit.java
2004-02-27 Hoda Amer
Fixed [Bug 53074] The CView to update with each reconcile
Added the ability for CView to update based on the translation unit working copy

View file

@ -217,6 +217,17 @@ public interface ICElement extends IAdaptable {
*/
boolean exists();
/**
* Returns the first ancestor of this C element that has the given type.
* Returns <code>null</code> if no such an ancestor can be found.
* This is a handle-only method.
*
* @param ancestorType the given type
* @return the first ancestor of this C element that has the given type, null if no such an ancestor can be found
* @since 2.0
*/
ICElement getAncestor(int ancestorType);
/**
* Returns the name of this element.
*

View file

@ -12,6 +12,8 @@ import org.eclipse.cdt.core.model.ICModelStatusConstants;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IOpenable;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
@ -79,8 +81,41 @@ public abstract class CElement extends PlatformObject implements ICElement {
public boolean exists() {
return getElementInfo() != null;
// return getResource() != null;
}
/**
* Returns the element that is located at the given source offset
* in this element. This is a helper method for <code>ITranslationUnit#getElementAtOffset</code>,
* and only works on compilation units and types. The offset given is
* known to be within this element's source range already, and if no finer
* grained element is found at the offset, this element is returned.
*/
protected ICElement getSourceElementAtOffset(int offset) throws CModelException {
if (this instanceof ISourceReference && this instanceof Parent) {
ICElement[] children = ((Parent)this).getChildren();
for (int i = 0; i < children.length; i++) {
ICElement aChild = children[i];
if (aChild instanceof ISourceReference) {
ISourceReference child = (ISourceReference) children[i];
ISourceRange range = child.getSourceRange();
int startPos = range.getStartPos();
int endPos = startPos + range.getLength();
if (offset < endPos && offset >= startPos) {
if (child instanceof Parent) {
return ((Parent)child).getSourceElementAtOffset(offset);
} else {
return (ICElement)child;
}
}
}
}
} else {
// should not happen
//Assert.isTrue(false);
}
return this;
}
public boolean isReadOnly () {
IResource r = getUnderlyingResource();
@ -328,7 +363,22 @@ public abstract class CElement extends PlatformObject implements ICElement {
//}
}
}
}
}
/**
* @see IJavaElement
*/
public ICElement getAncestor(int ancestorType) {
ICElement element = this;
while (element != null) {
if (element.getElementType() == ancestorType) {
return element;
}
element= element.getParent();
}
return null;
}
/**
* Returns true if this element is an ancestor of the given element,
* otherwise false.

View file

@ -70,16 +70,11 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
}
public ICElement getElementAtOffset(int pos) throws CModelException {
ICElement[] celements = getChildren();
for (int i = 0; i < celements.length; i++) {
ISourceRange range = ((ISourceReference)celements[i]).getSourceRange();
int startPos = range.getStartPos();
int endPos = startPos + range.getLength();
if (pos >= startPos && pos <= endPos) {
return celements[i];
}
ICElement e= getSourceElementAtOffset(pos);
if (e == this) {
return null;
}
return null;
return e;
}
public ICElement getElement(String name ) {