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

Improving the source search algorithms.

This commit is contained in:
Mikhail Khodjaiants 2003-10-15 17:44:06 +00:00
parent 90ef3ef63b
commit eff499d538
4 changed files with 80 additions and 181 deletions

View file

@ -1,3 +1,9 @@
2003-10-15 Mikhail Khodjaiants
Improving the source search algorithms.
* CDirectorySourceLocation.java
* CProjectSourceLocation.java
* CSourceLocator.java
2003-10-14 Mikhail Khodjaiants 2003-10-14 Mikhail Khodjaiants
Improved the source search algorithm. Improved the source search algorithm.
* CProjectSourceLocation.java * CProjectSourceLocation.java

View file

@ -9,6 +9,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.LinkedList;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -177,11 +178,16 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation
} }
// Try for a file in another workspace project // Try for a file in another workspace project
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( filePath ); IFile[] wsFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation( filePath );
if ( f != null && f.exists() ) LinkedList list = new LinkedList();
{ for ( int j = 0; j < wsFiles.length; ++j )
return f; if ( wsFiles[j].exists() )
} if ( !searchForDuplicateFileNames() )
return wsFiles[j];
else
list.add( wsFiles[j] );
if ( list.size() > 0 )
return list;
file = filePath.toFile(); file = filePath.toFile();
if ( file.exists() ) if ( file.exists() )
@ -201,12 +207,18 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation
if ( file.exists() ) if ( file.exists() )
{ {
path = new Path( file.getAbsolutePath() ); // can't use getCanonicalPath because of links path = new Path( file.getAbsolutePath() ); // can't use getCanonicalPath because of links
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( path ); IFile[] wsFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation( path );
if ( f != null && f.exists() ) LinkedList list = new LinkedList();
{ for ( int j = 0; j < wsFiles.length; ++j )
return f; if ( wsFiles[j].exists() )
} if ( !searchForDuplicateFileNames() )
return createExternalFileStorage( path ); return wsFiles[j];
else
list.add( wsFiles[j] );
if ( list.size() > 0 )
return list;
else
return createExternalFileStorage( path );
} }
} }
return null; return null;
@ -355,4 +367,10 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation
String pathString = path.removeLastSegments( path.segmentCount() - segCount ).toOSString(); String pathString = path.removeLastSegments( path.segmentCount() - segCount ).toOSString();
return prefixString.equalsIgnoreCase( pathString ); return prefixString.equalsIgnoreCase( pathString );
} }
protected boolean searchForDuplicateFileNames()
{
// for now
return false;
}
} }

View file

