Patch from Anders Betn�- improve path mapping when remote debugging between systems...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPDocumentProvider.java
index 74f98be..301c6a1 100644 (file)
@@ -9,38 +9,31 @@ http://www.eclipse.org/legal/cpl-v10.html
 
 Contributors:
     IBM Corporation - Initial implementation
-    Klaus Hartlage - www.eclipseproject.de
+    www.phpeclipse.de
 **********************************************************************/
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
 import net.sourceforge.phpdt.core.ICompilationUnit;
-import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
 import net.sourceforge.phpdt.core.IProblemRequestor;
 import net.sourceforge.phpdt.core.JavaCore;
 import net.sourceforge.phpdt.core.JavaModelException;
 import net.sourceforge.phpdt.core.compiler.IProblem;
-import net.sourceforge.phpdt.internal.ui.PHPUIStatus;
+import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
 import net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension;
+import net.sourceforge.phpdt.internal.ui.text.spelling.SpellReconcileStrategy.SpellProblem;
 import net.sourceforge.phpdt.ui.PreferenceConstants;
-import net.sourceforge.phpdt.ui.text.JavaTextTools;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IMarker;
-import org.eclipse.core.resources.IMarkerDelta;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceRuleFactory;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.text.Assert;
@@ -68,14 +61,14 @@ import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.Canvas;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.editors.text.EditorsUI;
+import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
-import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
 import org.eclipse.ui.texteditor.AnnotationPreference;
 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
+import org.eclipse.ui.texteditor.IDocumentProvider;
 import org.eclipse.ui.texteditor.MarkerAnnotation;
 import org.eclipse.ui.texteditor.MarkerUtilities;
 import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
@@ -84,7 +77,7 @@ import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
  * The PHPDocumentProvider provides the IDocuments used by java editors.
  */
 
-public class PHPDocumentProvider extends TextFileDocumentProvider {
+public class PHPDocumentProvider extends TextFileDocumentProvider implements ICompilationUnitDocumentProvider {
   /**
                         * Here for visibility issues only.
                         */
@@ -168,7 +161,7 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                }
                
                /*
-                * @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence()
+                * @see net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence()
                 */
                public void beginReportingSequence() {
                        ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get();
@@ -216,7 +209,7 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                }
                
                /*
-                * @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence()
+                * @see net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence()
                 */
                public void endReportingSequence() {
                        ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get();
@@ -528,11 +521,10 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                        fProblem= problem;
                        fCompilationUnit= cu;
                        
-//                     if (SpellProblem.Spelling == fProblem.getID()) {
-//                             setType(SPELLING_ANNOTATION_TYPE);
-//                             fLayer= WARNING_LAYER;
-//                     } else 
-                   if (IProblem.Task == fProblem.getID()) {
+                       if (SpellProblem.Spelling == fProblem.getID()) {
+                               setType(SPELLING_ANNOTATION_TYPE);
+                               fLayer= WARNING_LAYER;
+                       } else if (IProblem.Task == fProblem.getID()) {
                                setType(JavaMarkerAnnotation.TASK_ANNOTATION_TYPE);
                                fLayer= TASK_LAYER;
                        } else if (fProblem.isWarning()) {
@@ -631,7 +623,7 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                }
                
                /*
-                * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
+                * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
                 */
                public IJavaAnnotation getOverlay() {
                        return null;
@@ -667,7 +659,7 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                }
                
                /*
-                * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
+                * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
                 */
                public ICompilationUnit getCompilationUnit() {
                        return fCompilationUnit;
@@ -842,15 +834,28 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
   private GlobalAnnotationModelListener fGlobalAnnotationModelListener;
 
   public PHPDocumentProvider() {
-       setParentDocumentProvider(new TextFileDocumentProvider(new JavaStorageDocumentProvider()));             
+//     setParentDocumentProvider(new TextFileDocumentProvider(new JavaStorageDocumentProvider()));             
+//     
+//     fPropertyListener= new IPropertyChangeListener() {
+//             public void propertyChange(PropertyChangeEvent event) {
+//                     if (HANDLE_TEMPORARY_PROBLEMS.equals(event.getProperty()))
+//                             enableHandlingTemporaryProblems();
+//             }
+//     };
+//     fGlobalAnnotationModelListener= new GlobalAnnotationModelListener();
+//     PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyListener);
+       
+       IDocumentProvider provider= new TextFileDocumentProvider(new JavaStorageDocumentProvider());
+       provider= new ForwardingDocumentProvider(IPHPPartitions.PHP_PARTITIONING, new JavaDocumentSetupParticipant(), provider);
+       setParentDocumentProvider(provider);
        
+       fGlobalAnnotationModelListener= new GlobalAnnotationModelListener();
        fPropertyListener= new IPropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent event) {
                        if (HANDLE_TEMPORARY_PROBLEMS.equals(event.getProperty()))
                                enableHandlingTemporaryProblems();
                }
        };
-       fGlobalAnnotationModelListener= new GlobalAnnotationModelListener();
        PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyListener);
 
   }
@@ -1099,8 +1104,7 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
        }
        
   protected void commitWorkingCopy(IProgressMonitor monitor, Object element, CompilationUnitInfo info, boolean overwrite) throws CoreException {
-       
-       synchronized (info.fCopy) {
+    synchronized (info.fCopy) {
                info.fCopy.reconcile();
        }
        
@@ -1153,12 +1157,39 @@ public class PHPDocumentProvider extends TextFileDocumentProvider {
                        }
                }
        }
