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