diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java
index 1e7630d7378..f49c62adfff 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java
@@ -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 null
+ */
+ 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)
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java
index 979caa163aa..7a69758d669 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java
@@ -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 FileEditorInput
- * is returned, otherwise, the input is an IStorageEditorInput
- * 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) {
+ // no translation unit - still try to get a sensible marker resource
+ // from the associated element
IResource markerResource= cproject.getProject();
return new ExternalEditorInput(new FileStorage(location), markerResource);
}