Added mapped path field
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPDocumentProvider.java
index fb4f623..a79460d 100644 (file)
@@ -54,9 +54,11 @@ import org.eclipse.jface.text.ILineTracker;
 import org.eclipse.jface.text.Position;
 import org.eclipse.jface.text.source.Annotation;
 import org.eclipse.jface.text.source.AnnotationModelEvent;
+import org.eclipse.jface.text.source.IAnnotationAccessExtension;
 import org.eclipse.jface.text.source.IAnnotationModel;
 import org.eclipse.jface.text.source.IAnnotationModelListener;
 import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
+import org.eclipse.jface.text.source.IAnnotationPresentation;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.ListenerList;
 import org.eclipse.jface.util.PropertyChangeEvent;
@@ -68,9 +70,12 @@ 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.FileDocumentProvider;
 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.MarkerAnnotation;
 import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
 
@@ -142,7 +147,7 @@ public class PHPDocumentProvider extends FileDocumentProvider {
     /*
      * @see AnnotationModel#addAnnotation(Annotation, Position, boolean)
      */
-    protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) {
+    protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) throws BadLocationException {
       super.addAnnotation(annotation, position, fireModelChanged);
 
       Object cached = fReverseMap.get(position);
@@ -185,65 +190,128 @@ public class PHPDocumentProvider extends FileDocumentProvider {
 
       return new Position(start, length);
     }
-
     /*
      * @see IProblemRequestor#endReporting()
      */
     public void endReporting() {
       if (!isActive())
         return;
-
+      
       if (fProgressMonitor != null && fProgressMonitor.isCanceled())
         return;
-
-      boolean isCanceled = false;
-      boolean temporaryProblemsChanged = false;
-      fPreviouslyOverlaid = fCurrentlyOverlaid;
-      fCurrentlyOverlaid = new ArrayList();
-
+      
+      
+      boolean isCanceled= false;
+      boolean temporaryProblemsChanged= false;
+      
       synchronized (fAnnotations) {
+        
+        fPreviouslyOverlaid= fCurrentlyOverlaid;
+        fCurrentlyOverlaid= new ArrayList();
 
         if (fGeneratedAnnotations.size() > 0) {
-          temporaryProblemsChanged = true;
+          temporaryProblemsChanged= true;      
           removeAnnotations(fGeneratedAnnotations, false, true);
           fGeneratedAnnotations.clear();
         }
-
+        
         if (fCollectedProblems != null && fCollectedProblems.size() > 0) {
-
-          Iterator e = fCollectedProblems.iterator();
+          
+          ICompilationUnit cu= getWorkingCopy(fInput);
+          Iterator e= fCollectedProblems.iterator();
           while (e.hasNext()) {
-
-            IProblem problem = (IProblem) e.next();
-
+            
+            IProblem problem= (IProblem) e.next();
+            
             if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
-              isCanceled = true;
+              isCanceled= true;
               break;
             }
-
-            Position position = createPositionFromProblem(problem);
+            
+            Position position= createPositionFromProblem(problem);
             if (position != null) {
-
-              ProblemAnnotation annotation = new ProblemAnnotation(problem);
-              overlayMarkers(position, annotation);
-              fGeneratedAnnotations.add(annotation);
-              addAnnotation(annotation, position, false);
-
-              temporaryProblemsChanged = true;
+              try {
+                ProblemAnnotation annotation= new ProblemAnnotation(problem, cu);
+                addAnnotation(annotation, position, false);
+                overlayMarkers(position, annotation);                                                          
+                fGeneratedAnnotations.add(annotation);
+                
+                temporaryProblemsChanged= true;
+              } catch (BadLocationException x) {
+                // ignore invalid position
+              }
             }
           }
-
+          
           fCollectedProblems.clear();
         }
-
+        
         removeMarkerOverlays(isCanceled);
         fPreviouslyOverlaid.clear();
-        fPreviouslyOverlaid = null;
+        fPreviouslyOverlaid= null;
       }
-
+      
       if (temporaryProblemsChanged)
-        fireModelChanged(new CompilationUnitAnnotationModelEvent(this, getResource(), false));
+        fireModelChanged();
     }
