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

Bug 225805 - Make compilation directory container return IFiles when possible.

This commit is contained in:
Sergey Prigogin 2010-12-15 02:23:32 +00:00
parent a6387c02ef
commit 686dacabdb
3 changed files with 83 additions and 93 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2009 Nokia and others. * Copyright (c) 2006, 2010 Nokia and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,29 +8,17 @@
* Contributors: * Contributors:
* Nokia - Initial implementation (159833) * Nokia - Initial implementation (159833)
* Broadcom - http://bugs.eclipse.org/247853 * Broadcom - http://bugs.eclipse.org/247853
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.debug.core.sourcelookup; package org.eclipse.cdt.debug.core.sourcelookup;
import java.io.File; import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; import org.eclipse.cdt.debug.internal.core.sourcelookup.SourceUtils;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType; import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer; import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
public class AbsolutePathSourceContainer extends AbstractSourceContainer { public class AbsolutePathSourceContainer extends AbstractSourceContainer {
/** /**
@ -39,67 +27,6 @@ public class AbsolutePathSourceContainer extends AbstractSourceContainer {
*/ */
public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.absolutePath"; //$NON-NLS-1$ public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.absolutePath"; //$NON-NLS-1$
private Object[] findSourceElementByFile(File file) {
IFile[] wfiles = ResourceLookup.findFilesForLocation(new Path(file.getAbsolutePath()));
if (wfiles.length > 0) {
ResourceLookup.sortFilesByRelevance(wfiles, getProject());
return wfiles;
}
try {
// Check the canonical path as well to support case insensitive file
// systems like Windows.
wfiles = ResourceLookup.findFilesForLocation(new Path(file.getCanonicalPath()));
if (wfiles.length > 0) {
ResourceLookup.sortFilesByRelevance(wfiles, getProject());
return wfiles;
}
// The file is not already in the workspace so try to create an external translation unit for it.
ISourceLookupDirector director = getDirector();
if (director != null) {
ILaunchConfiguration launchConfiguration = director.getLaunchConfiguration();
if (launchConfiguration != null) {
String projectName = launchConfiguration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
if (projectName.length() > 0) {
ICProject project = CoreModel.getDefault().getCModel().getCProject(projectName);
if (project != null) {
IPath path = Path.fromOSString(file.getCanonicalPath());
String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment());
return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, path, id) };
}
}
}
}
} catch (IOException e) { // ignore if getCanonicalPath throws
} catch (CoreException e) {
}
// If we can't create an ETU then fall back on LocalFileStorage.
return new LocalFileStorage[] { new LocalFileStorage(file) };
}
/**
* Find the project associated with the current launch configuration
* @return IProject or null
*/
private IProject getProject() {
ISourceLookupDirector director = getDirector();
if (director != null) {
ILaunchConfiguration config = director.getLaunchConfiguration();
if (config != null) {
try {
String name = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
if (name.length() > 0)
return ResourcesPlugin.getWorkspace().getRoot().getProject(name);
} catch (CoreException e) {
// Don't care carry on search using other heuristics
}
}
}
return null;
}
public boolean isValidAbsoluteFilePath(String name) { public boolean isValidAbsoluteFilePath(String name) {
return isValidAbsoluteFilePath(new File(name)); return isValidAbsoluteFilePath(new File(name));
} }
@ -112,7 +39,7 @@ public class AbsolutePathSourceContainer extends AbstractSourceContainer {
if (name != null) { if (name != null) {
File file = new File(name); File file = new File(name);
if (isValidAbsoluteFilePath(file)) { if (isValidAbsoluteFilePath(file)) {
return findSourceElementByFile(file); return SourceUtils.findSourceElements(file, getDirector());
} }
} }
return new Object[0]; return new Object[0];

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.debug.internal.core.sourcelookup;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
@ -22,7 +23,6 @@ import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.sourcelookup.ISourceContainer; import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType; import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer; import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
/** /**
* A directory in the local file system that is used for running the C/C++ compiler. This container * A directory in the local file system that is used for running the C/C++ compiler. This container
@ -33,7 +33,7 @@ import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
* the source container list. * the source container list.
* *
* Source elements returned from <code>findSourceElements(...)</code> are instances of * Source elements returned from <code>findSourceElements(...)</code> are instances of
* <code>LocalFileStorage</code>. * <code>IFile</code> or <code>LocalFileStorage</code>.
* <p> * <p>
* Clients may instantiate this class. * Clients may instantiate this class.
* </p> * </p>
@ -104,30 +104,30 @@ public class CompilationDirectorySourceContainer extends CompositeSourceContaine
} }
/** /**
* Source elements returned from this method are instances of <code>LocalFileStorage</code>. * Source elements returned from this method are instances of <code>IFile</code> or <code>LocalFileStorage</code>.
* @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(String) * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(String)
*/ */
public Object[] findSourceElements(String name) throws CoreException { public Object[] findSourceElements(String name) throws CoreException {
ArrayList<Object> sources = new ArrayList<Object>(); File file = new File(fDirectory, name);
File directory = getDirectory(); List<Object> sources;
File file = new File(directory, name);
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
sources.add(new LocalFileStorage(file)); sources = Arrays.asList(SourceUtils.findSourceElements(file, getDirector()));
} else {
sources = new ArrayList<Object>();
} }
// Check sub-folders // Check sub-folders
if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) { if (fSubfolders && (isFindDuplicates() || sources.isEmpty())) {
ISourceContainer[] containers = getSourceContainers(); for (ISourceContainer container : getSourceContainers()) {
for (int i = 0; i < containers.length; i++) { Object[] elements = container.findSourceElements(name);
Object[] objects = containers[i].findSourceElements(name); if (elements == null || elements.length == 0) {
if (objects == null || objects.length == 0) {
continue; continue;
} }
if (isFindDuplicates()) { if (isFindDuplicates()) {
for (int j = 0; j < objects.length; j++) for (Object element : elements)
sources.add(objects[j]); sources.add(element);
} else { } else {
sources.add(objects[0]); sources.add(elements[0]);
break; break;
} }
} }

View file

@ -11,6 +11,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.sourcelookup; package org.eclipse.cdt.debug.internal.core.sourcelookup;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import com.ibm.icu.text.MessageFormat; import com.ibm.icu.text.MessageFormat;
@ -24,6 +25,9 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.CDebugUtils; import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
@ -33,16 +37,21 @@ import org.eclipse.cdt.debug.core.sourcelookup.IDirectorySourceLocation;
import org.eclipse.cdt.debug.core.sourcelookup.IMappingSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.IMappingSourceContainer;
import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation; import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation;
import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
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.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainer; import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer; import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
@ -217,17 +226,71 @@ public class SourceUtils {
} }
} }
/**
* Returns the project from the launch configuration, or {@code null} if it's not available.
*/
public static IProject getLaunchConfigurationProject(ISourceLookupDirector director) { public static IProject getLaunchConfigurationProject(ISourceLookupDirector director) {
String name = getLaunchConfigurationProjectName(director);
return name != null ? ResourcesPlugin.getWorkspace().getRoot().getProject(name) : null;
}
/**
* Returns the project name from the launch configuration, or {@code null} if it's not available.
*/
public static String getLaunchConfigurationProjectName(ISourceLookupDirector director) {
ILaunchConfiguration config = director.getLaunchConfiguration(); ILaunchConfiguration config = director.getLaunchConfiguration();
if (config != null) { if (config != null) {
try { try {
String name = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$ String name = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
if (name.length() > 0) if (name.length() > 0)
return ResourcesPlugin.getWorkspace().getRoot().getProject(name); return name;
} catch (CoreException e) { } catch (CoreException e) {
CDebugCorePlugin.log(e); CDebugCorePlugin.log(e);
} }
} }
return null; return null;
} }
/**
* Returns source elements corresponding to a file.
* @param file A source or header file.
* @param director A source lookup director.
* @return An array of source elements sorted in relevance order. The elements of the array can
* be either instances of IFile or LocalFileStorage. The returned array can be empty if
* no source elements match the given file.
*/
public static Object[] findSourceElements(File file, ISourceLookupDirector director) {
IFile[] wfiles = ResourceLookup.findFilesForLocation(new Path(file.getAbsolutePath()));
if (wfiles.length > 0) {
ResourceLookup.sortFilesByRelevance(wfiles, getLaunchConfigurationProject(director));
return wfiles;
}
try {
// Check the canonical path as well to support case insensitive file
// systems like Windows.
wfiles = ResourceLookup.findFilesForLocation(new Path(file.getCanonicalPath()));
if (wfiles.length > 0) {
ResourceLookup.sortFilesByRelevance(wfiles, getLaunchConfigurationProject(director));
return wfiles;
}
// The file is not already in the workspace so try to create an external translation unit for it.
if (director != null) {
String projectName = getLaunchConfigurationProjectName(director);
if (projectName != null) {
ICProject project = CoreModel.getDefault().getCModel().getCProject(projectName);
if (project != null) {
IPath path = Path.fromOSString(file.getCanonicalPath());
String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment());
return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, path, id) };
}
}
}
} catch (IOException e) { // ignore if getCanonicalPath throws
}
// If we can't create an ETU then fall back on LocalFileStorage.
return new LocalFileStorage[] { new LocalFileStorage(file) };
}
} }