1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / PHPPairMatcher.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/PHPPairMatcher.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/PHPPairMatcher.java
new file mode 100644 (file)
index 0000000..c23d428
--- /dev/null
@@ -0,0 +1,234 @@
+package net.sourceforge.phpdt.internal.ui.text;
+
+/*
+ * (c) Copyright IBM Corp. 2000, 2001.
+ * All Rights Reserved.
+ */
+
+import java.io.IOException;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.source.ICharacterPairMatcher;
+
+/**
+ * Helper class for match pairs of characters.
+ */
+public class PHPPairMatcher implements ICharacterPairMatcher {
+       protected char[]    fPairs;           // Holds the brace pairs
+
+       protected IDocument fDocument;
+
+       protected int       fOffset;          // The current text position from which we search
+       protected int       nPosOpening;      // The position of the opening brace
+       protected int       nPosClosing;      // The position of the closing brace
+       protected int       fAnchor;
+
+       protected PHPCodeReader fReader = new PHPCodeReader();
+
+       /**
+        *
+        * @param pairs The array of opening and closing braces we need for searching the brace matching
+        */
+
+       public PHPPairMatcher (char[] pairs) {
+               fPairs = pairs;
+       }
+
+       /*
+        * @see org.eclipse.jface.text.source.ICharacterPairMatcher#clear()
+        */
+       public void clear() {
+               if (fReader != null) {
+                       try {
+                               fReader.close();
+                       } catch (IOException x) {
+                               // ignore
+                       }
+               }
+       }
+
+       /**
+        * Try to find a matching pair of opening and closing braces
+        *
+        * @return  Returns a region if a matching pair was found.
+        */
+
+       public IRegion match (IDocument document, int offset) {
+               fOffset = offset;
+
+               if (fOffset < 0) {
+                       return null;
+               }
+
+               fDocument = document;
+
+               if (matchPairsAt ()) {                                                                          // If we found a pair of opening and closing braces
+                       return new Region (nPosOpening, nPosClosing - nPosOpening + 1);
+               }
+
+               return null;
+       }
+
+       public int getAnchor() {
+               return fAnchor;
+       }
+
+       public void dispose() {
+               fDocument = null;
+               if (fReader != null) {
+                       try {
+                               fReader.close();
+                       } catch (IOException x) {
+                               // ignore
+                       }
+                       fReader = null;
+               }
+       }
+
+       /**
+        * Take the character from the current text position, checks for opening or closing braces and search for
+        * the corresponding opening or closing brace
+        *
+        * @return True if a pair was found
+        */
+       protected boolean matchPairsAt() {
+               int   i;
+               int   nIndexOpen  = fPairs.length;        // Opening index
+               int   nIndexClose = fPairs.length;
+               char  cCurrentChar;                       // The character from the current text position
+               char  cPrevChar;                          // The character from the previouis text position, just
+                                                         // for the case the user pointed the cursor after the opening or closing brace
+
+               nPosOpening = -1;
+               nPosClosing = -1;
+
+               // get the chars preceding and following the start position
+               try {
+            cCurrentChar = fDocument.getChar (fOffset);
+                       cPrevChar    = fDocument.getChar (Math.max (fOffset - 1, 0));
+
+                       // search for opening peer character next to the activation point
+                       for (i = 0; i < fPairs.length; i = i + 2) {     // The opening brace is on even indexes
+                               if (cCurrentChar == fPairs[i]) {            // If the current character matches an opening brace
+                                       nPosOpening  = fOffset;                 // Remember the character position
+                                       nIndexOpen   = i;                       // and remember the opening brace for which we search the closing one
+                               }
+                               else if (cPrevChar == fPairs[i]) {          // If the current character is not an opening, but the previous character is an opening brace
+                                       nPosOpening  = fOffset - 1;             // Remember the character position
+                                       nIndexOpen   = i;                       // and remember the opening brace for which we search the closing one
+                               }
+                       }
+
+                       // search for closing peer character next to the activation point
+
+                       if (nPosOpening < 0) {                              // If we didn't find an opening brace
+                               for (i = 1; i < fPairs.length; i = i + 2) {     // The closing brace is on odd indexes
+                                       if (cCurrentChar == fPairs[i]) {            // If the current character matches an closing brace
+                                               nPosClosing  = fOffset;                 // Remember the character position
+                                               nIndexClose  = i;                       // and remember the opening brace for which we search the closing one
+                                       }
+                                       else if (cPrevChar == fPairs[i]) {          // If the current character is not an opening, but the previous character is an opening brace
+                                               nPosClosing  = fOffset - 1;             // Remember the character position
+                                               nIndexClose  = i;                       // and remember the opening brace for which we search the closing one
+                                       }
+                               }
+                       }
+
+                       if (nPosClosing > -1) {                             // If we found a closing brace on current position (or before)
+                               fAnchor     = RIGHT;
+                               nPosOpening = searchForOpeningPeer (nPosClosing, fPairs[nIndexClose - 1], fPairs[nIndexClose], fDocument);
+
+                               return (nPosOpening > -1);                      // If we found a opening brace, return true
+                       }
+                       else if (nPosOpening > -1) {                        // If we found a opening brace on current position (or before)
+                               fAnchor     = LEFT;
+                               nPosClosing = searchForClosingPeer (nPosOpening, fPairs[nIndexOpen], fPairs[nIndexOpen + 1], fDocument);
+
+                               return (nPosClosing > -1);                      // If we found an closing brace for this opening brace return true
+                       }
+               }
+               catch (BadLocationException x) {
+               }
+               catch (IOException x) {
+               }
+
+               return false;
+       }
+
+       /**
+        *
+        * @param offset               The search start position
+        * @param openingPeer          The opening brace we had found
+        * @param closingPeer          The closing brace we search for
+        * @param document             The document we currently are in
+        *
+        * @return
+        *
+        * @throws IOException
+        */
+       protected int searchForClosingPeer (int offset, char openingPeer,
+                                                   char closingPeer, IDocument document) throws IOException {
+               fReader.configureForwardReader (document, offset + 1, document.getLength(), true, true);
+
+               int stack = 1;                                  // As we have already the opening brace
+               int c     = fReader.read ();                            // Read character on position one after the opening brace
+
+               while (c != PHPCodeReader.EOF) {                // As long as we are not at the end
+                       if ((c == openingPeer) &&                   // If character is opening brace again
+                           (c != closingPeer)) {                   // and not a closing brace (how could it be?)
+                               stack++;                                // put it on stack
+                       }
+                       else if (c == closingPeer) {                // If it's a closing brace
+                               stack--;                                // Decrement level counter
+                       }
+
+                       if (stack == 0) {                           // If we found the matching closing brace
+                               return fReader.getOffset ();            // return the position of this closing brace
+                       }
+
+                       c = fReader.read ();                        // Read the next character
+               }
+
+               return -1;
+       }
+
+       /**
+        *
+        * @param offset               The search start position
+        * @param openingPeer          The opening brace we search for
+        * @param closingPeer          The closing brace we search for
+        * @param document             The document we currently are in
+        *
+        * @return
+        *
+        * @throws IOException
+        */
+       protected int searchForOpeningPeer (int offset, char openingPeer,
+                                               char closingPeer, IDocument document) throws IOException {
+               fReader.configureBackwardReader (document, offset, true, true);
+
+               int stack = 1;                                  // As we have already the opening brace
+               int c     = fReader.read ();                    // Read character on position one before the closing brace
+
+               while (c != PHPCodeReader.EOF) {                // As long as we are not at the start of text
+                       if ((c == closingPeer) &&                   // If character is closing brace again
+                           (c != openingPeer)) {                   // and not a opening brace (how could it be?)
+                               stack++;                                // put it on stack
+                       }
+                       else if (c == openingPeer) {                // If it's a opening brace
+                               stack--;                                // Decrement level counter
+                       }
+
+                       if (stack == 0) {                           // If we found the matching closing brace
+                               return fReader.getOffset ();            // return the position of this closing brace
+                       }
+
+                       c = fReader.read ();                        // Read the previous character
+               }
+
+               return -1;
+       }
+}