+    /*
+     * @see IProblemRequestor#endReporting()
+     */
+//    public void endReporting() {
+//      if (!isActive())
+//        return;
+//
+//      if (fProgressMonitor != null && fProgressMonitor.isCanceled())
+//        return;
+//
+//      boolean isCanceled = false;
+//      boolean temporaryProblemsChanged = false;
+//      fPreviouslyOverlaid = fCurrentlyOverlaid;
+//      fCurrentlyOverlaid = new ArrayList();
+//
+//      synchronized (fAnnotations) {
+//
+//        if (fGeneratedAnnotations.size() > 0) {
+//          temporaryProblemsChanged = true;
+//          removeAnnotations(fGeneratedAnnotations, false, true);
+//          fGeneratedAnnotations.clear();
+//        }
+//
+//        if (fCollectedProblems != null && fCollectedProblems.size() > 0) {
+//
+//          Iterator e = fCollectedProblems.iterator();
+//          while (e.hasNext()) {
+//
+//            IProblem problem = (IProblem) e.next();
+//
+//            if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
+//              isCanceled = true;
+//              break;
+//            }
+//
+//            Position position = createPositionFromProblem(problem);
+//            if (position != null) {
+//
+//              ProblemAnnotation annotation = new ProblemAnnotation(problem);
+//              overlayMarkers(position, annotation);
+//              fGeneratedAnnotations.add(annotation);
+//              addAnnotation(annotation, position, false);
+//
+//              temporaryProblemsChanged = true;
+//            }
+//          }
+//
+//          fCollectedProblems.clear();
+//        }
+//
+//        removeMarkerOverlays(isCanceled);
+//        fPreviouslyOverlaid.clear();
+//        fPreviouslyOverlaid = null;
+//      }
+//
+//      if (temporaryProblemsChanged)
+//        fireModelChanged(new CompilationUnitAnnotationModelEvent(this, getResource(), false));
+//    }
 
     private Object getAnnotations(Position position) {
       return fReverseMap.get(position);
@@ -468,154 +536,211 @@ public class PHPDocumentProvider extends FileDocumentProvider {
   }
 
   /**
-                * Annotation representating an <code>IProblem</code>.
-                */
-  static protected class ProblemAnnotation extends Annotation implements IJavaAnnotation {
-
-    //    private static Image fgQuickFixImage;
-    //    private static Image fgQuickFixErrorImage;
-    //    private static boolean fgQuickFixImagesInitialized = false;
-
-    private List fOverlaids;
-    private IProblem fProblem;
-    private Image fImage;
-    //    private boolean fQuickFixImagesInitialized = false;
-    private AnnotationType fType;
-
-    public ProblemAnnotation(IProblem problem) {
-
-      fProblem = problem;
-      setLayer(MarkerAnnotation.PROBLEM_LAYER + 1);
-
-      if (IProblem.Task == fProblem.getID())
-        fType = AnnotationType.TASK;
-      else if (fProblem.isWarning())
-        fType = AnnotationType.WARNING;
-      else
-        fType = AnnotationType.ERROR;
-    }
-
-    private void initializeImages() {
-      // http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
-      //      if (!fQuickFixImagesInitialized) {
-      //        if (indicateQuixFixableProblems() && JavaCorrectionProcessor.hasCorrections(fProblem.getID())) {
-      //          if (!fgQuickFixImagesInitialized) {
-      //            fgQuickFixImage = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
-      //            fgQuickFixErrorImage = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
-      //            fgQuickFixImagesInitialized = true;
-      //          }
-      //          if (fType == AnnotationType.ERROR)
-      //            fImage = fgQuickFixErrorImage;
-      //          else
-      //            fImage = fgQuickFixImage;
-      //        }
-      //        fQuickFixImagesInitialized = true;
-      //      }
-    }
-
-    private boolean indicateQuixFixableProblems() {
-      return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
-    }
-
-    /*
-     * @see Annotation#paint
-     */
-    public void paint(GC gc, Canvas canvas, Rectangle r) {
-      initializeImages();
-      if (fImage != null)
-        drawImage(fImage, gc, canvas, r, SWT.CENTER, SWT.TOP);
-    }
-
-    /*
-     * @see IJavaAnnotation#getImage(Display)
-     */
-    public Image getImage(Display display) {
-      initializeImages();
-      return fImage;
-    }
-
-    /*
-     * @see IJavaAnnotation#getMessage()
-     */
-    public String getMessage() {
-      return fProblem.getMessage();
-    }
-
-    /*
-     * @see IJavaAnnotation#isTemporary()
-     */
-    public boolean isTemporary() {
-      return true;
-    }
-
-    /*
-     * @see IJavaAnnotation#getArguments()
-     */
-    public String[] getArguments() {
-      return isProblem() ? fProblem.getArguments() : null;
-    }
-
-    /*
-     * @see IJavaAnnotation#getId()
-     */
-    public int getId() {
-      return isProblem() ? fProblem.getID() : -1;
-    }
-
-    /*
-     * @see IJavaAnnotation#isProblem()
-     */
-    public boolean isProblem() {
-      return fType == AnnotationType.WARNING || fType == AnnotationType.ERROR;
-    }
-
-    /*
-     * @see IJavaAnnotation#isRelevant()
-     */
-    public boolean isRelevant() {
-      return true;
-    }
-
-    /*
-     * @see IJavaAnnotation#hasOverlay()
-     */
-    public boolean hasOverlay() {
-      return false;
-    }
-
-    /*
-     * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
-     */
-    public void addOverlaid(IJavaAnnotation annotation) {
-      if (fOverlaids == null)
-        fOverlaids = new ArrayList(1);
-      fOverlaids.add(annotation);
-    }
-
-    /*
-     * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
-     */
-    public void removeOverlaid(IJavaAnnotation annotation) {
-      if (fOverlaids != null) {
-        fOverlaids.remove(annotation);
-        if (fOverlaids.size() == 0)
-          fOverlaids = null;
-      }
-    }
-
-    /*
-     * @see IJavaAnnotation#getOverlaidIterator()
-     */
-    public Iterator getOverlaidIterator() {
-      if (fOverlaids != null)
-        return fOverlaids.iterator();
-      return null;
-    }
-
-    public AnnotationType getAnnotationType() {
-      return fType;
-    }
-  };
+        * Annotation representating an <code>IProblem</code>.
+        */
+       static protected class ProblemAnnotation extends Annotation implements IJavaAnnotation, IAnnotationPresentation {
+               
+               private static final String SPELLING_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.spelling";
+
+               //XXX: To be fully correct these constants should be non-static
+               /** 
+                * The layer in which task problem annotations are located.
+                */
+               private static final int TASK_LAYER;
+               /** 
+                * The layer in which info problem annotations are located.
+                */
+               private static final int INFO_LAYER;
+               /** 
+                * The layer in which warning problem annotations representing are located.
+                */
+               private static final int WARNING_LAYER;
+               /** 
+                * The layer in which error problem annotations representing are located.
+                */
+               private static final int ERROR_LAYER;
+               
+               static {
+                       AnnotationPreferenceLookup lookup= EditorsUI.getAnnotationPreferenceLookup();
+                       TASK_LAYER= computeLayer("org.eclipse.ui.workbench.texteditor.task", lookup); //$NON-NLS-1$
+                       INFO_LAYER= computeLayer("net.sourceforge.phpdt.ui.info", lookup); //$NON-NLS-1$
+                       WARNING_LAYER= computeLayer("net.sourceforge.phpdt.ui.warning", lookup); //$NON-NLS-1$
+                       ERROR_LAYER= computeLayer("net.sourceforge.phpdt.ui.error", lookup); //$NON-NLS-1$
+               }
+               
+               private static int computeLayer(String annotationType, AnnotationPreferenceLookup lookup) {
+                       Annotation annotation= new Annotation(annotationType, false, null);
+                       AnnotationPreference preference= lookup.getAnnotationPreference(annotation);
+                       if (preference != null)
+                               return preference.getPresentationLayer() + 1;
+                       else
+                               return IAnnotationAccessExtension.DEFAULT_LAYER + 1;
+               }
+
+//             private static Image fgQuickFixImage;
+//             private static Image fgQuickFixErrorImage;
+//             private static boolean fgQuickFixImagesInitialized= false;
+               
+               private ICompilationUnit fCompilationUnit;
+               private List fOverlaids;
+               private IProblem fProblem;
+               private Image fImage;
+               private boolean fQuickFixImagesInitialized= false;
+               private int fLayer= IAnnotationAccessExtension.DEFAULT_LAYER;
+               
+               public ProblemAnnotation(IProblem problem, ICompilationUnit cu) {
+                       
+                       fProblem= problem;
+                       fCompilationUnit= cu;
+                       
+//                     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()) {
+                               setType(JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE);
+                               fLayer= WARNING_LAYER;
+                       } else if (fProblem.isError()) {
+                               setType(JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE);
+                               fLayer= ERROR_LAYER;
+                       } else {
+                               setType(JavaMarkerAnnotation.INFO_ANNOTATION_TYPE);
+                               fLayer= INFO_LAYER;
+                       }
+               }
+               
+               /*
+                * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
+                */
+               public int getLayer() {
+                       return fLayer;
+               }
+               
+               private void initializeImages() {
+                       // http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
+//                     if (!fQuickFixImagesInitialized) {
+//                             if (isProblem() && indicateQuixFixableProblems() && JavaCorrectionProcessor.hasCorrections(this)) { // no light bulb for tasks
+//                                     if (!fgQuickFixImagesInitialized) {
+//                                             fgQuickFixImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
+//                                             fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
+//                                             fgQuickFixImagesInitialized= true;
+//                                     }
+//                                     if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(getType()))
+//                                             fImage= fgQuickFixErrorImage;
+//                                     else
+//                                             fImage= fgQuickFixImage;
+//                             }
+//                             fQuickFixImagesInitialized= true;
+//                     }
+               }
+
+               private boolean indicateQuixFixableProblems() {
+                       return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
+               }
+                                       
+               /*
+                * @see Annotation#paint
+                */
+               public void paint(GC gc, Canvas canvas, Rectangle r) {
+                       initializeImages();
+                       if (fImage != null)
+                               drawImage(fImage, gc, canvas, r, SWT.CENTER, SWT.TOP);
+               }
+               
+               /*
+                * @see IJavaAnnotation#getImage(Display)
+                */
+               public Image getImage(Display display) {
+                       initializeImages();
+                       return fImage;
+               }
+               
+               /*
+                * @see IJavaAnnotation#getMessage()
+                */
+               public String getText() {
+                       return fProblem.getMessage();
+               }
+               
+               /*
+                * @see IJavaAnnotation#getArguments()
+                */
+               public String[] getArguments() {
+                       return isProblem() ? fProblem.getArguments() : null;
+               }
+
+               /*
+                * @see IJavaAnnotation#getId()
+                */
+               public int getId() {
+                       return fProblem.getID();
+               }
+
+               /*
+                * @see IJavaAnnotation#isProblem()
+                */
+               public boolean isProblem() {
+                       String type= getType();
+                       return  JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(type)  || 
+                                               JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(type) ||
+                                               SPELLING_ANNOTATION_TYPE.equals(type);
+               }
+               
+               /*
+                * @see IJavaAnnotation#hasOverlay()
+                */
+               public boolean hasOverlay() {
+                       return false;
+               }
+               
+               /*
+                * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
+                */
+               public IJavaAnnotation getOverlay() {
+                       return null;
+               }
+               
+               /*
+                * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
+                */
+               public void addOverlaid(IJavaAnnotation annotation) {
+                       if (fOverlaids == null)
+                               fOverlaids= new ArrayList(1);
+                       fOverlaids.add(annotation);
+               }
+
+               /*
+                * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
+                */
+               public void removeOverlaid(IJavaAnnotation annotation) {
+                       if (fOverlaids != null) {
+                               fOverlaids.remove(annotation);
+                               if (fOverlaids.size() == 0)
+                                       fOverlaids= null;
+                       }
+               }
+               
+               /*
+                * @see IJavaAnnotation#getOverlaidIterator()
+                */
+               public Iterator getOverlaidIterator() {
+                       if (fOverlaids != null)
+                               return fOverlaids.iterator();
+                       return null;
+               }
+               
+               /*
+                * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
+                */
+               public ICompilationUnit getCompilationUnit() {
+                       return fCompilationUnit;
+               }
+       }
+       
   /**
                 * Internal structure for mapping positions to some value. 
                 * The reason for this specific structure is that positions can
@@ -1120,60 +1245,116 @@ public class PHPDocumentProvider extends FileDocumentProvider {
   }
 
   /*
-   * @see AbstractDocumentProvider#resetDocument(Object)
+   * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doResetDocument(java.lang.Object, org.eclipse.core.runtime.IProgressMonitor)
    */
