Search in sources :

Example 1 with ISourceRoot

use of org.eclipse.cdt.core.model.ISourceRoot in project linuxtools by eclipse.

the class STLink2SourceSupport method getFileForPathImpl.

private static IFile getFileForPathImpl(IPath path, IProject project) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    if (path.isAbsolute()) {
        return root.getFileForLocation(path);
    }
    if (project != null && project.exists()) {
        ICProject cproject = CoreModel.getDefault().create(project);
        if (cproject != null) {
            try {
                ISourceRoot[] roots = cproject.getAllSourceRoots();
                for (ISourceRoot sourceRoot : roots) {
                    IContainer r = sourceRoot.getResource();
                    IResource res = r.findMember(path);
                    if (res != null && res.exists() && res instanceof IFile) {
                        return (IFile) res;
                    }
                }
                IOutputEntry[] entries = cproject.getOutputEntries();
                for (IOutputEntry pathEntry : entries) {
                    IPath p = pathEntry.getPath();
                    IResource r = root.findMember(p);
                    if (r instanceof IContainer) {
                        IContainer parent = (IContainer) r;
                        IResource res = parent.findMember(path);
                        if (res != null && res.exists() && res instanceof IFile) {
                            return (IFile) res;
                        }
                    }
                }
            } catch (CModelException e) {
                Activator.getDefault().getLog().log(e.getStatus());
            }
        }
    }
    // no match found...try and see if we are dealing with a link
    IPath realPath = project.getLocation().append(path).makeAbsolute();
    URI realURI = URIUtil.toURI(realPath.toString());
    try {
        FindLinkedResourceVisitor visitor = new STLink2SourceSupport.FindLinkedResourceVisitor(realURI);
        project.accept(visitor, IResource.DEPTH_INFINITE);
        // If we find a match, make note of the target and the real C project.
        if (visitor.foundElement()) {
            return (IFile) visitor.getResource();
        }
    } catch (CoreException e) {
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) ICProject(org.eclipse.cdt.core.model.ICProject) IPath(org.eclipse.core.runtime.IPath) CModelException(org.eclipse.cdt.core.model.CModelException) URI(java.net.URI) ISourceRoot(org.eclipse.cdt.core.model.ISourceRoot) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IOutputEntry(org.eclipse.cdt.core.model.IOutputEntry) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Aggregations

URI (java.net.URI)1 CModelException (org.eclipse.cdt.core.model.CModelException)1 ICProject (org.eclipse.cdt.core.model.ICProject)1 IOutputEntry (org.eclipse.cdt.core.model.IOutputEntry)1 ISourceRoot (org.eclipse.cdt.core.model.ISourceRoot)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1