1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +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;
import java.net.URI;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
@ -350,6 +352,14 @@ public interface ICElement extends IAdaptable {
*
*/
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

View file

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

View file

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

View file

@ -54,6 +54,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.LocalProjectScope;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
@ -394,7 +395,9 @@ public class CModelManager implements IResourceChangeListener, ICDescriptorListe
if (headerContentTypeId == null) {
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) {
@ -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
// an external translation unit for it.
if (contentTypeId != null && path.toFile().exists()) {
return new ExternalTranslationUnit(cproject, path, contentTypeId);
// TODO: use URI
return new ExternalTranslationUnit(cproject, URIUtil.toURI(path), contentTypeId);
}
} else {
// !path.isAbsolute()
@ -416,7 +420,9 @@ public class CModelManager implements IResourceChangeListener, ICDescriptorListe
if (headerContentTypeId == null) {
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) {

View file

@ -47,7 +47,7 @@ public class CreateWorkingCopyOperation extends CModelOperation {
if (tu.getResource() != null) {
workingCopy= new WorkingCopy(tu.getParent(), (IFile)tu.getResource(), tu.getContentTypeId(), this.factory, this.problemRequestor);
} 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
// 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;
import java.net.URI;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.runtime.IPath;
@ -24,8 +26,8 @@ public class ExternalTranslationUnit extends TranslationUnit {
* @param parent
* @param path
*/
public ExternalTranslationUnit(ICElement parent, IPath path, String contentTypeID) {
super(parent, path, contentTypeID);
public ExternalTranslationUnit(ICElement parent, URI uri, String contentTypeID) {
super(parent, uri, contentTypeID);
}
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.IIncludeReference;
import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
@ -113,7 +114,8 @@ public class IncludeReference extends Openable implements IIncludeReference {
} else if (child.isFile()){
String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), names[i]);
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) {

View file

@ -17,6 +17,7 @@ package org.eclipse.cdt.internal.core.model;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
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.index.IndexBasedCodeReaderFactory;
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.IProject;
import org.eclipse.core.resources.IResource;
@ -77,7 +82,7 @@ import org.eclipse.core.runtime.content.IContentType;
*/
public class TranslationUnit extends Openable implements ITranslationUnit {
private IPath location = null;
private URI location = null;
private String contentTypeId;
/**
@ -94,10 +99,9 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
setContentTypeID(idType);
}
public TranslationUnit(ICElement parent, IPath path, String idType) {
super(parent, (IResource)null, path.toString(), ICElement.C_UNIT);
public TranslationUnit(ICElement parent, URI uri, String idType) {
super(parent, (IResource)null, uri.toString(), ICElement.C_UNIT);
setContentTypeID(idType);
setLocation(path);
}
public ITranslationUnit getTranslationUnit() {
@ -290,7 +294,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return (INamespace[]) aList.toArray(new INamespace[0]);
}
protected void setLocation(IPath loc) {
protected void setLocationURI(URI loc) {
location = loc;
}
@ -298,9 +302,21 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (location == null) {
IFile file = getFile();
if (file != null) {
location = file.getLocation();
return file.getLocation();
} 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;
@ -465,7 +481,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (file != null) {
workingCopy= new WorkingCopy(getParent(), file, getContentTypeId(), factory);
} 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
workingCopy.open(monitor);
@ -678,7 +694,16 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
if (res != null)
return res.exists();
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;
}
@ -909,4 +934,6 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
final ILanguage result= fLanguageOfContext;
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.IOException;
import java.net.URI;
import java.util.ArrayList;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -69,8 +70,8 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
problemRequestor = requestor;
}
public WorkingCopy(ICElement parent, IPath path, String id, IBufferFactory bufferFactory) {
super(parent, path, id);
public WorkingCopy(ICElement parent, URI uri, String id, IBufferFactory bufferFactory) {
super(parent, uri, id);
this.bufferFactory =
bufferFactory == null ?
getBufferManager() :
@ -224,7 +225,7 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
if (file != null) {
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;
import java.net.URI;
import java.util.Collections;
import java.util.List;
@ -150,6 +151,10 @@ abstract class CElementHandle implements ICElementHandle, ISourceReference {
return getTranslationUnit().getPath();
}
public URI getLocationURI() {
return getTranslationUnit().getLocationURI();
}
public IResource getResource() {
return getTranslationUnit().getResource();
}

View file

@ -112,7 +112,7 @@ public class BasicCEditorTest extends BaseUITestCase {
}
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);
assertTrue(editor instanceof CEditor);
fEditor= (CEditor) editor;

View file

@ -55,7 +55,7 @@ public class AbsolutePathSourceContainer extends AbstractSourceContainer {
{
IPath path = Path.fromOSString(file.getCanonicalPath());
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

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.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
@ -328,7 +329,9 @@ public class CSourceNotFoundEditor extends CommonSourceNotFoundEditor {
{
ITranslationUnit remappedTU = tu;
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);
return true;
}