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

Partial fix for 176036: The CEditor does not work well with files opened from outside of the workspace

This commit is contained in:
Anton Leherbauer 2007-05-15 12:28:06 +00:00
parent 6abeeb8397
commit 9fb46ad9ae
2 changed files with 36 additions and 23 deletions

View file

@ -22,6 +22,8 @@ import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jface.preference.IPreferenceStore;
@ -40,9 +42,11 @@ import org.eclipse.jface.text.source.IAnnotationModelListener;
import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
import org.eclipse.ui.editors.text.ILocationProvider;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.MarkerAnnotation;
@ -61,6 +65,7 @@ import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.core.model.IBufferFactory;
import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
/**
* A document provider for C/C++ content.
@ -729,6 +734,15 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} else if (element instanceof ITranslationUnitEditorInput) {
ITranslationUnitEditorInput input = (ITranslationUnitEditorInput)element;
original = input.getTranslationUnit();
} else if (element instanceof IAdaptable) {
IAdaptable adaptable= (IAdaptable)element;
ILocationProvider locationProvider= (ILocationProvider)adaptable.getAdapter(ILocationProvider.class);
if (locationProvider != null) {
IPath location= locationProvider.getPath(element);
if (location != null) {
original= createTranslationUnit(location);
}
}
}
if (original == null) {
@ -768,6 +782,19 @@ public class CDocumentProvider extends TextFileDocumentProvider {
return tuInfo;
}
/**
* Try to synthesize an ITranslationUnit out of thin air.
* @param location the file system location of the file in question
* @return a translation unit or <code>null</code>
*/
private ITranslationUnit createTranslationUnit(IPath location) {
IEditorInput input= EditorUtility.getEditorInputForLocation(location, null);
if (input instanceof ITranslationUnitEditorInput) {
return ((ITranslationUnitEditorInput)input).getTranslationUnit();
}
return null;
}
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object,
* org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)

View file

@ -255,20 +255,6 @@ public class EditorUtility {
return null;
}
/**
* Utility method to get an editor input for the given file system location.
* If the location denotes a workspace file, a <code>FileEditorInput</code>
* is returned, otherwise, the input is an <code>IStorageEditorInput</code>
* assuming the location points to an existing file in the file system.
*
* @param location a valid file system location
* @return an editor input
* @deprecated Use {@link #getEditorInputForLocation(IPath, ICElement)} instead
*/
public static IEditorInput getEditorInputForLocation(IPath location) {
return getEditorInputForLocation(location, null);
}
/**
* Utility method to open an editor for the given file system location
* using {@link #getEditorInputForLocation(IPath, ICElement)} to create
@ -306,16 +292,18 @@ public class EditorUtility {
// include paths
try {
ICProject[] projects = CCorePlugin.getDefault().getCoreModel().getCModel().getCProjects();
for (int i = 0; i < projects.length; i++) {
outerFor: for (int i = 0; i < projects.length; i++) {
IIncludeReference[] includeReferences = projects[i].getIncludeReferences();
for (int j = 0; j < includeReferences.length; j++) {
if (includeReferences[j].isOnIncludeEntry(location)) {
context = includeReferences[j].getCProject();
break;
context = projects[i];
break outerFor;
}
}
if (context != null)
break;
}
if (context == null && projects.length > 0) {
// last resort: just take any of them
context= projects[0];
}
} catch (CModelException e) {
}
@ -329,10 +317,8 @@ public class EditorUtility {
if (unit != null) {
return new ExternalEditorInput(unit, new FileStorage(location));
}
}
// no translation unit - still try to get a sensible marker resource
// from the associated element
if (cproject != null) {
IResource markerResource= cproject.getProject();
return new ExternalEditorInput(new FileStorage(location), markerResource);
}