* Helper class for match pairs of characters.
*/
public class PHPPairMatcher implements ICharacterPairMatcher {
-
-
- public static final int LEFT= 1;
- public static final int RIGHT= 2;
+ protected char[] fPairs; // Holds the brace pairs
-
- protected char[] fPairs;
protected IDocument fDocument;
- protected int fOffset;
-
- protected int fStartPos;
- protected int fEndPos;
- protected int fAnchor;
-
- protected PHPCodeReader fReader= new PHPCodeReader();
-
-
- public PHPPairMatcher(char[] pairs) {
- fPairs= pairs;
+
+ 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 IRegion match(IDocument document, int offset) {
- fOffset= offset;
+ /**
+ * 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)
+ if (fOffset < 0) {
return null;
+ }
- fDocument= document;
+ fDocument = document;
+
+ if (matchPairsAt ()) { // If we found a pair of opening and closing braces
+ return new Region (nPosOpening, nPosClosing - nPosOpening + 1);
+ }
- if (matchPairsAt() && fStartPos != fEndPos)
- return new Region(fStartPos, fEndPos - fStartPos + 1);
-
return null;
}
-
+
public int getAnchor() {
return fAnchor;
}
-
+
public void dispose() {
- fDocument= null;
+ fDocument = null;
if (fReader != null) {
try {
fReader.close();
} catch (IOException x) {
// ignore
}
- fReader= null;
+ fReader = null;
}
}
-
- protected boolean matchPairsAt() {
- int i;
- int pairIndex1= fPairs.length;
- int pairIndex2= fPairs.length;
+ /**
+ * 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
- fStartPos= -1;
- fEndPos= -1;
+ nPosOpening = -1;
+ nPosClosing = -1;
// get the chars preceding and following the start position
try {
-
- char prevChar= fDocument.getChar(Math.max(fOffset - 1, 0));
- char nextChar= fDocument.getChar(fOffset);
+ 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) {
- if (nextChar == fPairs[i]) {
- fStartPos= fOffset;
- pairIndex1= i;
- } else if (prevChar == fPairs[i]) {
- fStartPos= fOffset - 1;
- pairIndex1= i;
+ 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
- for (i= 1; i < fPairs.length; i= i + 2) {
- if (prevChar == fPairs[i]) {
- fEndPos= fOffset - 1;
- pairIndex2= i;
- } else if (nextChar == fPairs[i]) {
- fEndPos= fOffset;
- pairIndex2= i;
+
+ 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 (fEndPos > -1) {
- fAnchor= RIGHT;
- fStartPos= searchForOpeningPeer(fEndPos, fPairs[pairIndex2 - 1], fPairs[pairIndex2], fDocument);
- if (fStartPos > -1)
- return true;
- else
- fEndPos= -1;
- } else if (fStartPos > -1) {
- fAnchor= LEFT;
- fEndPos= searchForClosingPeer(fStartPos, fPairs[pairIndex1], fPairs[pairIndex1 + 1], fDocument);
- if (fEndPos > -1)
- return true;
- else
- fStartPos= -1;
+ 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);
- } catch (BadLocationException x) {
- } catch (IOException x) {
+ return (nPosClosing > -1); // If we found an closing brace for this opening brace return true
+ }
+ }
+ catch (BadLocationException x) {
+ }
+ catch (IOException x) {
}
return false;
}
-
- protected int searchForClosingPeer(int offset, int openingPeer, int closingPeer, IDocument document) throws IOException {
-
- fReader.configureForwardReader(document, offset + 1, document.getLength(), true, true);
-
- int stack= 1;
- int c= fReader.read();
- while (c != PHPCodeReader.EOF) {
- if (c == openingPeer && c != closingPeer)
- stack++;
- else if (c == closingPeer)
- stack--;
-
- if (stack == 0)
- return fReader.getOffset();
-
- c= fReader.read();
+
+ /**
+ *
+ * @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;
+
+ return -1;
}
-
- protected int searchForOpeningPeer(int offset, int openingPeer, int closingPeer, IDocument document) throws IOException {
-
- fReader.configureBackwardReader(document, offset, true, true);
-
- int stack= 1;
- int c= fReader.read();
- while (c != PHPCodeReader.EOF) {
- if (c == closingPeer && c != openingPeer)
- stack++;
- else if (c == openingPeer)
- stack--;
-
- if (stack == 0)
- return fReader.getOffset();
-
- c= fReader.read();
+
+ /**
+ *
+ * @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;
}
}