/********************************************************************** Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html Contributors: IBM Corporation - Initial implementation Klaus Hartlage - www.eclipseproject.de **********************************************************************/ package net.sourceforge.phpeclipse.phpeditor.php; import java.text.MessageFormat; import java.util.ArrayList; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.TextPresentation; import org.eclipse.jface.text.contentassist.CompletionProposal; import org.eclipse.jface.text.contentassist.ContextInformation; 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.IContextInformationPresenter; import org.eclipse.jface.text.contentassist.IContextInformationValidator; import org.eclipse.swt.graphics.Point; /** * Example PHP completion processor. */ 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 final static String[] fgProposals = PHPCodeScanner.fgFunctionNames; // { // "array", // "break", // "class", // "continue", // "do", // "echo", // "else", // "elseif", // "endfor", // "endif", // "for", // "if", // "while", // "endwhile", // "switch", // "case", // "endswitch", // "return", // "define", // "include", // "include_once", // "require", // "require_once", // "function", // "new", // "old_function", // "default", // "global", // "static", // "foreach", // "endforeach", // "extends", // "empty", // "isset", // "var" }; protected IContextInformationValidator fValidator = new Validator(); /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { // ArrayList arrList = new ArrayList(5); // IDocument document = viewer.getDocument(); // if (documentOffset > 0) { // try { // char character = document.getChar(documentOffset - 1); // Point point = PHPWordExtractor.findWord(document, documentOffset); // if (point != null) { // String word = document.get(point.x, point.y); // for (int i = 0; i < fgProposals.length; i++) { // if ((fgProposals[i].length() >= point.y) && fgProposals[i].substring(0, point.y).equals(word)) { // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$ // arrList.add(new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] }))); //$NON-NLS-1$ // } // } // if (arrList.size() > 0) { // ICompletionProposal[] result = new ICompletionProposal[arrList.size()]; // for (int i=0;i 0) { // try { // char character = document.getChar(documentOffset - 1); // if (character=='$') { // // } // result = new ICompletionProposal[fgProposals.length]; // for (int i = 0; i < fgProposals.length; i++) { // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$ // result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$ // } // } catch (BadLocationException e) { // } // // } else { ICompletionProposal[] result = new ICompletionProposal[fgProposals.length]; for (int i = 0; i < fgProposals.length; i++) { IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$ result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$ } return result; } /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { IContextInformation[] result = new IContextInformation[5]; for (int i = 0; i < result.length; i++) result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$ MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$ return result; } /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public char[] getCompletionProposalAutoActivationCharacters() { return new char[] { '$' }; } /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public char[] getContextInformationAutoActivationCharacters() { return new char[] { }; } /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public IContextInformationValidator getContextInformationValidator() { return fValidator; } /* (non-Javadoc) * Method declared on IContentAssistProcessor */ public String getErrorMessage() { return null; } }