improved syntax highligthing
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPContentOutlinePage.java
index d7ab260..e081aea 100644 (file)
@@ -51,8 +51,8 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                public Position position;
 
                public Segment(String name, Position position) {
-                       this.name= name;
-                       this.position= position;
+                       this.name = name;
+                       this.position = position;
                }
 
                public String toString() {
@@ -65,34 +65,56 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
         */
        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 final static String SEGMENTS = "__php_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;
+                       int lines = document.getNumberOfLines();
+                       int increment = Math.max(Math.round((float) (lines / 10)), 10);
+                       
+      String text = document.get();
+                       int lastIndex = 0;
+      int i=0;
+                       lastIndex = text.indexOf("function ", lastIndex);
+                       while (lastIndex > 0) {
 
                                try {
-
-                                       int offset= document.getLineOffset(line);
-                                       int end= document.getLineOffset(line + length);
-                                       length= end - offset;
-                                       Position p= new Position(offset, length);
+          i = lastIndex+9;
+          while ((i<text.length())&&Character.isJavaIdentifierPart(text.charAt(i))) {
+            i++;
+          } 
+                                       Position p = new Position(lastIndex, i-lastIndex);
                                        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) {
+                                       fContent.add(new Segment(text.substring(lastIndex,i), p));
+     //     MessageFormat.format("function", new Object[] { new Integer(lastIndex)}), p)); //$NON-NLS-1$
+                                       lastIndex = text.indexOf("function", lastIndex+1);
+                               } catch (BadLocationException e) {
+                               } catch (BadPositionCategoryException e) {
                                }
+
                        }
+
+                       //                      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) {
+                       //                              }
+                       //                      }
                }
 
                /*
@@ -100,7 +122,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                 */
                public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
                        if (oldInput != null) {
-                               IDocument document= fDocumentProvider.getDocument(oldInput);
+                               IDocument document = fDocumentProvider.getDocument(oldInput);
                                if (document != null) {
                                        try {
                                                document.removePositionCategory(SEGMENTS);
@@ -113,7 +135,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                        fContent.clear();
 
                        if (newInput != null) {
-                               IDocument document= fDocumentProvider.getDocument(newInput);
+                               IDocument document = fDocumentProvider.getDocument(newInput);
                                if (document != null) {
                                        document.addPositionCategory(SEGMENTS);
                                        document.addPositionUpdater(fPositionUpdater);
@@ -129,7 +151,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                public void dispose() {
                        if (fContent != null) {
                                fContent.clear();
-                               fContent= null;
+                               fContent = null;
                        }
                }
 
@@ -182,10 +204,10 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
         */
        public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
                super();
-               fDocumentProvider= provider;
-               fTextEditor= editor;
+               fDocumentProvider = provider;
+               fTextEditor = editor;
        }
-       
+
        /* (non-Javadoc)
         * Method declared on ContentOutlinePage
         */
@@ -193,7 +215,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
 
                super.createControl(parent);
 
-               TreeViewer viewer= getTreeViewer();
+               TreeViewer viewer = getTreeViewer();
                viewer.setContentProvider(new ContentProvider());
                viewer.setLabelProvider(new LabelProvider());
                viewer.addSelectionChangedListener(this);
@@ -201,7 +223,7 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                if (fInput != null)
                        viewer.setInput(fInput);
        }
-       
+
        /* (non-Javadoc)
         * Method declared on ContentOutlinePage
         */
@@ -209,13 +231,13 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
 
                super.selectionChanged(event);
 
-               ISelection selection= event.getSelection();
+               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();
+                       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) {
@@ -223,23 +245,23 @@ public class PHPContentOutlinePage extends ContentOutlinePage {
                        }
                }
        }
-       
+
        /**
         * Sets the input of the outline page
         */
        public void setInput(Object input) {
-               fInput= input;
+               fInput = input;
                update();
        }
-       
+
        /**
         * Updates the outline page.
         */
        public void update() {
-               TreeViewer viewer= getTreeViewer();
+               TreeViewer viewer = getTreeViewer();
 
                if (viewer != null) {
-                       Control control= viewer.getControl();
+                       Control control = viewer.getControl();
                        if (control != null && !control.isDisposed()) {
                                control.setRedraw(false);
                                viewer.setInput(fInput);