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

Created ICElement.getLocationURI() method. Changed ITranslationUnit concrete implementations to only be constructable from URIs.

This commit is contained in:
Chris Recoskie 2008-01-15 19:13:24 +00:00
parent 424485c971
commit f40c392f11
13 changed files with 95 additions and 25 deletions

View file

@ -12,6 +12,8 @@
package org.eclipse.cdt.core.model; package org.eclipse.cdt.core.model;
import java.net.URI;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IAdaptable;
@ -351,6 +353,14 @@ public interface ICElement extends IAdaptable {
*/ */
IPath getPath(); IPath getPath();
/**
* Returns an absolute URI corresponding to the innermost file enclosing this element.
*
* @since 5.0
* @return the URI corresponding to the location
*/
URI getLocationURI();
/** /**
* Returns the underlying resource that contains * Returns the underlying resource that contains
* this element, or <code>null</code> if this element is not contained * this element, or <code>null</code> if this element is not contained

View file

@ -16,6 +16,7 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -323,7 +324,7 @@ public class Binary extends Openable implements IBinary {
// See if this source file is already in the project. // See if this source file is already in the project.
// We check this to determine if we should create a TranslationUnit or ExternalTranslationUnit // We check this to determine if we should create a TranslationUnit or ExternalTranslationUnit
IFile sourceFile = getCProject().getProject().getFile(filename); IFile sourceFile = getCProject().getProject().getFile(filename);
IPath path = new Path(filename); URI uri = sourceFile.getLocationURI();
IFile wkspFile = null; IFile wkspFile = null;
if (sourceFile.exists()) if (sourceFile.exists())
@ -331,7 +332,7 @@ public class Binary extends Openable implements IBinary {
else { else {
IFile[] filesInWP = ResourcesPlugin IFile[] filesInWP = ResourcesPlugin
.getWorkspace().getRoot() .getWorkspace().getRoot()
.findFilesForLocation(path); .findFilesForLocationURI(uri);
for (int j = 0; j < filesInWP.length; j++) { for (int j = 0; j < filesInWP.length; j++) {
if (filesInWP[j].isAccessible()) { if (filesInWP[j].isAccessible()) {
@ -351,7 +352,7 @@ public class Binary extends Openable implements IBinary {
if (wkspFile != null) if (wkspFile != null)
tu = new TranslationUnit(this, wkspFile, id); tu = new TranslationUnit(this, wkspFile, id);
else else
tu = new ExternalTranslationUnit(this, path, id); tu = new ExternalTranslationUnit(this, uri, id);
if (! info.includesChild(tu)) if (! info.includesChild(tu))
info.addChild(tu); info.addChild(tu);

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -103,6 +104,18 @@ public abstract class CElement extends PlatformObject implements ICElement {
return new Path(getElementName()); return new Path(getElementName());
} }
public URI getLocationURI() {
IResource res = getUnderlyingResource();
if(res != null) {
return res.getLocationURI();
}
else {
return null;
}
}
public boolean exists() { public boolean exists() {
try { try {
return getElementInfo() != null; return getElementInfo() != null;

View file

@ -54,6 +54,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.CCoreInternals; import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.LocalProjectScope; import org.eclipse.cdt.internal.core.LocalProjectScope;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
@ -394,7 +395,9 @@ public class CModelManager implements IResourceChangeListener, ICDescriptorListe
if (headerContentTypeId == null) { if (headerContentTypeId == null) {
headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER; headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER;
} }
return new ExternalTranslationUnit(includeReferences[i], path, headerContentTypeId);
// TODO: use URI
return new ExternalTranslationUnit(includeReferences[i], URIUtil.toURI(path), headerContentTypeId);
} }
} }
} catch (CModelException e) { } catch (CModelException e) {
@ -403,7 +406,8 @@ public class CModelManager implements IResourceChangeListener, ICDescriptorListe
// if the file exists and it has a known C/C++ file extension then just create // if the file exists and it has a known C/C++ file extension then just create
// an external translation unit for it. // an external translation unit for it.
if (contentTypeId != null && path.toFile().exists()) { if (contentTypeId != null && path.toFile().exists()) {
return new ExternalTranslationUnit(cproject, path, contentTypeId); // TODO: use URI
return new ExternalTranslationUnit(cproject, URIUtil.toURI(path), contentTypeId);
} }
} else { } else {
// !path.isAbsolute() // !path.isAbsolute()
@ -416,7 +420,9 @@ public class CModelManager implements IResourceChangeListener, ICDescriptorListe
if (headerContentTypeId == null) { if (headerContentTypeId == null) {
headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER; headerContentTypeId= CoreModel.hasCCNature(project) ? CCorePlugin.CONTENT_TYPE_CXXHEADER : CCorePlugin.CONTENT_TYPE_CHEADER;
} }
return new ExternalTranslationUnit(includeReferences[i], includePath, headerContentTypeId);
// TODO: use URI
return new ExternalTranslationUnit(includeReferences[i], URIUtil.toURI(includePath), headerContentTypeId);
} }
} }
} catch (CModelException e) { } catch (CModelException e) {

View file

@ -47,7 +47,7 @@ public class CreateWorkingCopyOperation extends CModelOperation {
if (tu.getResource() != null) { if (tu.getResource() != null) {
workingCopy= new WorkingCopy(tu.getParent(), (IFile)tu.getResource(), tu.getContentTypeId(), this.factory, this.problemRequestor); workingCopy= new WorkingCopy(tu.getParent(), (IFile)tu.getResource(), tu.getContentTypeId(), this.factory, this.problemRequestor);
} else { } else {
workingCopy= new WorkingCopy(tu.getParent(), tu.getLocation(), tu.getContentTypeId(), this.factory); workingCopy= new WorkingCopy(tu.getParent(), tu.getLocationURI(), tu.getContentTypeId(), this.factory);
} }
// open the working copy now to ensure contents are that of the current state of this element // open the working copy now to ensure contents are that of the current state of this element
// Alain: Actually no, delay the parsing 'till it is really needed. Doing the parsing here // Alain: Actually no, delay the parsing 'till it is really needed. Doing the parsing here

View file

@ -12,6 +12,8 @@
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
import java.net.URI;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
@ -24,8 +26,8 @@ public class ExternalTranslationUnit extends TranslationUnit {
* @param parent * @param parent
* @param path * @param path
*/ */
public ExternalTranslationUnit(ICElement parent, IPath path, String contentTypeID) { public ExternalTranslationUnit(ICElement parent, URI uri, String contentTypeID) {
super(parent, path, contentTypeID); super(parent, uri, contentTypeID);
} }
public IPath getPath() { public IPath getPath() {

View file

@ -24,6 +24,7 @@ import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IIncludeEntry; import org.eclipse.cdt.core.model.IIncludeEntry;
import org.eclipse.cdt.core.model.IIncludeReference; import org.eclipse.cdt.core.model.IIncludeReference;
import org.eclipse.cdt.utils.PathUtil; import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
@ -113,7 +114,8 @@ public class IncludeReference extends Openable implements IIncludeReference {
} else if (child.isFile()){ } else if (child.isFile()){
String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), names[i]); String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), names[i]);
if (id != null) { if (id != null) {
celement = new ExternalTranslationUnit(this, path.append(names[i]), id); // TODO: should use URI
celement = new ExternalTranslationUnit(this, URIUtil.toURI(path.append(names[i])), id);
} }
} }
if (celement != null) { if (celement != null) {

View file

@ -17,6 +17,7 @@ package org.eclipse.cdt.internal.core.model;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -63,6 +64,10 @@ import org.eclipse.cdt.internal.core.dom.NullCodeReaderFactory;
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory; import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
import org.eclipse.cdt.internal.core.index.IndexBasedCodeReaderFactory; import org.eclipse.cdt.internal.core.index.IndexBasedCodeReaderFactory;
import org.eclipse.cdt.internal.core.pdom.indexer.ProjectIndexerInputAdapter; import org.eclipse.cdt.internal.core.pdom.indexer.ProjectIndexerInputAdapter;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
@ -77,7 +82,7 @@ import org.eclipse.core.runtime.content.IContentType;
*/ */
public class TranslationUnit extends Openable implements ITranslationUnit { public class TranslationUnit extends Openable implements ITranslationUnit {
private IPath location = null; private URI location = null;
private String contentTypeId; private String contentTypeId;
/** /**
@ -94,10 +99,9 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
setContentTypeID(idType); setContentTypeID(idType);
} }
public TranslationUnit(ICElement parent, IPath path, String idType) { public TranslationUnit(ICElement parent, URI uri, String idType) {
super(parent, (IResource)null, path.toString(), ICElement.C_UNIT); super(parent, (IResource)null, uri.toString(), ICElement.C_UNIT);
setContentTypeID(idType); setContentTypeID(idType);
setLocation(path);
} }
public ITranslationUnit getTranslationUnit() { public ITranslationUnit getTranslationUnit() {
@ -290,7 +294,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return (INamespace[]) aList.toArray(new INamespace[0]); return (INamespace[]) aList.toArray(new INamespace[0]);
} }
protected void setLocation(IPath loc) { protected void setLocationURI(URI loc) {
location = loc; location = loc;
} }
@ -298,9 +302,21 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (location == null) { if (location == null) {
IFile file = getFile(); IFile file = getFile();
if (file != null) { if (file != null) {
location = file.getLocation(); return file.getLocation();
} else { } else {
return getPath(); return null;
}
}
return URIUtil.toPath(location);
}
public URI getLocationURI() {
if (location == null) {
IFile file = getFile();
if (file != null) {
location = file.getLocationURI();
} else {
return null;
} }
} }
return location; return location;
@ -465,7 +481,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (file != null) { if (file != null) {
workingCopy= new WorkingCopy(getParent(), file, getContentTypeId(), factory); workingCopy= new WorkingCopy(getParent(), file, getContentTypeId(), factory);
} else { } else {
workingCopy= new WorkingCopy(getParent(), getLocation(), getContentTypeId(), factory); workingCopy= new WorkingCopy(getParent(), getLocationURI(), getContentTypeId(), factory);
} }
// open the working copy now to ensure contents are that of the current state of this element // open the working copy now to ensure contents are that of the current state of this element
workingCopy.open(monitor); workingCopy.open(monitor);
@ -678,7 +694,16 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (res != null) if (res != null)
return res.exists(); return res.exists();
if (location != null) { if (location != null) {
return location.toFile().exists(); try {
IFileStore fileStore = EFS.getStore(location);
IFileInfo info = fileStore.fetchInfo();
return info.exists();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
return false; return false;
} }
@ -909,4 +934,6 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
final ILanguage result= fLanguageOfContext; final ILanguage result= fLanguageOfContext;
return result != null ? result : getLanguage(); return result != null ? result : getLanguage();
} }
} }

View file

@ -15,6 +15,7 @@ package org.eclipse.cdt.internal.core.model;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -69,8 +70,8 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
problemRequestor = requestor; problemRequestor = requestor;
} }
public WorkingCopy(ICElement parent, IPath path, String id, IBufferFactory bufferFactory) { public WorkingCopy(ICElement parent, URI uri, String id, IBufferFactory bufferFactory) {
super(parent, path, id); super(parent, uri, id);
this.bufferFactory = this.bufferFactory =
bufferFactory == null ? bufferFactory == null ?
getBufferManager() : getBufferManager() :
@ -224,7 +225,7 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
if (file != null) { if (file != null) {
return new TranslationUnit(getParent(), getFile(), getContentTypeId()); return new TranslationUnit(getParent(), getFile(), getContentTypeId());
} }
return new ExternalTranslationUnit(getParent(), getLocation(), getContentTypeId()); return new ExternalTranslationUnit(getParent(), getLocationURI(), getContentTypeId());
} }
/** /**

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.internal.core.model.ext; package org.eclipse.cdt.internal.core.model.ext;
import java.net.URI;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -150,6 +151,10 @@ abstract class CElementHandle implements ICElementHandle, ISourceReference {
return getTranslationUnit().getPath(); return getTranslationUnit().getPath();
} }
public URI getLocationURI() {
return getTranslationUnit().getLocationURI();
}
public IResource getResource() { public IResource getResource() {
return getTranslationUnit().getResource(); return getTranslationUnit().getResource();
} }

View file

@ -112,7 +112,7 @@ public class BasicCEditorTest extends BaseUITestCase {
} }
private void setUpEditor(File file) throws PartInitException, CModelException { private void setUpEditor(File file) throws PartInitException, CModelException {
IEditorPart editor= EditorUtility.openInEditor(new ExternalTranslationUnit(fCProject, Path.fromOSString(file.toString()), CCorePlugin.CONTENT_TYPE_CXXSOURCE)); IEditorPart editor= EditorUtility.openInEditor(new ExternalTranslationUnit(fCProject, file.toURI(), CCorePlugin.CONTENT_TYPE_CXXSOURCE));
assertNotNull(editor); assertNotNull(editor);
assertTrue(editor instanceof CEditor); assertTrue(editor instanceof CEditor);
fEditor= (CEditor) editor; fEditor= (CEditor) editor;

View file

@ -55,7 +55,7 @@ public class AbsolutePathSourceContainer extends AbstractSourceContainer {
{ {
IPath path = Path.fromOSString(file.getCanonicalPath()); IPath path = Path.fromOSString(file.getCanonicalPath());
String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment()); String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment());
return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, new Path(file.getCanonicalPath()), id) }; return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, file.toURI(), id) };
} }
} }
} catch (IOException e) { // ignore if getCanonicalPath throws } catch (IOException e) { // ignore if getCanonicalPath throws

View file

@ -28,6 +28,7 @@ import org.eclipse.cdt.debug.ui.ICDebugUIConstants;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit; import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.ui.util.EditorUtility; import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
@ -328,7 +329,9 @@ public class CSourceNotFoundEditor extends CommonSourceNotFoundEditor {
{ {
ITranslationUnit remappedTU = tu; ITranslationUnit remappedTU = tu;
if (tu instanceof ExternalTranslationUnit) if (tu instanceof ExternalTranslationUnit)
remappedTU = new ExternalTranslationUnit(tu.getParent(), newLocation.getFullPath(), tu.getContentTypeId());
// TODO: source lookup needs to be modified to use URIs
remappedTU = new ExternalTranslationUnit(tu.getParent(), URIUtil.toURI(newLocation.getFullPath()), tu.getContentTypeId());
EditorUtility.openInEditor(remappedTU); EditorUtility.openInEditor(remappedTU);
return true; return true;
} }