From dfeae8915c69d3e2528de91c93a347b5e7da7f66 Mon Sep 17 00:00:00 2001 From: Teodor Madan Date: Mon, 4 Apr 2011 14:33:40 +0000 Subject: [PATCH] Bug 341607 - Empty editor content for unavailable file resources when debugger steps into those source files --- .../CompilationDirectorySourceContainer.java | 4 +- .../core/sourcelookup/SourceUtils.java | 46 +++++++++++++++---- .../internal/ui/CDebugModelPresentation.java | 24 +++++----- .../AbstractToggleBreakpointAdapter.java | 2 +- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CompilationDirectorySourceContainer.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CompilationDirectorySourceContainer.java index 13deac25a49..296804d7e0c 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CompilationDirectorySourceContainer.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CompilationDirectorySourceContainer.java @@ -112,9 +112,9 @@ public class CompilationDirectorySourceContainer extends CompositeSourceContaine if (!file.isAbsolute()) { file = new File(fDirectory, name); } - List sources; + List sources = new ArrayList(); if (file.exists() && file.isFile()) { - sources = Arrays.asList(SourceUtils.findSourceElements(file, getDirector())); + sources.addAll(Arrays.asList(SourceUtils.findSourceElements(file, getDirector()))); } else { sources = new ArrayList(); } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/SourceUtils.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/SourceUtils.java index 272d5d8f58c..6b340857ab6 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/SourceUtils.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/SourceUtils.java @@ -28,6 +28,7 @@ import javax.xml.transform.TransformerException; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugUtils; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; @@ -37,8 +38,8 @@ import org.eclipse.cdt.debug.core.sourcelookup.IDirectorySourceLocation; import org.eclipse.cdt.debug.core.sourcelookup.IMappingSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation; 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.filesystem.URIUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; @@ -145,7 +146,7 @@ public class SourceUtils { clazz = CDebugCorePlugin.getDefault().getBundle().loadClass(className); } catch (ClassNotFoundException e) { CDebugCorePlugin.log(MessageFormat.format("Unable to restore source location - class not found {0}", //$NON-NLS-1$ - new String[] { className })); + (Object[]) new String[] { className })); continue; } ICSourceLocation location = null; @@ -256,14 +257,15 @@ public class SourceUtils { * @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 + * be either instances of IFile, ITranslationUnit 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())); + IProject launchConfigurationProject = getLaunchConfigurationProject(director); if (wfiles.length > 0) { - ResourceLookup.sortFilesByRelevance(wfiles, getLaunchConfigurationProject(director)); - return wfiles; + ResourceLookup.sortFilesByRelevance(wfiles, launchConfigurationProject); + return updateUnavailableResources(wfiles, launchConfigurationProject); } try { @@ -271,8 +273,8 @@ public class SourceUtils { // systems like Windows. wfiles = ResourceLookup.findFilesForLocation(new Path(file.getCanonicalPath())); if (wfiles.length > 0) { - ResourceLookup.sortFilesByRelevance(wfiles, getLaunchConfigurationProject(director)); - return wfiles; + ResourceLookup.sortFilesByRelevance(wfiles, launchConfigurationProject); + return updateUnavailableResources(wfiles, launchConfigurationProject); } // The file is not already in the workspace so try to create an external translation unit for it. @@ -281,9 +283,7 @@ public class SourceUtils { 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) }; + return new ITranslationUnit[] { CoreModel.getDefault().createTranslationUnitFrom(project, URIUtil.toURI(file.getCanonicalPath(), true)) }; } } } @@ -293,4 +293,30 @@ public class SourceUtils { // If we can't create an ETU then fall back on LocalFileStorage. return new LocalFileStorage[] { new LocalFileStorage(file) }; } + + /** + * Check for IFile to be available in workspace ( see {@link IFile#isAccessible()} ). + * Unavailable resources are replaced with {@link ITranslationUnit} + * @param wfiles + * @param project + * @return + */ + private static Object[] updateUnavailableResources(IFile[] wfiles, IProject project){ + // with no projects context we will not be able to create ITranslationUnits + if (project == null) { + return wfiles; + } + + ICProject cProject = CoreModel.getDefault().create(project); + Object[] result = new Object[wfiles.length]; + for (int i=0; i< wfiles.length; ++i) { + IFile wkspFile = wfiles[i]; + if (wkspFile.isAccessible()) { + result[i] = wkspFile; + } else { + result[i] = CoreModel.getDefault().createTranslationUnitFrom(cProject, wkspFile.getLocationURI()); + } + } + return result; + } } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugModelPresentation.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugModelPresentation.java index 2e126b9cb9a..9447f043b0a 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugModelPresentation.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDebugModelPresentation.java @@ -166,7 +166,7 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode IProject project = b.getMarker().getResource().getProject(); // Select the most 'relevant' IFile for an external location file = ResourceLookup.selectFileForLocation(path, project); - if (file == null) { + if (file == null || !file.isAccessible()) { // Try resolving the path to a real io.File File fsfile = new File( handle ); if ( fsfile.isFile() && fsfile.exists() ) { @@ -470,7 +470,7 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode } } catch( DebugException e ) { - return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.2" ), new String[] { e.getMessage() } ); //$NON-NLS-1$ + return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.2" ), e.getMessage() ); //$NON-NLS-1$ } catch( CoreException e ) { CDebugUIPlugin.log( e ); @@ -682,15 +682,15 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode String reason = ""; //$NON-NLS-1$ if ( info != null && info instanceof ICDISignalExitInfo ) { ICDISignalExitInfo sigInfo = (ICDISignalExitInfo)info; - reason = ' ' + MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.5" ), new String[]{ sigInfo.getName(), sigInfo.getDescription() } ); //$NON-NLS-1$ + reason = ' ' + MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.5" ), sigInfo.getName(), sigInfo.getDescription() ); //$NON-NLS-1$ } else if ( info != null && info instanceof ICDIExitInfo ) { - reason = ' ' + MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.6" ), new Integer[] { Integer.valueOf( ((ICDIExitInfo)info).getCode() ) } ); //$NON-NLS-1$ + reason = ' ' + MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.6" ), Integer.valueOf( ((ICDIExitInfo)info).getCode() ) ); //$NON-NLS-1$ } - return MessageFormat.format( label, new String[] { target.getName(), reason } ); + return MessageFormat.format( label, target.getName(), reason ); } else if ( state.equals( CDebugElementState.SUSPENDED ) ) { - return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.7" ), new String[] { target.getName() } ); //$NON-NLS-1$ + return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.7" ), target.getName() ); //$NON-NLS-1$ } } } @@ -718,10 +718,10 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode Object info = element.getCurrentStateInfo(); if ( info instanceof ICDISignalReceived ) { ICDISignal signal = ((ICDISignalReceived)info).getSignal(); - reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.13" ), new String[]{ signal.getName(), signal.getDescription() } ); //$NON-NLS-1$ + reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.13" ), signal.getName(), signal.getDescription() ); //$NON-NLS-1$ } else if ( info instanceof ICDIWatchpointTrigger ) { - reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.14" ), new String[]{ ((ICDIWatchpointTrigger)info).getOldValue(), ((ICDIWatchpointTrigger)info).getNewValue() } ); //$NON-NLS-1$ + reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.14" ), ((ICDIWatchpointTrigger)info).getOldValue(), ((ICDIWatchpointTrigger)info).getNewValue() ); //$NON-NLS-1$ } else if ( info instanceof ICDIWatchpointScope ) { reason = CDebugUIMessages.getString( "CDTDebugModelPresentation.15" ); //$NON-NLS-1$ @@ -733,12 +733,12 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode reason = CDebugUIMessages.getString( "CDTDebugModelPresentation.17" ); //$NON-NLS-1$ } else if ( info instanceof ICDIEventBreakpointHit ) { - reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.20" ), new String[]{ ((ICDIEventBreakpointHit)info).getEventBreakpointType() } ); //$NON-NLS-1$ + reason = MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.20" ), ((ICDIEventBreakpointHit)info).getEventBreakpointType() ); //$NON-NLS-1$ } } - return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.18" ), new String[] { thread.getName(), reason } ); //$NON-NLS-1$ + return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.18" ), thread.getName(), reason ); //$NON-NLS-1$ } - return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.19" ), new String[] { thread.getName() } ); //$NON-NLS-1$ + return MessageFormat.format( CDebugUIMessages.getString( "CDTDebugModelPresentation.19" ), thread.getName() ); //$NON-NLS-1$ } protected String getStackFrameText( IStackFrame f, boolean qualified ) throws DebugException { @@ -790,7 +790,7 @@ public class CDebugModelPresentation extends LabelProvider implements IDebugMode } public static String getFormattedString( String string, String[] args ) { - return MessageFormat.format( string, args ); + return MessageFormat.format( string, (Object[]) args ); } } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/AbstractToggleBreakpointAdapter.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/AbstractToggleBreakpointAdapter.java index cf90164009a..b6039bb2c7a 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/AbstractToggleBreakpointAdapter.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/AbstractToggleBreakpointAdapter.java @@ -267,7 +267,7 @@ abstract public class AbstractToggleBreakpointAdapter implements IToggleBreakpoi IPath location = provider.getPath( editorInput ); if ( location != null ) { IFile[] files = root.findFilesForLocationURI( URIUtil.toURI( location ) ); - if ( files.length > 0 ) + if ( files.length > 0 && files[0].isAccessible()) return files[0]; } }