diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog
index 0bac687fea5..9a825bb600f 100644
--- a/core/org.eclipse.cdt.core/ChangeLog
+++ b/core/org.eclipse.cdt.core/ChangeLog
@@ -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
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java
index 4b77d035e70..614c17173dc 100644
--- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java
@@ -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 null
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.
*
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java
index 8fcac502e85..4445e75eb19 100644
--- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java
+++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java
@@ -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 ITranslationUnit#getElementAtOffset
,
+ * 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.
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 5667cf28663..73c6dfd25e3 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
@@ -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 ) {