+       
 }
 
 /*
  * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createSaveOperation(java.lang.Object, org.eclipse.jface.text.IDocument, boolean)
  */
 protected DocumentProviderOperation createSaveOperation(final Object element, final IDocument document, final boolean overwrite) throws CoreException {
+//     final FileInfo info= getFileInfo(element);
+//     if (info instanceof CompilationUnitInfo) {
+//             return new DocumentProviderOperation() {
+//                     /*
+//                      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
+//                      */
+//                     protected void execute(IProgressMonitor monitor) throws CoreException {
+//                             commitWorkingCopy(monitor, element, (CompilationUnitInfo) info, overwrite);
+//                     }
+//                     /*
+//                      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#getSchedulingRule()
+//                      */
+//                     public ISchedulingRule getSchedulingRule() {
+//                             if (info.fElement instanceof IFileEditorInput) {
+//                                     IFile file= ((IFileEditorInput) info.fElement).getFile();
+//                                     IResourceRuleFactory ruleFactory= ResourcesPlugin.getWorkspace().getRuleFactory();
+//                                     if (file == null || !file.exists())
+//                                             return ruleFactory.createRule(file);
+//                                     else
+//                                             return ruleFactory.modifyRule(file);
+//                             } else
+//                                     return null;
+//                     }
+//             };
+//     }
+//     return null;
        final FileInfo info= getFileInfo(element);
        if (info instanceof CompilationUnitInfo) {
                return new DocumentProviderOperation() {
@@ -1174,11 +1205,7 @@ protected DocumentProviderOperation createSaveOperation(final Object element, fi
                        public ISchedulingRule getSchedulingRule() {
                                if (info.fElement instanceof IFileEditorInput) {
                                        IFile file= ((IFileEditorInput) info.fElement).getFile();
-                                       IResourceRuleFactory ruleFactory= ResourcesPlugin.getWorkspace().getRuleFactory();
-                                       if (file == null || !file.exists())
-                                               return ruleFactory.createRule(file);
-                                       else
-                                               return ruleFactory.modifyRule(file);
+                                       return computeSchedulingRule(file);
                                } else
                                        return null;
                        }
@@ -1452,24 +1479,26 @@ protected DocumentProviderOperation createSaveOperation(final Object element, fi
         */
   public void saveDocumentContent(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
     throws CoreException {
-
-    if (!fIsAboutToSave)
-      return;
-
-    if (element instanceof IFileEditorInput) {
-      IFileEditorInput input = (IFileEditorInput) element;
-      try {
-        String encoding = getEncoding(element);
-        if (encoding == null)
-          encoding = ResourcesPlugin.getEncoding();
-        InputStream stream = new ByteArrayInputStream(document.get().getBytes(encoding));
-        IFile file = input.getFile();
-        file.setContents(stream, overwrite, true, monitor);
-      } catch (IOException x) {
-        IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.OK, x.getMessage(), x);
-        throw new CoreException(s);
-      }
-    }
+       if (!fIsAboutToSave)
+               return;
+       super.saveDocument(monitor, element, document, overwrite);
+//    if (!fIsAboutToSave)
+//      return;
+//
+//    if (element instanceof IFileEditorInput) {
+//      IFileEditorInput input = (IFileEditorInput) element;
+//      try {
+//        String encoding = getEncoding(element);
+//        if (encoding == null)
+//          encoding = ResourcesPlugin.getEncoding();
+//        InputStream stream = new ByteArrayInputStream(document.get().getBytes(encoding));
+//        IFile file = input.getFile();
+//        file.setContents(stream, overwrite, true, monitor);
+//      } catch (IOException x) {
+//        IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.OK, x.getMessage(), x);
+//        throw new CoreException(s);
+//      }
+//    }
   }
   /**
    * Returns the underlying resource for the given element.
@@ -1477,16 +1506,16 @@ protected DocumentProviderOperation createSaveOperation(final Object element, fi
    * @param the element
    * @return the underlying resource of the given element
    */
-  public IResource getUnderlyingResource(Object element) {
-    if (element instanceof IFileEditorInput) {
-      IFileEditorInput input = (IFileEditorInput) element;
-      return input.getFile();
-    }
-    return null;
-  }
+//  public IResource getUnderlyingResource(Object element) {
+//    if (element instanceof IFileEditorInput) {
+//      IFileEditorInput input = (IFileEditorInput) element;
+//      return input.getFile();
+//    }
+//    return null;
+//  }
 
        /*
-        * @see org.eclipse.jdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#getWorkingCopy(java.lang.Object)
+        * @see net.sourceforge.phpdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#getWorkingCopy(java.lang.Object)
         */
        public ICompilationUnit getWorkingCopy(Object element) {
                FileInfo fileInfo= getFileInfo(element);                
@@ -1499,7 +1528,7 @@ protected DocumentProviderOperation createSaveOperation(final Object element, fi
 
 
   /*
-        * @see org.eclipse.jdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#shutdown()
+        * @see net.sourceforge.phpdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#shutdown()
         */
        public void shutdown() {
                PHPeclipsePlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyListener);
@@ -1543,5 +1572,36 @@ protected DocumentProviderOperation createSaveOperation(final Object element, fi
   public void removeGlobalAnnotationModelListener(IAnnotationModelListener listener) {
     fGlobalAnnotationModelListener.removeListener(listener);
   }
-
+       /**
+        * Computes the scheduling rule needed to create or modify a resource. If
+        * the resource exists, its modify rule is returned. If it does not, the 
+        * resource hierarchy is iterated towards the workspace root to find the
+        * first parent of <code>toCreateOrModify</code> that exists. Then the
+        * 'create' rule for the last non-existing resource is returned.
+        * <p>
+        * XXX This is a workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=67601
+        * IResourceRuleFactory.createRule should iterate the hierarchy itself. 
+        * </p>
+        * <p> 
+        * XXX to be replaced by call to TextFileDocumentProvider.computeSchedulingRule after 3.0
+        * </p>
+        * 
+        * @param toCreateOrModify the resource to create or modify
+        * @return the minimal scheduling rule needed to modify or create a resource
+        */
+   protected ISchedulingRule computeSchedulingRule(IResource toCreateOrModify) {
+               IResourceRuleFactory factory= ResourcesPlugin.getWorkspace().getRuleFactory();
+               if (toCreateOrModify.exists()) {
+                       return factory.modifyRule(toCreateOrModify);
+               } else {
+                       IResource parent= toCreateOrModify;
+                       do {
+                               toCreateOrModify= parent;
+                               parent= toCreateOrModify.getParent();
+                       } while (parent != null && !parent.exists());
+                       
+                       return factory.createRule(toCreateOrModify);
+               }
+       }
 }