1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Improves handling of multiple IFile's for a location, bug 211056.

This commit is contained in:
Markus Schorn 2008-05-28 13:37:44 +00:00
parent d4f3962b40
commit c377be5b0e

View file

@ -7,18 +7,24 @@
* *
* Contributors: * Contributors:
* Andrew Ferguson (Symbian) - initial API and implementation * Andrew Ferguson (Symbian) - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.index; package org.eclipse.cdt.core.index;
import java.net.URI; import java.net.URI;
import java.util.Arrays;
import java.util.Comparator;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.index.IndexFileLocation; import org.eclipse.cdt.internal.core.index.IndexFileLocation;
import org.eclipse.core.filesystem.URIUtil; 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.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
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;
@ -35,6 +41,25 @@ import org.eclipse.core.runtime.Path;
* @since 4.0 * @since 4.0
*/ */
public class IndexLocationFactory { public class IndexLocationFactory {
/**
* Comparator to sort files for location.
*/
private static final class FILE_COMPARATOR implements Comparator<IFile> {
public int compare(IFile o1, IFile o2) {
return compare(o1.getLocationURI(), o2.getLocationURI());
}
private int compare(URI uri1, URI uri2) {
if (uri1 == uri2)
return 0;
if (uri1 == null)
return -1;
if (uri2 == null)
return 1;
return uri1.toString().compareTo(uri2.toString());
}
}
/** /**
* Returns * Returns
* <ul> * <ul>
@ -89,16 +114,36 @@ public class IndexLocationFactory {
*/ */
public static IIndexFileLocation getIFLExpensive(ICProject cproject, String absolutePath) { public static IIndexFileLocation getIFLExpensive(ICProject cproject, String absolutePath) {
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(absolutePath)); IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(absolutePath));
if(files.length==1) { switch(files.length) {
case 0:
return getExternalIFL(absolutePath);
case 1:
return getWorkspaceIFL(files[0]); return getWorkspaceIFL(files[0]);
} }
if(cproject!=null) {
for(int i=0; i<files.length; i++) { Arrays.sort(files, new FILE_COMPARATOR());
if(files[i].getProject().equals(cproject.getProject())) final IProject preferredProject= cproject == null ? null : cproject.getProject();
return getWorkspaceIFL(files[i]); IFile fileInCProject= null;
for (IFile file : files) {
// check for preferred project
final IProject project = file.getProject();
if (preferredProject != null && preferredProject.equals(project))
return getWorkspaceIFL(file);
if (fileInCProject == null) {
try {
if (project.hasNature(CProjectNature.C_NATURE_ID)) {
fileInCProject= file;
}
} catch (CoreException e) {
// treat as non-c project
} }
} }
return new IndexFileLocation(URIUtil.toURI(absolutePath), null); }
if (fileInCProject != null)
return getWorkspaceIFL(fileInCProject);
return getWorkspaceIFL(files[0]);
} }
/** /**