fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / JavaParameterListValidator.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
12
13 import org.eclipse.jface.text.Assert;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.TextPresentation;
19 import org.eclipse.jface.text.contentassist.IContextInformation;
20 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
21 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.StyleRange;
24
25 public class JavaParameterListValidator implements
26                 IContextInformationValidator, IContextInformationPresenter {
27
28         private int fPosition;
29
30         private ITextViewer fViewer;
31
32         private IContextInformation fInformation;
33
34         private int fCurrentParameter;
35
36         public JavaParameterListValidator() {
37         }
38
39         /**
40          * @see IContextInformationValidator#install(IContextInformation,
41          *      ITextViewer, int)
42          * @see IContextInformationPresenter#install(IContextInformation,
43          *      ITextViewer, int)
44          */
45         public void install(IContextInformation info, ITextViewer viewer,
46                         int documentPosition) {
47                 fPosition = documentPosition;
48                 fViewer = viewer;
49                 fInformation = info;
50
51                 fCurrentParameter = -1;
52         }
53
54         private int getCommentEnd(IDocument d, int pos, int end)
55                         throws BadLocationException {
56                 while (pos < end) {
57                         char curr = d.getChar(pos);
58                         pos++;
59                         if (curr == '*') {
60                                 if (pos < end && d.getChar(pos) == '/') {
61                                         return pos + 1;
62                                 }
63                         }
64                 }
65                 return end;
66         }
67
68         private int getStringEnd(IDocument d, int pos, int end, char ch)
69                         throws BadLocationException {
70                 while (pos < end) {
71                         char curr = d.getChar(pos);
72                         pos++;
73                         if (curr == '\\') {
74                                 // ignore escaped characters
75                                 pos++;
76                         } else if (curr == ch) {
77                                 return pos;
78                         }
79                 }
80                 return end;
81         }
82
83         private int getCharCount(IDocument document, int start, int end,
84                         String increments, String decrements, boolean considerNesting)
85                         throws BadLocationException {
86
87                 Assert.isTrue((increments.length() != 0 || decrements.length() != 0)
88                                 && !increments.equals(decrements));
89
90                 int nestingLevel = 0;
91                 int charCount = 0;
92                 while (start < end) {
93                         char curr = document.getChar(start++);
94                         switch (curr) {
95                         case '/':
96                                 if (start < end) {
97                                         char next = document.getChar(start);
98                                         if (next == '*') {
99                                                 // a comment starts, advance to the comment end
100                                                 start = getCommentEnd(document, start + 1, end);
101                                         } else if (next == '/') {
102                                                 // '//'-comment: nothing to do anymore on this line
103                                                 start = end;
104                                         }
105                                 }
106                                 break;
107                         case '*':
108                                 if (start < end) {
109                                         char next = document.getChar(start);
110                                         if (next == '/') {
111                                                 // we have been in a comment: forget what we read before
112                                                 charCount = 0;
113                                                 ++start;
114                                         }
115                                 }
116                                 break;
117                         case '"':
118                         case '\'':
119                                 start = getStringEnd(document, start, end, curr);
120                                 break;
121                         default:
122
123                                 if (considerNesting) {
124
125                                         if ('(' == curr)
126                                                 ++nestingLevel;
127                                         else if (')' == curr)
128                                                 --nestingLevel;
129
130                                         if (nestingLevel != 0)
131                                                 break;
132                                 }
133
134                                 if (increments.indexOf(curr) >= 0) {
135                                         ++charCount;
136                                 }
137
138                                 if (decrements.indexOf(curr) >= 0) {
139                                         --charCount;
140                                 }
141                         }
142                 }
143
144                 return charCount;
145         }
146
147         /**
148          * @see IContextInformationValidator#isContextInformationValid(int)
149          */
150         public boolean isContextInformationValid(int position) {
151
152                 try {
153                         if (position < fPosition)
154                                 return false;
155
156                         IDocument document = fViewer.getDocument();
157                         IRegion line = document.getLineInformationOfOffset(fPosition);
158
159                         if (position < line.getOffset() || position >= document.getLength())
160                                 return false;
161
162                         return getCharCount(document, fPosition, position,
163                                         "(<", ")>", false) >= 0; //$NON-NLS-1$//$NON-NLS-2$
164
165                 } catch (BadLocationException x) {
166                         return false;
167                 }
168         }
169
170         /**
171          * @see IContextInformationPresenter#updatePresentation(int,
172          *      TextPresentation)
173          */
174         public boolean updatePresentation(int position,
175                         TextPresentation presentation) {
176
177                 int currentParameter = -1;
178
179                 try {
180                         currentParameter = getCharCount(fViewer.getDocument(), fPosition,
181                                         position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$
182                 } catch (BadLocationException x) {
183                         return false;
184                 }
185
186                 if (fCurrentParameter != -1) {
187                         if (currentParameter == fCurrentParameter)
188                                 return false;
189                 }
190
191                 presentation.clear();
192                 fCurrentParameter = currentParameter;
193
194                 String s = fInformation.getInformationDisplayString();
195                 int start = 0;
196                 int occurrences = 0;
197                 while (occurrences < fCurrentParameter) {
198                         int found = s.indexOf(',', start);
199                         if (found == -1)
200                                 break;
201                         start = found + 1;
202                         ++occurrences;
203                 }
204
205                 if (occurrences < fCurrentParameter) {
206                         presentation.addStyleRange(new StyleRange(0, s.length(), null,
207                                         null, SWT.NORMAL));
208                         return true;
209                 }
210
211                 if (start == -1)
212                         start = 0;
213
214                 int end = s.indexOf(',', start);
215                 if (end == -1)
216                         end = s.length();
217
218                 if (start > 0)
219                         presentation.addStyleRange(new StyleRange(0, start, null, null,
220                                         SWT.NORMAL));
221
222                 if (end > start)
223                         presentation.addStyleRange(new StyleRange(start, end - start, null,
224                                         null, SWT.BOLD));
225
226                 if (end < s.length())
227                         presentation.addStyleRange(new StyleRange(end, s.length() - end,
228                                         null, null, SWT.NORMAL));
229
230                 return true;
231         }
232 }