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