@ -22,6 +22,7 @@ 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.sourcelookup.ICSourceLocation; import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocation;
import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation; import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation;
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;
import org.eclipse.core.resources.IResourceProxy; import org.eclipse.core.resources.IResourceProxy;
@ -76,7 +77,6 @@ public class CProjectSourceLocation implements IProjectSourceLocation
{ {
setProject( project ); setProject( project );
fGenerated = true; fGenerated = true;
// initializeFolders();
} }
/** /**
@ -86,7 +86,6 @@ public class CProjectSourceLocation implements IProjectSourceLocation
{ {
setProject( project ); setProject( project );
fGenerated = generated; fGenerated = generated;
// initializeFolders();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -151,24 +150,22 @@ public class CProjectSourceLocation implements IProjectSourceLocation
private Object doFindSourceElement( String name ) private Object doFindSourceElement( String name )
{ {
File file = new File( name ); File file = new File( name );
return ( file.isAbsolute() ) ? findFileByAbsolutePath( name ) : return ( file.isAbsolute() ) ? findFileByAbsolutePath( file ) : findFileByRelativePath( name );
findFileByRelativePath( name );
} }
private Object findFileByAbsolutePath( String name ) private Object findFileByAbsolutePath( File file )
{ {
File file = new File( name );
LinkedList list = new LinkedList(); LinkedList list = new LinkedList();
if ( file.isAbsolute() && file.exists() ) if ( file.exists() )
{ {
IResource[] folders = getFolders(); IPath path = new Path( file.getAbsolutePath() );
for ( int i = 0; i < folders.length; ++i ) IFile[] wsFiles = CDebugCorePlugin.getWorkspace().getRoot().findFilesForLocation( path );
{ for ( int i = 0; i < wsFiles.length; ++i )
Object result = findFile( folders[i], file ); if ( wsFiles[i].getProject().equals( getProject() ) && wsFiles[i].exists() )
if ( !searchForDuplicateFileNames() ) if ( !searchForDuplicateFileNames() )
return result; return wsFiles[i];
list.add( result ); else
} list.add( wsFiles[i] );
} }
return ( list.size() > 0 ) ? list : null; return ( list.size() > 0 ) ? list : null;
} }
@ -182,110 +179,21 @@ public class CProjectSourceLocation implements IProjectSourceLocation
if ( list.size() > 0 && !searchForDuplicateFileNames() ) if ( list.size() > 0 && !searchForDuplicateFileNames() )
break; break;
IPath path = folders[i].getLocation().append( fileName ); IPath path = folders[i].getLocation().append( fileName );
Object result = findFile( folders[i], new File( path.toOSString() ) ); File file = new File( path.toOSString() );
if ( result != null ) if ( file.exists() )
{ {
if ( !searchForDuplicateFileNames() ) IFile[] wsFiles = CDebugCorePlugin.getWorkspace().getRoot().findFilesForLocation( path );
return result; for ( int j = 0; j < wsFiles.length; ++j )
list.add( result ); if ( wsFiles[j].exists() )
if ( !searchForDuplicateFileNames() )
return wsFiles[j];
else
list.add( wsFiles[j] );
} }
} }
return ( list.size() > 0 ) ? list : null; return ( list.size() > 0 ) ? list : null;
} }
// private Object findFile( final File file )
// {
// if ( file != null )
// {
// final String name = file.getName();
// IResource[] folders = getFolders();
// final LinkedList list = new LinkedList();
// for ( int i = 0; i < folders.length; ++i )
// {
// if ( list.size() > 0 && !searchForDuplicateFileNames() )
// break;
// try
// {
// folders[i].accept(
// new IResourceProxyVisitor()
// {
// public boolean visit( IResourceProxy proxy ) throws CoreException
// {
// // use equalsIgnoreCase to make it work for Wondows
// if ( proxy.getType() == IResource.FILE && proxy.getName().equalsIgnoreCase( name ) )
// {
// IResource resource = proxy.requestResource();
// File file1 = new File( resource.getLocation().toOSString() );
// if ( file1.exists() && file1.equals( file ) )
// list.addLast( resource );
// return false;
// }
// else if ( proxy.getType() == IResource.FOLDER )
// return false;
// return true;
// }
// },
// IResource.NONE );
// }
// catch( CoreException e )
// {
// }
// }
// if ( list.size() == 1 || (list.size() > 0 && !searchForDuplicateFileNames()) )
// return list.getFirst();
// if ( list.size() > 0 )
// return list;
// }
// return null;
// }
private Object findFile( IResource folder, final File file )
{
// The workspace resources are case-sensitive, so we can not just
// append the file name to the folder name and check if the result exists.
final Object[] result = new Object[] { null };
if ( file != null && folder != null )
{
final String name = file.getName();
IPath dirPath = new Path( file.getAbsolutePath() );
dirPath = dirPath.removeLastSegments( 1 );
final File dir = new File( dirPath.toOSString() );
if ( dir.equals( folder.getLocation().toFile() ) )
{
try
{
folder.accept(
new IResourceProxyVisitor()
{
public boolean visit( IResourceProxy proxy ) throws CoreException
{
if ( result[0] != null )
return false;
// use equalsIgnoreCase to make it work for Wondows
if ( proxy.getType() == IResource.FILE && proxy.getName().equalsIgnoreCase( name ) )
{
IResource resource = proxy.requestResource();
File file1 = new File( resource.getLocation().toOSString() );
if ( file1.exists() && file1.equals( file ) )
result[0] = resource;
return false;
}
else if ( proxy.getType() == IResource.FOLDER )
return false;
return true;
}
},
IResource.NONE );
}
catch( CoreException e )
{
}
}
}
return result[0];
}
private Object cacheLookup( String name ) private Object cacheLookup( String name )
{ {
return fCache.get( name ); return fCache.get( name );
@ -362,7 +270,6 @@ public class CProjectSourceLocation implements IProjectSourceLocation
if ( isGeneric == null || isGeneric.trim().length() == 0 ) if ( isGeneric == null || isGeneric.trim().length() == 0 )
isGeneric = Boolean.FALSE.toString(); isGeneric = Boolean.FALSE.toString();
setGenerated( isGeneric.equals( Boolean.TRUE.toString() ) ); setGenerated( isGeneric.equals( Boolean.TRUE.toString() ) );
// initializeFolders();
return; return;
} }
catch( ParserConfigurationException e ) catch( ParserConfigurationException e )

View file

@ -6,7 +6,6 @@
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 java.text.MessageFormat; import java.text.MessageFormat;
@ -21,7 +20,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.apache.xerces.dom.DocumentImpl; import org.apache.xerces.dom.DocumentImpl;
import org.eclipse.cdt.core.resources.FileStorage;
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.model.IStackFrameInfo; import org.eclipse.cdt.debug.core.model.IStackFrameInfo;
@ -36,10 +34,8 @@ import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener; import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
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.model.IPersistableSourceLocator; import org.eclipse.debug.core.model.IPersistableSourceLocator;
@ -122,23 +118,19 @@ public class CSourceLocator implements ICSourceLocator, IPersistableSourceLocato
String fileName = info.getFile(); String fileName = info.getFile();
if ( fileName != null && fileName.length() > 0 ) if ( fileName != null && fileName.length() > 0 )
{ {
result = findFileByAbsolutePath( fileName ); ICSourceLocation[] locations = getSourceLocations();
if ( result == null ) for ( int i = 0; i < locations.length; ++i )
{ {
ICSourceLocation[] locations = getSourceLocations(); try
for ( int i = 0; i < locations.length; ++i )
{ {
try result = locations[i].findSourceElement( fileName );
{
result = locations[i].findSourceElement( fileName );
}
catch( CoreException e )
{
// do nothing
}
if ( result != null )
break;
} }
catch( CoreException e )
{
// do nothing
}
if ( result != null )
break;
} }
} }
} }
@ -165,7 +157,10 @@ public class CSourceLocator implements ICSourceLocator, IPersistableSourceLocato
{ {
try try
{ {
if ( locations[i].findSourceElement( resource.getLocation().toOSString() ) != null ) Object result = locations[i].findSourceElement( resource.getLocation().toOSString() );
if ( result instanceof IFile && ((IFile)result).equals( resource ) )
return true;
if ( result instanceof List && ((List)result).contains( resource ) )
return true; return true;
} }
catch( CoreException e ) catch( CoreException e )
@ -248,29 +243,6 @@ public class CSourceLocator implements ICSourceLocator, IPersistableSourceLocato
return false; return false;
} }
private Object findFileByAbsolutePath( String fileName )
{
File file = new File( fileName );
if ( file.isAbsolute() && file.exists() )
{
try
{
Path path = new Path( file.getCanonicalPath() );
// Try for a file in another workspace project
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( path );
if ( f != null && f.exists() )
{
return f;
}
return new FileStorage( path );
}
catch( IOException e )
{
}
}
return null;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator#findSourceElement(String) * @see org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator#findSourceElement(String)
*/ */
@ -279,23 +251,19 @@ public class CSourceLocator implements ICSourceLocator, IPersistableSourceLocato
Object result = null; Object result = null;
if ( fileName != null && fileName.length() > 0 ) if ( fileName != null && fileName.length() > 0 )
{ {
result = findFileByAbsolutePath( fileName ); ICSourceLocation[] locations = getSourceLocations();
if ( result == null ) for ( int i = 0; i < locations.length; ++i )
{ {
ICSourceLocation[] locations = getSourceLocations(); try
for ( int i = 0; i < locations.length; ++i )
{ {
try result = locations[i].findSourceElement( fileName );
{
result = locations[i].findSourceElement( fileName );
}
catch( CoreException e )
{
// do nothing
}
if ( result != null )
break;
} }
catch( CoreException e )
{
// do nothing
}
if ( result != null )
break;
} }
} }
return result; return result;