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 = PHPFunctionNames.FUNCTION_NAMES;
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 ArrayList arrList = new ArrayList(5);
138 IDocument document = viewer.getDocument();
139 if (documentOffset > 0) {
141 ICompletionProposal[] result;
142 char character = document.getChar(documentOffset - 1);
143 if (character == '$') {
144 //viewer. .getActivePage().getActiveEditor();
145 result = new ICompletionProposal[fgProposals.length];
146 for (int i = 0; i < fgProposals.length; i++) {
147 IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
148 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$
152 } catch (BadLocationException e) {
153 return new ICompletionProposal[0];
157 ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
158 for (int i = 0; i < fgProposals.length; i++) {
159 IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
160 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$
167 * Method declared on IContentAssistProcessor
169 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
170 IContextInformation[] result = new IContextInformation[5];
171 for (int i = 0; i < result.length; i++)
172 result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
173 MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
178 * Method declared on IContentAssistProcessor
180 public char[] getCompletionProposalAutoActivationCharacters() {
181 return new char[] { '$' };
185 * Method declared on IContentAssistProcessor
187 public char[] getContextInformationAutoActivationCharacters() {
193 * Method declared on IContentAssistProcessor
195 public IContextInformationValidator getContextInformationValidator() {
200 * Method declared on IContentAssistProcessor
202 public String getErrorMessage() {