1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.text.MessageFormat;
15 import java.util.ArrayList;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.TextPresentation;
21 import org.eclipse.jface.text.contentassist.CompletionProposal;
22 import org.eclipse.jface.text.contentassist.ContextInformation;
23 import org.eclipse.jface.text.contentassist.ICompletionProposal;
24 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
25 import org.eclipse.jface.text.contentassist.IContextInformation;
26 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
27 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
28 import org.eclipse.swt.graphics.Point;
31 * Example PHP completion processor.
33 public class PHPCompletionProcessor implements IContentAssistProcessor {
36 * Simple content assist tip closer. The tip is valid in a range
37 * of 5 characters around its popup location.
39 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
41 protected int fInstallOffset;
44 * @see IContextInformationValidator#isContextInformationValid(int)
46 public boolean isContextInformationValid(int offset) {
47 return Math.abs(fInstallOffset - offset) < 5;
51 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
53 public void install(IContextInformation info, ITextViewer viewer, int offset) {
54 fInstallOffset = offset;
58 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
60 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
65 protected final static String[] fgProposals = PHPCodeScanner.fgFunctionNames;
103 protected IContextInformationValidator fValidator = new Validator();
106 * Method declared on IContentAssistProcessor
108 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
110 // ArrayList arrList = new ArrayList(5);
111 // IDocument document = viewer.getDocument();
112 // if (documentOffset > 0) {
114 // char character = document.getChar(documentOffset - 1);
115 // Point point = PHPWordExtractor.findWord(document, documentOffset);
116 // if (point != null) {
117 // String word = document.get(point.x, point.y);
118 // for (int i = 0; i < fgProposals.length; i++) {
119 // if ((fgProposals[i].length() >= point.y) && fgProposals[i].substring(0, point.y).equals(word)) {
120 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
121 // 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$
124 // if (arrList.size() > 0) {
125 // ICompletionProposal[] result = new ICompletionProposal[arrList.size()];
126 // for (int i=0;i<arrList.size();i++) {
127 // result[i] = (CompletionProposal) arrList.get(i);
132 // } catch (BadLocationException e) {
137 // if (documentOffset > 0) {
139 // char character = document.getChar(documentOffset - 1);
140 // if (character=='$') {
143 // result = new ICompletionProposal[fgProposals.length];
144 // for (int i = 0; i < fgProposals.length; i++) {
145 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
146 // 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$
148 // } catch (BadLocationException e) {
152 ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
153 for (int i = 0; i < fgProposals.length; i++) {
154 IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
155 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$
162 * Method declared on IContentAssistProcessor
164 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
165 IContextInformation[] result = new IContextInformation[5];
166 for (int i = 0; i < result.length; i++)
167 result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
168 MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
173 * Method declared on IContentAssistProcessor
175 public char[] getCompletionProposalAutoActivationCharacters() {
176 return new char[] { '$' };
180 * Method declared on IContentAssistProcessor
182 public char[] getContextInformationAutoActivationCharacters() {
188 * Method declared on IContentAssistProcessor
190 public IContextInformationValidator getContextInformationValidator() {
195 * Method declared on IContentAssistProcessor
197 public String getErrorMessage() {