From 9ce85f46dea706d2100296514892a583c0651743 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Sat, 19 Jun 2004 03:15:56 +0000 Subject: [PATCH] Fix for PR 66108 --- core/org.eclipse.cdt.core/ChangeLog | 4 ++ .../cdt/core/model/ITranslationUnit.java | 16 ++++++++ .../cdt/internal/core/model/CElement.java | 39 +++++++++++++++++++ .../internal/core/model/TranslationUnit.java | 8 ++++ 4 files changed, 67 insertions(+) diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index ae0d652fd7f..ccf421793ac 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -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 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 0642e0e65a2..24698fa3db7 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 @@ -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 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[] getElementsAtOffset(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/CElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java index 1bc0e262d71..752fc3f075f 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 @@ -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 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[] 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(); 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 8cf8c13e480..476381c821c 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 @@ -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();