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.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
18 import net.sourceforge.phpdt.internal.corext.template.ContextType;
19 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
20 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
21 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
22 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
23 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
24 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26 import net.sourceforge.phpeclipse.phpeditor.AbstractContentOutlinePage;
27 import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage;
28 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.ITextViewer;
32 import org.eclipse.jface.text.TextPresentation;
33 import org.eclipse.jface.text.contentassist.ICompletionProposal;
34 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
35 import org.eclipse.jface.text.contentassist.IContextInformation;
36 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
37 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
38 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.ui.IEditorPart;
43 * Example PHP completion processor.
45 public class PHPCompletionProcessor implements IContentAssistProcessor {
48 * Simple content assist tip closer. The tip is valid in a range
49 * of 5 characters around its popup location.
51 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
53 protected int fInstallOffset;
56 * @see IContextInformationValidator#isContextInformationValid(int)
58 public boolean isContextInformationValid(int offset) {
59 return Math.abs(fInstallOffset - offset) < 5;
63 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
65 public void install(IContextInformation info, ITextViewer viewer, int offset) {
66 fInstallOffset = offset;
70 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
72 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
77 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
79 private final IContextInformation fContextInformation;
80 private int fPosition;
82 public ContextInformationWrapper(IContextInformation contextInformation) {
83 fContextInformation = contextInformation;
87 * @see IContextInformation#getContextDisplayString()
89 public String getContextDisplayString() {
90 return fContextInformation.getContextDisplayString();
94 * @see IContextInformation#getImage()
96 public Image getImage() {
97 return fContextInformation.getImage();
101 * @see IContextInformation#getInformationDisplayString()
103 public String getInformationDisplayString() {
104 return fContextInformation.getInformationDisplayString();
108 * @see IContextInformationExtension#getContextInformationPosition()
110 public int getContextInformationPosition() {
114 public void setContextInformationPosition(int position) {
115 fPosition = position;
119 // public final class VariablesCompletionProposal implements IJavaCompletionProposal {
120 // private String fDisplayString;
121 // private String fReplacementString;
122 // private int fReplacementOffset;
123 // private int fReplacementLength;
124 // private int fCursorPosition;
125 // private Image fImage;
126 // private IContextInformation fContextInformation;
127 // private String fAdditionalProposalInfo;
130 // * Creates a new completion proposal based on the provided information. The replacement string is
131 // * considered being the display string too. All remaining fields are set to <code>null</code>.
133 // * @param replacementString the actual string to be inserted into the document
134 // * @param replacementOffset the offset of the text to be replaced
135 // * @param replacementLength the length of the text to be replaced
136 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
138 // public VariablesCompletionProposal(
139 // String replacementString,
140 // int replacementOffset,
141 // int replacementLength,
142 // int cursorPosition) {
143 // this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
147 // * Creates a new completion proposal. All fields are initialized based on the provided information.
149 // * @param replacementString the actual string to be inserted into the document
150 // * @param replacementOffset the offset of the text to be replaced
151 // * @param replacementLength the length of the text to be replaced
152 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
153 // * @param image the image to display for this proposal
154 // * @param displayString the string to be displayed for the proposal
155 // * @param contentInformation the context information associated with this proposal
156 // * @param additionalProposalInfo the additional information associated with this proposal
158 // public VariablesCompletionProposal(
159 // String replacementString,
160 // int replacementOffset,
161 // int replacementLength,
162 // int cursorPosition,
164 // String displayString,
165 // IContextInformation contextInformation,
166 // String additionalProposalInfo) {
167 // // Assert.isNotNull(replacementString);
168 // // Assert.isTrue(replacementOffset >= 0);
169 // // Assert.isTrue(replacementLength >= 0);
170 // // Assert.isTrue(cursorPosition >= 0);
172 // fReplacementString = replacementString;
173 // fReplacementOffset = replacementOffset;
174 // fReplacementLength = replacementLength;
175 // fCursorPosition = cursorPosition;
177 // fDisplayString = displayString;
178 // fContextInformation = contextInformation;
179 // fAdditionalProposalInfo = additionalProposalInfo;
183 // * @see ICompletionProposal#apply
185 // public void apply(IDocument document) {
187 // document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
188 // } catch (BadLocationException x) {
194 // * @see ICompletionProposal#getSelection
196 // public Point getSelection(IDocument document) {
197 // return new Point(fReplacementOffset + fCursorPosition, 0);
201 // * @see ICompletionProposal#getContextInformation()
203 // public IContextInformation getContextInformation() {
204 // return fContextInformation;
208 // * @see ICompletionProposal#getImage()
210 // public Image getImage() {
215 // * @see ICompletionProposal#getDisplayString()
217 // public String getDisplayString() {
218 // if (fDisplayString != null)
219 // return fDisplayString;
220 // return fReplacementString;
224 // * @see ICompletionProposal#getAdditionalProposalInfo()
226 // public String getAdditionalProposalInfo() {
227 // return fAdditionalProposalInfo;
230 // * Returns the relevance of the proposal.
232 // public int getRelevance() {
237 protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
239 private char[] fProposalAutoActivationSet;
240 protected IContextInformationValidator fValidator = new Validator();
241 private TemplateEngine fTemplateEngine;
242 private PHPCompletionProposalComparator fComparator;
243 private int fNumberOfComputedResults = 0;
245 public PHPCompletionProcessor() {
247 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
248 if (contextType != null)
249 fTemplateEngine = new TemplateEngine(contextType);
251 fComparator = new PHPCompletionProposalComparator();
255 * Tells this processor to order the proposals alphabetically.
257 * @param order <code>true</code> if proposals should be ordered.
259 public void orderProposalsAlphabetically(boolean order) {
260 fComparator.setOrderAlphabetically(order);
264 * Sets this processor's set of characters triggering the activation of the
265 * completion proposal computation.
267 * @param activationSet the activation set
269 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
270 fProposalAutoActivationSet = activationSet;
273 * Method declared on IContentAssistProcessor
275 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
276 // IDocument document = viewer.getDocument();
277 // if (documentOffset > 0) {
279 // ICompletionProposal[] result;
280 // char character = document.getChar(documentOffset - 1);
281 // if (character == '$') {
282 ////viewer. .getActivePage().getActiveEditor();
283 // result = new ICompletionProposal[fgProposals.length];
284 // for (int i = 0; i < fgProposals.length; i++) {
285 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
286 // 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$
290 // } catch (BadLocationException e) {
291 // return new ICompletionProposal[0];
295 // ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
296 // for (int i = 0; i < fgProposals.length; i++) {
297 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
298 // 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$
301 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
302 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
306 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
307 IDocument document = viewer.getDocument();
308 Object[] identifiers = null;
311 PHPEditor editor = null;
312 AbstractContentOutlinePage outlinePage = null;
314 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
315 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
316 editor = (PHPEditor) targetEditor;
317 outlinePage = editor.getfOutlinePage();
318 if (outlinePage instanceof PHPContentOutlinePage) {
319 identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
324 if (fTemplateEngine != null) {
325 ICompletionProposal[] results;
327 fTemplateEngine.reset();
328 fTemplateEngine.complete(viewer, offset); //, unit);
329 // } catch (JavaModelException x) {
330 // Shell shell= viewer.getTextWidget().getShell();
331 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
334 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
336 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
337 if (identifiers != null) {
338 IdentifierEngine identifierEngine;
341 // for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
342 // proposal = (String) variables[j++];
343 // IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
344 // results[i] = new VariablesCompletionProposal(proposal, offset, 0, proposal.length(), null, proposal, info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
347 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
348 if (contextType != null) {
349 identifierEngine = new IdentifierEngine(contextType);
350 identifierEngine.complete(viewer, offset, identifiers);
351 identifierResults = identifierEngine.getResults();
355 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
356 if (PHPFunctionNames.FUNCTION_NAMES != null) {
357 BuiltInEngine builtinEngine;
360 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
361 if (contextType != null) {
362 builtinEngine = new BuiltInEngine(contextType);
363 builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
364 builtinResults = builtinEngine.getResults();
368 // concatenate arrays
369 IPHPCompletionProposal[] total;
370 total = new IPHPCompletionProposal[templateResults.length + identifierResults.length + builtinResults.length];
371 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
372 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
373 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
376 fNumberOfComputedResults = (results == null ? 0 : results.length);
378 * Order here and not in result collector to make sure that the order
379 * applies to all proposals and not just those of the compilation unit.
381 return order(results);
383 return new IPHPCompletionProposal[0];
386 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
387 int contextPosition = offset;
389 IDocument document = viewer.getDocument();
393 // PHPCodeReader reader= new PHPCodeReader();
394 // reader.configureBackwardReader(document, offset, true, true);
396 // int nestingLevel= 0;
398 // int curr= reader.read();
399 // while (curr != PHPCodeReader.EOF) {
401 // if (')' == (char) curr)
404 // else if ('(' == (char) curr) {
407 // if (nestingLevel < 0) {
408 // int start= reader.getOffset();
409 // if (looksLikeMethod(reader))
414 // curr= reader.read();
416 // } catch (IOException e) {
419 return contextPosition;
423 * Method declared on IContentAssistProcessor
425 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
426 // IContextInformation[] result = new IContextInformation[5];
427 // for (int i = 0; i < result.length; i++)
428 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
429 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
433 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
435 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
436 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
437 List result = addContextInformations(viewer, contextInformationPosition);
438 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
441 private List addContextInformations(ITextViewer viewer, int offset) {
442 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
444 List result = new ArrayList();
445 for (int i = 0; i < proposals.length; i++) {
446 IContextInformation contextInformation = proposals[i].getContextInformation();
447 if (contextInformation != null) {
448 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
449 wrapper.setContextInformationPosition(offset);
457 * Order the given proposals.
459 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
460 Arrays.sort(proposals, fComparator);
465 * Method declared on IContentAssistProcessor
467 public char[] getCompletionProposalAutoActivationCharacters() {
468 return fProposalAutoActivationSet;
469 // return null; // new char[] { '$' };
473 * Method declared on IContentAssistProcessor
475 public char[] getContextInformationAutoActivationCharacters() {
481 * Method declared on IContentAssistProcessor
483 public IContextInformationValidator getContextInformationValidator() {
488 * Method declared on IContentAssistProcessor
490 public String getErrorMessage() {