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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
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;
27 public class JavaParameterListValidator implements
28 IContextInformationValidator, IContextInformationPresenter {
30 private int fPosition;
32 private ITextViewer fViewer;
34 private IContextInformation fInformation;
36 private int fCurrentParameter;
38 public JavaParameterListValidator() {
42 * @see IContextInformationValidator#install(IContextInformation,
44 * @see IContextInformationPresenter#install(IContextInformation,
47 public void install(IContextInformation info, ITextViewer viewer,
48 int documentPosition) {
49 fPosition = documentPosition;
53 fCurrentParameter = -1;
56 private int getCommentEnd(IDocument d, int pos, int end)
57 throws BadLocationException {
59 char curr = d.getChar(pos);
62 if (pos < end && d.getChar(pos) == '/') {
70 private int getStringEnd(IDocument d, int pos, int end, char ch)
71 throws BadLocationException {
73 char curr = d.getChar(pos);
76 // ignore escaped characters
78 } else if (curr == ch) {
85 private int getCharCount(IDocument document, int start, int end,
86 String increments, String decrements, boolean considerNesting)
87 throws BadLocationException {
89 Assert.isTrue((increments.length() != 0 || decrements.length() != 0)
90 && !increments.equals(decrements));
95 char curr = document.getChar(start++);
99 char next = document.getChar(start);
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
111 char next = document.getChar(start);
113 // we have been in a comment: forget what we read before
121 start = getStringEnd(document, start, end, curr);
125 if (considerNesting) {
129 else if (')' == curr)
132 if (nestingLevel != 0)
136 if (increments.indexOf(curr) >= 0) {
140 if (decrements.indexOf(curr) >= 0) {
150 * @see IContextInformationValidator#isContextInformationValid(int)
152 public boolean isContextInformationValid(int position) {
155 if (position < fPosition)
158 IDocument document = fViewer.getDocument();
159 IRegion line = document.getLineInformationOfOffset(fPosition);
161 if (position < line.getOffset() || position >= document.getLength())
164 return getCharCount(document, fPosition, position,
165 "(<", ")>", false) >= 0; //$NON-NLS-1$//$NON-NLS-2$
167 } catch (BadLocationException x) {
173 * @see IContextInformationPresenter#updatePresentation(int,
176 public boolean updatePresentation(int position,
177 TextPresentation presentation) {
179 int currentParameter = -1;
182 currentParameter = getCharCount(fViewer.getDocument(), fPosition,
183 position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$
184 } catch (BadLocationException x) {
188 if (fCurrentParameter != -1) {
189 if (currentParameter == fCurrentParameter)
193 presentation.clear();
194 fCurrentParameter = currentParameter;
196 String s = fInformation.getInformationDisplayString();
199 while (occurrences < fCurrentParameter) {
200 int found = s.indexOf(',', start);
207 if (occurrences < fCurrentParameter) {
208 presentation.addStyleRange(new StyleRange(0, s.length(), null,
216 int end = s.indexOf(',', start);
221 presentation.addStyleRange(new StyleRange(0, start, null, null,
225 presentation.addStyleRange(new StyleRange(start, end - start, null,
228 if (end < s.length())
229 presentation.addStyleRange(new StyleRange(end, s.length() - end,
230 null, null, SWT.NORMAL));