1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 17:35:35 +02:00

Fix for PR 66108

This commit is contained in:
Alain Magloire 2004-06-19 03:15:56 +00:00
parent 1972c2b70a
commit 9ce85f46de
4 changed files with 67 additions and 0 deletions

View file

@ -1,3 +1,7 @@
2004-06-18 Alain Magloire
Fix for PR 66108
2004-06-18 Alain Magloire
This was heavy and lots of files were change. The problem: to create the CElementInfo we use

View file

@ -116,6 +116,22 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
*/
ICElement getElementAtOffset(int offset) throws CModelException;
/**
* Returns the elements within this translation unit that
* includes the given source position (that is, a method, field, etc.), or
* an empty array if there are no elements 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.
* You have this behavior when at expansion of a macro.
*
* @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[] getElementsAtOffset(int offset) throws CModelException;
ICElement getElement(String name) throws CModelException;
/**

View file

@ -4,6 +4,8 @@ package org.eclipse.cdt.internal.core.model;
* All Rights Reserved.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -125,6 +127,43 @@ public abstract class CElement extends PlatformObject implements ICElement {
return this;
}
/**
* Returns the elements that are 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[] getSourceElementsAtOffset(int offset) throws CModelException {
if (this instanceof ISourceReference && this instanceof Parent) {
ArrayList list = new ArrayList();
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) {
ICElement[] elements = ((Parent)child).getSourceElementsAtOffset(offset);
list.addAll(Arrays.asList(elements));
} else {
list.add(child);
}
}
}
}
children = new ICElement[list.size()];
list.toArray(children);
return children;
} else {
// should not happen
//Assert.isTrue(false);
}
return new ICElement[]{this};
}
public boolean isReadOnly () {
IResource r = getUnderlyingResource();

View file

@ -88,6 +88,14 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return e;
}
public ICElement[] getElementsAtOffset(int pos) throws CModelException {
ICElement[] e= getSourceElementsAtOffset(pos);
if (e.length == 1 && e[0] == this) {
return CElement.NO_ELEMENTS;
}
return e;
}
public ICElement getElement(String name ) {
try {
ICElement[] celements = getChildren();