From aefe1c303fa9d3445ad4cfa1b7f06fbd1b29470c Mon Sep 17 00:00:00 2001 From: robekras Date: Sun, 15 Jan 2012 16:36:42 +0000 Subject: [PATCH] 1) Fixed issue #852: Correct matching of closing brace with the according opening brace and vice versa. --- .../phpdt/internal/ui/text/PHPPairMatcher.java | 199 ++++++++++++-------- 1 files changed, 122 insertions(+), 77 deletions(-) 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 index ab9abd5..b43ff4e 100644 --- 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 @@ -17,20 +17,22 @@ import org.eclipse.jface.text.source.ICharacterPairMatcher; * Helper class for match pairs of characters. */ public class PHPPairMatcher implements ICharacterPairMatcher { - protected char[] fPairs; + protected char[] fPairs; // Holds the brace pairs protected IDocument fDocument; - protected int fOffset; - - protected int fStartPos; - - protected int fEndPos; - + 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; } @@ -48,17 +50,23 @@ public class PHPPairMatcher implements ICharacterPairMatcher { } } + /** + * 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; - if (matchPairsAt() && fStartPos != fEndPos) - return new Region(fStartPos, fEndPos - fStartPos + 1); + if (matchPairsAt ()) { // If we found a pair of opening and closing braces + return new Region (nPosOpening, nPosClosing - nPosOpening + 1); + } return null; } @@ -79,108 +87,145 @@ public class PHPPairMatcher implements ICharacterPairMatcher { } } + /** + * 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 pairIndex1 = fPairs.length; - int pairIndex2 = fPairs.length; + 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) { + if (nPosClosing > -1) { // If we found a closing brace on current position (or before) 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; + 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 { + /** + * + * @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); - 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 - int stack = 1; - int c = fReader.read(); - while (c != PHPCodeReader.EOF) { - if (c == openingPeer && c != closingPeer) - stack++; - else if (c == closingPeer) - stack--; + 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) - return fReader.getOffset(); + if (stack == 0) { // If we found the matching closing brace + return fReader.getOffset (); // return the position of this closing brace + } - c = fReader.read(); + c = fReader.read (); // Read the next character } return -1; } - protected int searchForOpeningPeer(int offset, int openingPeer, - int closingPeer, IDocument document) throws IOException { - + /** + * + * @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; - int c = fReader.read(); - while (c != PHPCodeReader.EOF) { - if (c == closingPeer && c != openingPeer) - stack++; - else if (c == openingPeer) - stack--; + int stack = 1; // As we have already the opening brace + int c = fReader.read (); // Read character on position one before the closing brace - if (stack == 0) - return fReader.getOffset(); + 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(); + c = fReader.read (); // Read the previous character } return -1; -- 1.7.1