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;
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;
25 public class JavaParameterListValidator implements
26 IContextInformationValidator, IContextInformationPresenter {
28 private int fPosition;
30 private ITextViewer fViewer;
32 private IContextInformation fInformation;
34 private int fCurrentParameter;
36 public JavaParameterListValidator() {
40 * @see IContextInformationValidator#install(IContextInformation,
42 * @see IContextInformationPresenter#install(IContextInformation,
45 public void install(IContextInformation info, ITextViewer viewer,
46 int documentPosition) {
47 fPosition = documentPosition;
51 fCurrentParameter = -1;
54 private int getCommentEnd(IDocument d, int pos, int end)
55 throws BadLocationException {
57 char curr = d.getChar(pos);
60 if (pos < end && d.getChar(pos) == '/') {
68 private int getStringEnd(IDocument d, int pos, int end, char ch)
69 throws BadLocationException {
71 char curr = d.getChar(pos);
74 // ignore escaped characters
76 } else if (curr == ch) {
83 private int getCharCount(IDocument document, int start, int end,
84 String increments, String decrements, boolean considerNesting)
85 throws BadLocationException {
87 Assert.isTrue((increments.length() != 0 || decrements.length() != 0)
88 && !increments.equals(decrements));
93 char curr = document.getChar(start++);
97 char next = document.getChar(start);
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
109 char next = document.getChar(start);
111 // we have been in a comment: forget what we read before
119 start = getStringEnd(document, start, end, curr);
123 if (considerNesting) {
127 else if (')' == curr)
130 if (nestingLevel != 0)
134 if (increments.indexOf(curr) >= 0) {
138 if (decrements.indexOf(curr) >= 0) {
148 * @see IContextInformationValidator#isContextInformationValid(int)
150 public boolean isContextInformationValid(int position) {
153 if (position < fPosition)
156 IDocument document = fViewer.getDocument();
157 IRegion line = document.getLineInformationOfOffset(fPosition);
159 if (position < line.getOffset() || position >= document.getLength())
162 return getCharCount(document, fPosition, position,
163 "(<", ")>", false) >= 0; //$NON-NLS-1$//$NON-NLS-2$
165 } catch (BadLocationException x) {
171 * @see IContextInformationPresenter#updatePresentation(int,
174 public boolean updatePresentation(int position,
175 TextPresentation presentation) {
177 int currentParameter = -1;
180 currentParameter = getCharCount(fViewer.getDocument(), fPosition,
181 position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$
182 } catch (BadLocationException x) {
186 if (fCurrentParameter != -1) {
187 if (currentParameter == fCurrentParameter)
191 presentation.clear();
192 fCurrentParameter = currentParameter;
194 String s = fInformation.getInformationDisplayString();
197 while (occurrences < fCurrentParameter) {
198 int found = s.indexOf(',', start);
205 if (occurrences < fCurrentParameter) {
206 presentation.addStyleRange(new StyleRange(0, s.length(), null,
214 int end = s.indexOf(',', start);
219 presentation.addStyleRange(new StyleRange(0, start, null, null,
223 presentation.addStyleRange(new StyleRange(start, end - start, null,
226 if (end < s.length())
227 presentation.addStyleRange(new StyleRange(end, s.length() - end,
228 null, null, SWT.NORMAL));