Improved PHPCodeReader for "Go to Matching Bracket" Action
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPContentOutlinePage.java
index d7ab260..bdc86d8 100644 (file)
@@ -12,19 +12,23 @@ Contributors:
     Klaus Hartlage - www.eclipseproject.de
 **********************************************************************/
 
-import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
+import java.util.TreeSet;
 
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
+import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
+import net.sourceforge.phpdt.internal.compiler.parser.PHPSegment;
+import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren;
+import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
-import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.BadPositionCategoryException;
 import org.eclipse.jface.text.DefaultPositionUpdater;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IPositionUpdater;
-import org.eclipse.jface.text.Position;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.ITreeContentProvider;
@@ -32,220 +36,295 @@ import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
-
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 import org.eclipse.ui.texteditor.ITextEditor;
 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
+import test.PHPParserSuperclass;
+import test.PHPParserManager;
 
 /**
- * A content outline page which always represents the content of the
- * connected editor in 10 segments.
+ * A content outline page which always represents the functions of the
+ * connected PHPEditor.
  */
 public class PHPContentOutlinePage extends ContentOutlinePage {
+  private static final String ERROR = "error"; //$NON-NLS-1$
+  private static final String WARNING = "warning"; //$NON-NLS-1$
+
+  protected static class SegmentComparator implements Comparator {
+    public int compare(Object o1, Object o2) {
+      if (o1 instanceof PHPSegmentWithChildren && !(o2 instanceof PHPSegmentWithChildren)) {
+        return 1;
+      }
+      if (o2 instanceof PHPSegmentWithChildren && !(o1 instanceof PHPSegmentWithChildren)) {
+        return -1;
+      }
+      return ((PHPSegment) o1).toString().compareToIgnoreCase(((PHPSegment) o2).toString());
+    }
+  }
+
+  /**
+   * Divides the editor's document into ten segments and provides elements for them.
+   */
+  protected class ContentProvider implements ITreeContentProvider {
+
+    protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
+    protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
+    protected List fContent = new ArrayList(10);
+    protected TreeSet fVariables = new TreeSet();
+
+    //    private String getIdentifier(String text, int firstIndex) {
+    //      int i = firstIndex;
+    //      char c;
+    //      int textLength = text.length();
+    //      StringBuffer identifier = new StringBuffer();
+    //      while (i < textLength) {
+    //        c = text.charAt(i++);
+    //        if (Scanner.isPHPIdentifierPart(c) || (c == '$')) {
+    //          identifier.append(c);
+    //        } else if ((i == firstIndex + 1) && (c == '$')) {
+    //          identifier.append(c);
+    //        } else {
+    //          return identifier.toString();
+    //        }
+    //      }
+    //      return null;
+    //    }
+
+    protected void parse(IDocument document) {
+
+      //      int lines = document.getNumberOfLines();
+      //      int increment = Math.max(Math.round((float) (lines / 10)), 10);
+
+      String name;
+      int index;
+      String text = document.get();
+      PHPParserSuperclass parser = PHPParserManager.getParser(null);
+
+      PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
+      fVariables = outlineInfo.getVariables();
+
+      PHPSegmentWithChildren declarations = outlineInfo.getDeclarations();
+      PHPSegment temp;
+      for (int i = 0; i < declarations.size(); i++) {
+        temp = declarations.get(i);
+        fContent.add(temp);
+      }
+      Collections.sort(fContent, new SegmentComparator());
+
+    }
+
+    /*
+     * @see IContentProvider#inputChanged(Viewer, Object, Object)
+     */
+    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+      if (oldInput != null) {
+        IDocument document = fDocumentProvider.getDocument(oldInput);
+        if (document != null) {
+          try {
+            document.removePositionCategory(SEGMENTS);
+          } catch (BadPositionCategoryException x) {
+          }
+          document.removePositionUpdater(fPositionUpdater);
+        }
+      }
+
+      fContent.clear();
+      fVariables.clear();
+
+      if (newInput != null) {
+        IDocument document = fDocumentProvider.getDocument(newInput);
+        if (document != null) {
+          document.addPositionCategory(SEGMENTS);
+          document.addPositionUpdater(fPositionUpdater);
+
+          parse(document);
+        }
+      }
+    }
+
+    /*
+     * @see IContentProvider#dispose
+     */
+    public void dispose() {
+      if (fContent != null) {
+        fContent.clear();
+        fContent = null;
+      }
+      if (fVariables != null) {
+        fVariables.clear();
+        fVariables = null;
+      }
+    }
+
+    /*
+     * @see IContentProvider#isDeleted(Object)
+     */
+    public boolean isDeleted(Object element) {
+      return false;
+    }
+
+    /**
+     * returns all PHP variables
+     */
+    public Object[] getVariables() {
+      return fVariables.toArray();
+    }
+
+    /*
+     * @see IStructuredContentProvider#getElements(Object)
+     */
+    public Object[] getElements(Object element) {
+      return fContent.toArray();
+    }
+
+    /*
+     * @see ITreeContentProvider#hasChildren(Object)
+     */
+    public boolean hasChildren(Object element) {
+      if (element instanceof PHPSegmentWithChildren) {
+        return !((PHPSegmentWithChildren) element).getList().isEmpty();
+      }
+      return element == fInput;
+    }
+
+    /*
+     * @see ITreeContentProvider#getParent(Object)
+     */
+    public Object getParent(Object element) {
+      if (element instanceof PHPSegment) {
+        return ((PHPSegment) element).getParent();
+      }
+      return null;
+    }
+
+    /*
+     * @see ITreeContentProvider#getChildren(Object)
+     */
+    public Object[] getChildren(Object element) {
+      if (element == fInput)
+        return fContent.toArray();
+      if (element instanceof PHPSegmentWithChildren)
+        return ((PHPSegmentWithChildren) element).getList().toArray();
+      return new Object[0];
+    }
+  };
+
+  protected class OutlineLabelProvider extends LabelProvider {
+    private ImageDescriptorRegistry fRegistry;
+
+    public OutlineLabelProvider() {
+      fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
+      ;
+    }
+    /**
+    * The <code>LabelProvider</code> implementation of this 
+    * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may 
+    * override.
+    */
+    public Image getImage(Object element) {
+      if (element instanceof PHPSegment) {
+        ImageDescriptor descriptor = ((PHPSegment) element).getImage();
+        return fRegistry.get(descriptor);
+      }
+      return null;
+    }
+  }
+
+  protected Object fInput;
+  protected IDocumentProvider fDocumentProvider;
+  protected ITextEditor fTextEditor;
+  protected PHPEditor fEditor;
+  protected ContentProvider contentProvider;
+
+  /**
+   * Creates a content outline page using the given provider and the given editor.
+   */
+  public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
+    super();
+    contentProvider = null;
+    fDocumentProvider = provider;
+    fTextEditor = editor;
+    if (editor instanceof PHPEditor)
+      fEditor = (PHPEditor) editor;
+  }
+
+  /* (non-Javadoc)
+   * Method declared on ContentOutlinePage
+   */
+  public void createControl(Composite parent) {
+
+    super.createControl(parent);
+
+    TreeViewer viewer = getTreeViewer();
+
+    contentProvider = new ContentProvider();
+    viewer.setContentProvider(contentProvider);
+    viewer.setLabelProvider(new OutlineLabelProvider());
+
+    viewer.addSelectionChangedListener(this);
+
+    if (fInput != null)
+      viewer.setInput(fInput);
+  }
+
+  /* (non-Javadoc)
+   * Method declared on ContentOutlinePage
+   */
+  public void selectionChanged(SelectionChangedEvent event) {
+
+    super.selectionChanged(event);
+
+    ISelection selection = event.getSelection();
+    if (selection.isEmpty())
+      fTextEditor.resetHighlightRange();
+    else {
+      PHPSegment segment = (PHPSegment) ((IStructuredSelection) selection).getFirstElement();
+      int start = segment.getPosition().getOffset();
+      int length = segment.getPosition().getLength();
+      try {
+        fTextEditor.setHighlightRange(start, length, true);
+      } catch (IllegalArgumentException x) {
+        fTextEditor.resetHighlightRange();
+      }
+    }
+  }
+
+  /**
+   * Sets the input of the outline page
+   */
+  public void setInput(Object input) {
+    fInput = input;
+    update();
+  }
+
+  /**
+   * Updates the outline page.
+   */
+  public void update() {
+    TreeViewer viewer = getTreeViewer();
+
+    if (viewer != null) {
+      Control control = viewer.getControl();
+      if (control != null && !control.isDisposed()) {
+        control.setRedraw(false);
+        viewer.setInput(fInput);
+        viewer.expandAll();
+        control.setRedraw(true);
+      }
+    }
+  }
+  
+  public Object[] getVariables() {
+    if (contentProvider != null) {
+      return contentProvider.getVariables();
+    }
+    return null;
+  }
+  //  public ContentProvider getContentProvider() {
+  //    return contentProvider;
+  //  }
 
