Syntax Highlighting Prefs work now / Automatic parse on save option available
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPContentOutlinePage.java
index dfc01fb..86b9977 100644 (file)
@@ -12,13 +12,11 @@ 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 org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.BadPositionCategoryException;
 import org.eclipse.jface.text.DefaultPositionUpdater;
@@ -32,17 +30,19 @@ 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.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;
 
 /**
- * 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$
   /**
    * A segment element.
    */
@@ -60,6 +60,12 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
     }
   };
 
+  protected static class SegmentComparator implements Comparator {
+    public int compare(Object o1, Object o2) {
+      return ((Segment) o1).name.compareToIgnoreCase(((Segment) o2).name);
+    }
+  }
+
   /**
    * Divides the editor's document into ten segments and provides elements for them.
    */
@@ -68,6 +74,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
     protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
     protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
     protected List fContent = new ArrayList(10);
+    protected List fVariables = new ArrayList(100);
 
     private String getIdentifier(String text, int firstIndex) {
       int i = firstIndex;
@@ -76,7 +83,9 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
       StringBuffer identifier = new StringBuffer();
       while (i < textLength) {
         c = text.charAt(i++);
-        if (Character.isJavaIdentifierPart(c)) {
+        if (Character.isJavaIdentifierPart(c) || (c == '$')) {
+          identifier.append(c);
+        } else if ((i == firstIndex + 1) && (c == '$')) {
           identifier.append(c);
         } else {
           return identifier.toString();
@@ -170,10 +179,10 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
 
         if (functionMode && Character.isJavaIdentifierPart((char) c)) {
           functionMode = false;
-          lastIndex = i-1;
+          lastIndex = i - 1;
           identifier = getIdentifier(text, lastIndex);
           try {
-            i += identifier.length()-1;
+            i += identifier.length() - 1;
             Position p = new Position(lastIndex, i - lastIndex);
             document.addPosition(SEGMENTS, p);
             fContent.add(new Segment(text.substring(lastIndex, i), p));
@@ -187,11 +196,17 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
           identifier = getIdentifier(text, i - 1);
           if (identifier.equals("function")) {
             functionMode = true;
-            i+=8;
+            i += 8;
           }
+        } else if (c == '$') {
+          // get the variable name
+          identifier = getIdentifier(text, i - 1);
+          fVariables.add(identifier);
         }
 
       }
+      Collections.sort(fContent, new SegmentComparator());
+      Collections.sort(fVariables);
 
       //                       for (int line = 0; line < lines; line += increment) {
       //
@@ -230,6 +245,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
       }
 
       fContent.clear();
+      fVariables.clear();
 
       if (newInput != null) {
         IDocument document = fDocumentProvider.getDocument(newInput);
@@ -250,6 +266,10 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
         fContent.clear();
         fContent = null;
       }
+      if (fVariables != null) {
+        fVariables.clear();
+        fVariables = null;
+      }
     }
 
     /*
@@ -266,6 +286,12 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
       return fContent.toArray();
     }
 
+    /**
+     * returns all PHP variables
+     */
+    public Object[] getVariables() {
+      return fVariables.toArray();
+    }
     /*
      * @see ITreeContentProvider#hasChildren(Object)
      */