-  public void resetDocument(Object element) throws CoreException {
+  protected void doResetDocument(Object element, IProgressMonitor monitor) throws CoreException {
     if (element == null)
       return;
-
-    ElementInfo elementInfo = getElementInfo(element);
+    
+    ElementInfo elementInfo= getElementInfo(element);          
     if (elementInfo instanceof CompilationUnitInfo) {
-      CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
-
+      CompilationUnitInfo info= (CompilationUnitInfo) elementInfo;
+      
       IDocument document;
-      IStatus status = null;
-
+      IStatus status= null;
+      
       try {
-
-        ICompilationUnit original = (ICompilationUnit) info.fCopy.getOriginalElement();
-        IResource resource = original.getResource();
+        
+        ICompilationUnit original= (ICompilationUnit) info.fCopy.getOriginalElement();
+        IResource resource= original.getResource();
         if (resource instanceof IFile) {
-
-          IFile file = (IFile) resource;
-
+          
+          IFile file= (IFile) resource;
+          
           try {
-            refreshFile(file);
+            refreshFile(file, monitor);
           } catch (CoreException x) {
-            handleCoreException(x, PHPEditorMessages.getString("PHPDocumentProvider.error.resetDocument")); //$NON-NLS-1$
+            handleCoreException(x, PHPEditorMessages.getString("CompilationUnitDocumentProvider.error.resetDocument")); //$NON-NLS-1$
           }
-
-          IFileEditorInput input = new FileEditorInput(file);
-          document = super.createDocument(input);
-
+          
+          IFileEditorInput input= new FileEditorInput(file);
+          document= super.createDocument(input);
+          
         } else {
-          document = new Document();
+          document= createEmptyDocument();
         }
-
+        
       } catch (CoreException x) {
-        document = new Document();
-        status = x.getStatus();
+        document= createEmptyDocument();
+        status= x.getStatus();
       }
-
+      
       fireElementContentAboutToBeReplaced(element);
-
+      
       removeUnchangedElementListeners(element, info);
       info.fDocument.set(document.get());
-      info.fCanBeSaved = false;
-      info.fStatus = status;
+      info.fCanBeSaved= false;
+      info.fStatus= status;
       addUnchangedElementListeners(element, info);
-
+      
       fireElementContentReplaced(element);
       fireElementDirtyStateChanged(element, false);
-
+      
     } else {
-      super.resetDocument(element);
+      super.doResetDocument(element, monitor);
     }
   }
+  
+  /*
+   * @see AbstractDocumentProvider#resetDocument(Object)
+   */
+//  public void resetDocument(Object element) throws CoreException {
+//    if (element == null)
+//      return;
+//
+//    ElementInfo elementInfo = getElementInfo(element);
+//    if (elementInfo instanceof CompilationUnitInfo) {
+//      CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
+//
+//      IDocument document;
+//      IStatus status = null;
+//
+//      try {
+//
+//        ICompilationUnit original = (ICompilationUnit) info.fCopy.getOriginalElement();
+//        IResource resource = original.getResource();
+//        if (resource instanceof IFile) {
+//
+//          IFile file = (IFile) resource;
+//
+//          try {
+//            refreshFile(file);
+//          } catch (CoreException x) {
+//            handleCoreException(x, PHPEditorMessages.getString("PHPDocumentProvider.error.resetDocument")); //$NON-NLS-1$
+//          }
+//
+//          IFileEditorInput input = new FileEditorInput(file);
+//          document = super.createDocument(input);
+//
+//        } else {
+//          document = new Document();
+//        }
+//
+//      } catch (CoreException x) {
+//        document = new Document();
+//        status = x.getStatus();
+//      }
+//
+//      fireElementContentAboutToBeReplaced(element);
+//
+//      removeUnchangedElementListeners(element, info);
+//      info.fDocument.set(document.get());
+//      info.fCanBeSaved = false;
+//      info.fStatus = status;
+//      addUnchangedElementListeners(element, info);
+//
+//      fireElementContentReplaced(element);
+//      fireElementDirtyStateChanged(element, false);
+//
+//    } else {
+//      super.resetDocument(element);
+//    }
+//  }
   /**
         * Saves the content of the given document to the given element.
         * This is only performed when this provider initiated the save.