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.swt.SWT;
15 import org.eclipse.swt.custom.StyleRange;
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;
29 public class JavaParameterListValidator implements IContextInformationValidator, IContextInformationPresenter {
31 private int fPosition;
32 private ITextViewer fViewer;
33 private IContextInformation fInformation;
35 private int fCurrentParameter;
39 public JavaParameterListValidator() {
43 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
44 * @see IContextInformationPresenter#install(IContextInformation, ITextViewer, int)
46 public void install(IContextInformation info, ITextViewer viewer, int documentPosition) {
47 fPosition= documentPosition;
51 fCurrentParameter= -1;
54 private int getCommentEnd(IDocument d, int pos, int end) throws BadLocationException {
56 char curr= d.getChar(pos);
59 if (pos < end && d.getChar(pos) == '/') {
67 private int getStringEnd(IDocument d, int pos, int end, char ch) throws BadLocationException {
69 char curr= d.getChar(pos);
72 // ignore escaped characters
74 } else if (curr == ch) {
81 private int getCharCount(IDocument document, int start, int end, String increments, String decrements, boolean considerNesting) throws BadLocationException {
83 Assert.isTrue((increments.length() != 0 || decrements.length() != 0) && !increments.equals(decrements));
88 char curr= document.getChar(start++);
92 char next= document.getChar(start);
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
104 char next= document.getChar(start);
106 // we have been in a comment: forget what we read before
114 start= getStringEnd(document, start, end, curr);
118 if (considerNesting) {
122 else if (')' == curr)
125 if (nestingLevel != 0)
129 if (increments.indexOf(curr) >= 0) {
133 if (decrements.indexOf(curr) >= 0) {
143 * @see IContextInformationValidator#isContextInformationValid(int)
145 public boolean isContextInformationValid(int position) {
148 if (position < fPosition)
151 IDocument document= fViewer.getDocument();
152 IRegion line= document.getLineInformationOfOffset(fPosition);
154 if (position < line.getOffset() || position >= document.getLength())
157 return getCharCount(document, fPosition, position, "(<", ")>", false) >= 0; //$NON-NLS-1$//$NON-NLS-2$
159 } catch (BadLocationException x) {
165 * @see IContextInformationPresenter#updatePresentation(int, TextPresentation)
167 public boolean updatePresentation(int position, TextPresentation presentation) {
169 int currentParameter= -1;
172 currentParameter= getCharCount(fViewer.getDocument(), fPosition, position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$
173 } catch (BadLocationException x) {
177 if (fCurrentParameter != -1) {
178 if (currentParameter == fCurrentParameter)
182 presentation.clear();
183 fCurrentParameter= currentParameter;
185 String s= fInformation.getInformationDisplayString();
188 while (occurrences < fCurrentParameter) {
189 int found= s.indexOf(',', start);
196 if (occurrences < fCurrentParameter) {
197 presentation.addStyleRange(new StyleRange(0, s.length(), null, null, SWT.NORMAL));
204 int end= s.indexOf(',', start);
209 presentation.addStyleRange(new StyleRange(0, start, null, null, SWT.NORMAL));
212 presentation.addStyleRange(new StyleRange(start, end - start, null, null, SWT.BOLD));
214 if (end < s.length())
215 presentation.addStyleRange(new StyleRange(end, s.length() - end, null, null, SWT.NORMAL));