1) Fixed html syntax highlighting.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / PHPPairMatcher.java
1 package net.sourceforge.phpdt.internal.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import java.io.IOException;
9
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;
15
16 /**
17  * Helper class for match pairs of characters.
18  */
19 public class PHPPairMatcher implements ICharacterPairMatcher {
20         protected char[]    fPairs;           // Holds the brace pairs
21
22         protected IDocument fDocument;
23
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;
28
29         protected PHPCodeReader fReader = new PHPCodeReader();
30
31         /**
32          *
33          * @param pairs The array of opening and closing braces we need for searching the brace matching
34          */
35
36         public PHPPairMatcher (char[] pairs) {
37                 fPairs = pairs;
38         }
39
40         /*
41          * @see org.eclipse.jface.text.source.ICharacterPairMatcher#clear()
42          */
43         public void clear() {
44                 if (fReader != null) {
45                         try {
46                                 fReader.close();
47                         } catch (IOException x) {
48                                 // ignore
49                         }
50                 }
51         }
52
53         /**
54          * Try to find a matching pair of opening and closing braces
55          *
56          * @return  Returns a region if a matching pair was found.
57          */
58
59         public IRegion match (IDocument document, int offset) {
60                 fOffset = offset;
61
62                 if (fOffset < 0) {
63                         return null;
64                 }
65
66                 fDocument = document;
67
68                 if (matchPairsAt ()) {                                                                          // If we found a pair of opening and closing braces
69                         return new Region (nPosOpening, nPosClosing - nPosOpening + 1);
70                 }
71
72                 return null;
73         }
74
75         public int getAnchor() {
76                 return fAnchor;
77         }
78
79         public void dispose() {
80                 fDocument = null;
81                 if (fReader != null) {
82                         try {
83                                 fReader.close();
84                         } catch (IOException x) {
85                                 // ignore
86                         }
87                         fReader = null;
88                 }
89         }
90
91         /**
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
94          *
95          * @return True if a pair was found
96          */
97         protected boolean matchPairsAt() {
98                 int   i;
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
104
105                 nPosOpening = -1;
106                 nPosClosing = -1;
107
108                 // get the chars preceding and following the start position
109                 try {
110             cCurrentChar = fDocument.getChar (fOffset);
111                         cPrevChar    = fDocument.getChar (Math.max (fOffset - 1, 0));
112
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
118                                 }
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
122                                 }
123                         }
124
125                         // search for closing peer character next to the activation point
126
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
132                                         }
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
136                                         }
137                                 }
138                         }
139
140                         if (nPosClosing > -1) {                             // If we found a closing brace on current position (or before)
141                                 fAnchor     = RIGHT;
142                                 nPosOpening = searchForOpeningPeer (nPosClosing, fPairs[nIndexClose - 1], fPairs[nIndexClose], fDocument);
143
144                                 return (nPosOpening > -1);                      // If we found a opening brace, return true
145                         }
146                         else if (nPosOpening > -1) {                        // If we found a opening brace on current position (or before)
147                                 fAnchor     = LEFT;
148                                 nPosClosing = searchForClosingPeer (nPosOpening, fPairs[nIndexOpen], fPairs[nIndexOpen + 1], fDocument);
149
150                                 return (nPosClosing > -1);                      // If we found an closing brace for this opening brace return true
151                         }
152                 }
153                 catch (BadLocationException x) {
154                 }
155                 catch (IOException x) {
156                 }
157
158                 return false;
159         }
160
161         /**
162          *
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
167          *
168          * @return
169          *
170          * @throws IOException
171          */
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);
175
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
178
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
183                         }
184                         else if (c == closingPeer) {                // If it's a closing brace
185                                 stack--;                                // Decrement level counter
186                         }
187
188                         if (stack == 0) {                           // If we found the matching closing brace
189                                 return fReader.getOffset ();            // return the position of this closing brace
190                         }
191
192                         c = fReader.read ();                        // Read the next character
193                 }
194
195                 return -1;
196         }
197
198         /**
199          *
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
204          *
205          * @return
206          *
207          * @throws IOException
208          */
209         protected int searchForOpeningPeer (int offset, char openingPeer,
210                                                 char closingPeer, IDocument document) throws IOException {
211                 fReader.configureBackwardReader (document, offset, true, true);
212
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
215
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
220                         }
221                         else if (c == openingPeer) {                // If it's a opening brace
222                                 stack--;                                // Decrement level counter
223                         }
224
225                         if (stack == 0) {                           // If we found the matching closing brace
226                                 return fReader.getOffset ();            // return the position of this closing brace
227                         }
228
229                         c = fReader.read ();                        // Read the previous character
230                 }
231
232                 return -1;
233         }
234 }