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 7ae6f0df7e7..91edcd5d214 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
@@ -93,7 +93,7 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
* unit itself at the given position, or if the given position is not
* within the source range of this translation unit.
*
- * @param position a source position inside the translation unit
+ * @param line a position inside the translation unit
* @return the innermost C element enclosing a given source position or null
* if none (excluding the translation unit).
* @exception CModelException if the translation unit does not exist or if an
@@ -101,6 +101,21 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
*/
ICElement getElementAtLine(int line) throws CModelException;
+ /**
+ * Returns the smallest element within this translation unit that
+ * includes the given source position (that is, a method, field, etc.), or
+ * null
if there is no element other than the translation
+ * unit itself at the given position, or if the given position is not
+ * within the source range of this translation unit.
+ *
+ * @param position a source position inside the translation unit
+ * @return the innermost C element enclosing a given source position or null
+ * if none (excluding the translation unit).
+ * @exception CModelException if the translation unit does not exist or if an
+ * exception occurs while accessing its corresponding resource
+ */
+ ICElement getElementAtOffset(int offset) throws CModelException;
+
ICElement getElement(String name) throws CModelException;
/**
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 66aedd0fe0d..5667cf28663 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
@@ -69,6 +69,19 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return null;
}
+ 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];
+ }
+ }
+ return null;
+ }
+
public ICElement getElement(String name ) {
ICElement[] celements = getChildren();
for (int i = 0; i < celements.length; i++) {
@@ -250,6 +263,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
* @see Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
+ if (!(o instanceof ITranslationUnit)) return false;
return super.equals(o) && !((ITranslationUnit)o).isWorkingCopy();
}