From bff7d6fe56beac8755686e5d7e9c76a2b8791b64 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Thu, 2 Sep 2004 01:00:25 +0000 Subject: [PATCH] 2004-09-01 Alain Magloire Provide IWorkingCopy.getOriginal(ICElement) * model/org/eclipse/cdt/core/model/IWorkingCopy.java * model/org/eclipse/cdt/internal/core/model/WorkinCopy.java --- core/org.eclipse.cdt.core/ChangeLog | 6 ++ .../eclipse/cdt/core/model/IWorkingCopy.java | 13 ++++- .../cdt/internal/core/model/WorkingCopy.java | 56 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 2c8ba0a12e5..e0f7fc44bb2 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,9 @@ +2004-09-01 Alain Magloire + + Provide IWorkingCopy.getOriginal(ICElement) + * model/org/eclipse/cdt/core/model/IWorkingCopy.java + * model/org/eclipse/cdt/internal/core/model/WorkinCopy.java + 2004-08-31 Alain Magloire Fix for 72198 diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/IWorkingCopy.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/IWorkingCopy.java index d9ec6324622..3e13ac9497f 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/IWorkingCopy.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/IWorkingCopy.java @@ -75,9 +75,18 @@ public interface IWorkingCopy extends ITranslationUnit{ * getSharedWorkingCopy(IProgressMonitor, IBufferFactory). * A REMOVED CElementDelta is then reported on this working copy. */ - void destroy(); - + + /** + * Returns the original element the specified working copy element was created from, + * or null if this is not a working copy element. + * + * @param workingCopyElement the specified working copy element + * @return the original element the specified working copy element was created from, + * or null if this is not a working copy element + */ + ICElement getOriginal(ICElement workingCopyElement); + /** * Returns the original element this working copy was created from, * or null if this is not a working copy. diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/WorkingCopy.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/WorkingCopy.java index 69e6ef62b01..a976bf59f3c 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/WorkingCopy.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/WorkingCopy.java @@ -13,12 +13,15 @@ package org.eclipse.cdt.internal.core.model; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.ArrayList; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.IBuffer; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICModelStatusConstants; +import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.IProblemRequestor; +import org.eclipse.cdt.core.model.ISourceReference; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.core.resources.IFile; @@ -162,6 +165,59 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy { return this == o; } + /** + * Returns the original element the specified working copy element was created from, + * or null if this is not a working copy element. + * + * @param workingCopyElement the specified working copy element + * @return the original element the specified working copy element was created from, + * or null if this is not a working copy element + */ + public ICElement getOriginal(ICElement workingCopyElement) { + // It has to come from the same workingCopy, meaning ours. + if (workingCopyElement instanceof ISourceReference) { + ITranslationUnit wunit = ((ISourceReference)workingCopyElement).getTranslationUnit(); + if (!wunit.equals(this)) { + return null; + } + } else { + return null; + } + ITranslationUnit tu = getOriginalElement(); + if (tu == null) { + return null; // oops !! + } + + // look for it. + ICElement element = workingCopyElement; + ArrayList children = new ArrayList(); + while (element != null && element.getElementType() != ICElement.C_UNIT) { + children.add(element); + element = element.getParent(); + } + ICElement current = tu; + for (int i = children.size()-1; i >= 0; i--) { + ICElement child = (ICElement)children.get(i); + if (current instanceof IParent) { + try { + ICElement[] celems = ((IParent)current).getChildren(); + current = null; + for (int j = 0; j < celems.length; ++j) { + if (celems[j].getElementName().equals(child.getElementName()) && + celems[j].getElementType() == child.getElementType()) { + current = celems[j]; + break; + } + } + } catch (CModelException e) { + current = null; + } + } + } + return current; + } + + /** * @see org.eclipse.cdt.core.model.IWorkingCopy#getOriginalElement() */