From d1133259520a0183199448dba33c50f8727a8938 Mon Sep 17 00:00:00 2001 From: axelcl Date: Sat, 20 Aug 2005 16:27:17 +0000 Subject: [PATCH] misc --- .../ui/text/java/JavaParameterListValidator.java | 220 ++++++++++++++++++++ .../phpeditor/php/PHPCompletionProcessor.java | 57 +++--- 2 files changed, 249 insertions(+), 28 deletions(-) create mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaParameterListValidator.java diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaParameterListValidator.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaParameterListValidator.java new file mode 100644 index 0000000..82a09e9 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaParameterListValidator.java @@ -0,0 +1,220 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package net.sourceforge.phpdt.internal.ui.text.java; + + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; + +import org.eclipse.jface.text.Assert; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.TextPresentation; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.contentassist.IContextInformationPresenter; +import org.eclipse.jface.text.contentassist.IContextInformationValidator; + + + +public class JavaParameterListValidator implements IContextInformationValidator, IContextInformationPresenter { + + private int fPosition; + private ITextViewer fViewer; + private IContextInformation fInformation; + + private int fCurrentParameter; + + + + public JavaParameterListValidator() { + } + + /** + * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int) + * @see IContextInformationPresenter#install(IContextInformation, ITextViewer, int) + */ + public void install(IContextInformation info, ITextViewer viewer, int documentPosition) { + fPosition= documentPosition; + fViewer= viewer; + fInformation= info; + + fCurrentParameter= -1; + } + + private int getCommentEnd(IDocument d, int pos, int end) throws BadLocationException { + while (pos < end) { + char curr= d.getChar(pos); + pos++; + if (curr == '*') { + if (pos < end && d.getChar(pos) == '/') { + return pos + 1; + } + } + } + return end; + } + + private int getStringEnd(IDocument d, int pos, int end, char ch) throws BadLocationException { + while (pos < end) { + char curr= d.getChar(pos); + pos++; + if (curr == '\\') { + // ignore escaped characters + pos++; + } else if (curr == ch) { + return pos; + } + } + return end; + } + + private int getCharCount(IDocument document, int start, int end, String increments, String decrements, boolean considerNesting) throws BadLocationException { + + Assert.isTrue((increments.length() != 0 || decrements.length() != 0) && !increments.equals(decrements)); + + int nestingLevel= 0; + int charCount= 0; + while (start < end) { + char curr= document.getChar(start++); + switch (curr) { + case '/': + if (start < end) { + char next= document.getChar(start); + if (next == '*') { + // a comment starts, advance to the comment end + start= getCommentEnd(document, start + 1, end); + } else if (next == '/') { + // '//'-comment: nothing to do anymore on this line + start= end; + } + } + break; + case '*': + if (start < end) { + char next= document.getChar(start); + if (next == '/') { + // we have been in a comment: forget what we read before + charCount= 0; + ++ start; + } + } + break; + case '"': + case '\'': + start= getStringEnd(document, start, end, curr); + break; + default: + + if (considerNesting) { + + if ('(' == curr) + ++ nestingLevel; + else if (')' == curr) + -- nestingLevel; + + if (nestingLevel != 0) + break; + } + + if (increments.indexOf(curr) >= 0) { + ++ charCount; + } + + if (decrements.indexOf(curr) >= 0) { + -- charCount; + } + } + } + + return charCount; + } + + /** + * @see IContextInformationValidator#isContextInformationValid(int) + */ + public boolean isContextInformationValid(int position) { + + try { + if (position < fPosition) + return false; + + IDocument document= fViewer.getDocument(); + IRegion line= document.getLineInformationOfOffset(fPosition); + + if (position < line.getOffset() || position >= document.getLength()) + return false; + + return getCharCount(document, fPosition, position, "(<", ")>", false) >= 0; //$NON-NLS-1$//$NON-NLS-2$ + + } catch (BadLocationException x) { + return false; + } + } + + /** + * @see IContextInformationPresenter#updatePresentation(int, TextPresentation) + */ + public boolean updatePresentation(int position, TextPresentation presentation) { + + int currentParameter= -1; + + try { + currentParameter= getCharCount(fViewer.getDocument(), fPosition, position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$ + } catch (BadLocationException x) { + return false; + } + + if (fCurrentParameter != -1) { + if (currentParameter == fCurrentParameter) + return false; + } + + presentation.clear(); + fCurrentParameter= currentParameter; + + String s= fInformation.getInformationDisplayString(); + int start= 0; + int occurrences= 0; + while (occurrences < fCurrentParameter) { + int found= s.indexOf(',', start); + if (found == -1) + break; + start= found + 1; + ++ occurrences; + } + + if (occurrences < fCurrentParameter) { + presentation.addStyleRange(new StyleRange(0, s.length(), null, null, SWT.NORMAL)); + return true; + } + + if (start == -1) + start= 0; + + int end= s.indexOf(',', start); + if (end == -1) + end= s.length(); + + if (start > 0) + presentation.addStyleRange(new StyleRange(0, start, null, null, SWT.NORMAL)); + + if (end > start) + presentation.addStyleRange(new StyleRange(start, end - start, null, null, SWT.BOLD)); + + if (end < s.length()) + presentation.addStyleRange(new StyleRange(end, s.length() - end, null, null, SWT.NORMAL)); + + return true; + } +} + diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java index 98f40cb..c0bdd55 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java @@ -40,6 +40,7 @@ import net.sourceforge.phpdt.internal.corext.template.php.JavaContext; import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType; import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader; import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal; +import net.sourceforge.phpdt.internal.ui.text.java.JavaParameterListValidator; import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator; import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine; import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine; @@ -59,12 +60,10 @@ import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.Region; -import org.eclipse.jface.text.TextPresentation; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.jface.text.contentassist.IContextInformationExtension; -import org.eclipse.jface.text.contentassist.IContextInformationPresenter; import org.eclipse.jface.text.contentassist.IContextInformationValidator; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.swt.graphics.Image; @@ -82,30 +81,30 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { /** * Simple content assist tip closer. The tip is valid in a range of 5 characters around its popup location. */ - protected static class Validator implements IContextInformationValidator, IContextInformationPresenter { - protected int fInstallOffset; - - /* - * @see IContextInformationValidator#isContextInformationValid(int) - */ - public boolean isContextInformationValid(int offset) { - return Math.abs(fInstallOffset - offset) < 5; - } - - /* - * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int) - */ - public void install(IContextInformation info, ITextViewer viewer, int offset) { - fInstallOffset = offset; - } - - /* - * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation) - */ - public boolean updatePresentation(int documentPosition, TextPresentation presentation) { - return false; - } - }; +// protected static class Validator implements IContextInformationValidator, IContextInformationPresenter { +// protected int fInstallOffset; +// +// /* +// * @see IContextInformationValidator#isContextInformationValid(int) +// */ +// public boolean isContextInformationValid(int offset) { +// return Math.abs(fInstallOffset - offset) < 5; +// } +// +// /* +// * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int) +// */ +// public void install(IContextInformation info, ITextViewer viewer, int offset) { +// fInstallOffset = offset; +// } +// +// /* +// * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation) +// */ +// public boolean updatePresentation(int documentPosition, TextPresentation presentation) { +// return false; +// } +// }; private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension { private final IContextInformation fContextInformation; @@ -177,7 +176,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { private char[] fProposalAutoActivationSet; - protected IContextInformationValidator fValidator = new Validator(); + protected IContextInformationValidator fValidator = null;// = new Validator(); private TemplateEngine fTemplateEngine; @@ -971,13 +970,15 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { * (non-Javadoc) Method declared on IContentAssistProcessor */ public char[] getContextInformationAutoActivationCharacters() { - return new char[] {}; + return null; } /* * (non-Javadoc) Method declared on IContentAssistProcessor */ public IContextInformationValidator getContextInformationValidator() { + if (fValidator == null) + fValidator= new JavaParameterListValidator(); return fValidator; } -- 1.7.1