1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.io.IOException;
10 import org.eclipse.jface.text.BadLocationException;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.text.IRegion;
13 import org.eclipse.jface.text.Region;
14 import org.eclipse.jface.text.source.ICharacterPairMatcher;
17 * Helper class for match pairs of characters.
19 public class PHPPairMatcher implements ICharacterPairMatcher {
20 protected char[] fPairs; // Holds the brace pairs
22 protected IDocument fDocument;
24 protected int fOffset; // The current text position from which we search
25 protected int nPosOpening; // The position of the opening brace
26 protected int nPosClosing; // The position of the closing brace
27 protected int fAnchor;
29 protected PHPCodeReader fReader = new PHPCodeReader();
33 * @param pairs The array of opening and closing braces we need for searching the brace matching
36 public PHPPairMatcher (char[] pairs) {
41 * @see org.eclipse.jface.text.source.ICharacterPairMatcher#clear()
44 if (fReader != null) {
47 } catch (IOException x) {
54 * Try to find a matching pair of opening and closing braces
56 * @return Returns a region if a matching pair was found.
59 public IRegion match (IDocument document, int offset) {
68 if (matchPairsAt ()) { // If we found a pair of opening and closing braces
69 return new Region (nPosOpening, nPosClosing - nPosOpening + 1);
75 public int getAnchor() {
79 public void dispose() {
81 if (fReader != null) {
84 } catch (IOException x) {
92 * Take the character from the current text position, checks for opening or closing braces and search for
93 * the corresponding opening or closing brace
95 * @return True if a pair was found
97 protected boolean matchPairsAt() {
99 int nIndexOpen = fPairs.length; // Opening index
100 int nIndexClose = fPairs.length;
101 char cCurrentChar; // The character from the current text position
102 char cPrevChar; // The character from the previouis text position, just
103 // for the case the user pointed the cursor after the opening or closing brace
108 // get the chars preceding and following the start position
110 cCurrentChar = fDocument.getChar (fOffset);
111 cPrevChar = fDocument.getChar (Math.max (fOffset - 1, 0));
113 // search for opening peer character next to the activation point
114 for (i = 0; i < fPairs.length; i = i + 2) { // The opening brace is on even indexes
115 if (cCurrentChar == fPairs[i]) { // If the current character matches an opening brace
116 nPosOpening = fOffset; // Remember the character position
117 nIndexOpen = i; // and remember the opening brace for which we search the closing one
119 else if (cPrevChar == fPairs[i]) { // If the current character is not an opening, but the previous character is an opening brace
120 nPosOpening = fOffset - 1; // Remember the character position
121 nIndexOpen = i; // and remember the opening brace for which we search the closing one
125 // search for closing peer character next to the activation point
127 if (nPosOpening < 0) { // If we didn't find an opening brace
128 for (i = 1; i < fPairs.length; i = i + 2) { // The closing brace is on odd indexes
129 if (cCurrentChar == fPairs[i]) { // If the current character matches an closing brace
130 nPosClosing = fOffset; // Remember the character position
131 nIndexClose = i; // and remember the opening brace for which we search the closing one
133 else if (cPrevChar == fPairs[i]) { // If the current character is not an opening, but the previous character is an opening brace
134 nPosClosing = fOffset - 1; // Remember the character position
135 nIndexClose = i; // and remember the opening brace for which we search the closing one
140 if (nPosClosing > -1) { // If we found a closing brace on current position (or before)
142 nPosOpening = searchForOpeningPeer (nPosClosing, fPairs[nIndexClose - 1], fPairs[nIndexClose], fDocument);
144 return (nPosOpening > -1); // If we found a opening brace, return true
146 else if (nPosOpening > -1) { // If we found a opening brace on current position (or before)
148 nPosClosing = searchForClosingPeer (nPosOpening, fPairs[nIndexOpen], fPairs[nIndexOpen + 1], fDocument);
150 return (nPosClosing > -1); // If we found an closing brace for this opening brace return true
153 catch (BadLocationException x) {
155 catch (IOException x) {
163 * @param offset The search start position
164 * @param openingPeer The opening brace we had found
165 * @param closingPeer The closing brace we search for
166 * @param document The document we currently are in
170 * @throws IOException
172 protected int searchForClosingPeer (int offset, char openingPeer,
173 char closingPeer, IDocument document) throws IOException {
174 fReader.configureForwardReader (document, offset + 1, document.getLength(), true, true);
176 int stack = 1; // As we have already the opening brace
177 int c = fReader.read (); // Read character on position one after the opening brace
179 while (c != PHPCodeReader.EOF) { // As long as we are not at the end
180 if ((c == openingPeer) && // If character is opening brace again
181 (c != closingPeer)) { // and not a closing brace (how could it be?)
182 stack++; // put it on stack
184 else if (c == closingPeer) { // If it's a closing brace
185 stack--; // Decrement level counter
188 if (stack == 0) { // If we found the matching closing brace
189 return fReader.getOffset (); // return the position of this closing brace
192 c = fReader.read (); // Read the next character
200 * @param offset The search start position
201 * @param openingPeer The opening brace we search for
202 * @param closingPeer The closing brace we search for
203 * @param document The document we currently are in
207 * @throws IOException
209 protected int searchForOpeningPeer (int offset, char openingPeer,
210 char closingPeer, IDocument document) throws IOException {
211 fReader.configureBackwardReader (document, offset, true, true);
213 int stack = 1; // As we have already the opening brace
214 int c = fReader.read (); // Read character on position one before the closing brace
216 while (c != PHPCodeReader.EOF) { // As long as we are not at the start of text
217 if ((c == closingPeer) && // If character is closing brace again
218 (c != openingPeer)) { // and not a opening brace (how could it be?)
219 stack++; // put it on stack
221 else if (c == openingPeer) { // If it's a opening brace
222 stack--; // Decrement level counter
225 if (stack == 0) { // If we found the matching closing brace
226 return fReader.getOffset (); // return the position of this closing brace
229 c = fReader.read (); // Read the previous character