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

new method getElementAtOffset()

This commit is contained in:
Alain Magloire 2004-02-16 19:33:13 +00:00
parent c4af8be231
commit 61416ad865
2 changed files with 30 additions and 1 deletions

View file

@ -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 <code>null</code>
* 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
* <code>null</code> 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 <code>null</code>
* 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;
/**

View file

@ -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();
}