-       /**
-        * A segment element.
-        */
-       protected static class Segment {
-               public String name;
-               public Position position;
-
-               public Segment(String name, Position position) {
-                       this.name= name;
-                       this.position= position;
-               }
-
-               public String toString() {
-                       return name;
-               }
-       };
-
-       /**
-        * Divides the editor's document into ten segments and provides elements for them.
-        */
-       protected class ContentProvider implements ITreeContentProvider {
-
-               protected final static String SEGMENTS= "__java_segments"; //$NON-NLS-1$
-               protected IPositionUpdater fPositionUpdater= new DefaultPositionUpdater(SEGMENTS);
-               protected List fContent= new ArrayList(10);
-
-               protected void parse(IDocument document) {
-
-                       int lines= document.getNumberOfLines();
-                       int increment= Math.max(Math.round((float) (lines / 10)), 10);
-
-                       for (int line= 0; line < lines; line += increment) {
-
-                               int length= increment;
-                               if (line + increment > lines)
-                                       length= lines - line;
-
-                               try {
-
-                                       int offset= document.getLineOffset(line);
-                                       int end= document.getLineOffset(line + length);
-                                       length= end - offset;
-                                       Position p= new Position(offset, length);
-                                       document.addPosition(SEGMENTS, p);
-                                       fContent.add(new Segment(MessageFormat.format(PHPEditorMessages.getString("OutlinePage.segment.title_pattern"), new Object[] { new Integer(offset) }), p)); //$NON-NLS-1$
-
-                               } catch (BadPositionCategoryException x) {
-                               } catch (BadLocationException x) {
-                               }
-                       }
-               }
-
-               /*
-                * @see IContentProvider#inputChanged(Viewer, Object, Object)
-                */
-               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-                       if (oldInput != null) {
-                               IDocument document= fDocumentProvider.getDocument(oldInput);
-                               if (document != null) {
-                                       try {
-                                               document.removePositionCategory(SEGMENTS);
-                                       } catch (BadPositionCategoryException x) {
-                                       }
-                                       document.removePositionUpdater(fPositionUpdater);
-                               }
-                       }
-
-                       fContent.clear();
-
-                       if (newInput != null) {
-                               IDocument document= fDocumentProvider.getDocument(newInput);
-                               if (document != null) {
-                                       document.addPositionCategory(SEGMENTS);
-                                       document.addPositionUpdater(fPositionUpdater);
-
-                                       parse(document);
-                               }
-                       }
-               }
-
-               /*
-                * @see IContentProvider#dispose
-                */
-               public void dispose() {
-                       if (fContent != null) {
-                               fContent.clear();
-                               fContent= null;
-                       }
-               }
-
-               /*
-                * @see IContentProvider#isDeleted(Object)
-                */
-               public boolean isDeleted(Object element) {
-                       return false;
-               }
-
-               /*
-                * @see IStructuredContentProvider#getElements(Object)
-                */
-               public Object[] getElements(Object element) {
-                       return fContent.toArray();
-               }
-
-               /*
-                * @see ITreeContentProvider#hasChildren(Object)
-                */
-               public boolean hasChildren(Object element) {
-                       return element == fInput;
-               }
-
-               /*
-                * @see ITreeContentProvider#getParent(Object)
-                */
-               public Object getParent(Object element) {
-                       if (element instanceof Segment)
-                               return fInput;
-                       return null;
-               }
-
-               /*
-                * @see ITreeContentProvider#getChildren(Object)
-                */
-               public Object[] getChildren(Object element) {
-                       if (element == fInput)
-                               return fContent.toArray();
-                       return new Object[0];
-               }
-       };
-
-       protected Object fInput;
-       protected IDocumentProvider fDocumentProvider;
-       protected ITextEditor fTextEditor;
-
-       /**
-        * Creates a content outline page using the given provider and the given editor.
-        */
-       public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
-               super();
-               fDocumentProvider= provider;
-               fTextEditor= editor;
-       }
-       
-       /* (non-Javadoc)
-        * Method declared on ContentOutlinePage
-        */
-       public void createControl(Composite parent) {
-
-               super.createControl(parent);
-
-               TreeViewer viewer= getTreeViewer();
-               viewer.setContentProvider(new ContentProvider());
-               viewer.setLabelProvider(new LabelProvider());
-               viewer.addSelectionChangedListener(this);
-
-               if (fInput != null)
-                       viewer.setInput(fInput);
-       }
-       
-       /* (non-Javadoc)
-        * Method declared on ContentOutlinePage
-        */
-       public void selectionChanged(SelectionChangedEvent event) {
-
-               super.selectionChanged(event);
-
-               ISelection selection= event.getSelection();
-               if (selection.isEmpty())
-                       fTextEditor.resetHighlightRange();
-               else {
-                       Segment segment= (Segment) ((IStructuredSelection) selection).getFirstElement();
-                       int start= segment.position.getOffset();
-                       int length= segment.position.getLength();
-                       try {
-                               fTextEditor.setHighlightRange(start, length, true);
-                       } catch (IllegalArgumentException x) {
-                               fTextEditor.resetHighlightRange();
-                       }
-               }
-       }
-       
-       /**
-        * Sets the input of the outline page
-        */
-       public void setInput(Object input) {
-               fInput= input;
-               update();
-       }
-       
-       /**
-        * Updates the outline page.
-        */
-       public void update() {
-               TreeViewer viewer= getTreeViewer();
-
-               if (viewer != null) {
-                       Control control= viewer.getControl();
-                       if (control != null && !control.isDisposed()) {
-                               control.setRedraw(false);
-                               viewer.setInput(fInput);
-                               viewer.expandAll();
-                               control.setRedraw(true);
-                       }
-               }
-       }
 }