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.PHPContentOutlinePage;
27 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.TextPresentation;
32 import org.eclipse.jface.text.contentassist.ICompletionProposal;
33 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
34 import org.eclipse.jface.text.contentassist.IContextInformation;
35 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
36 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
37 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.ui.IEditorPart;
42 * Example PHP completion processor.
44 public class PHPCompletionProcessor implements IContentAssistProcessor {
47 * Simple content assist tip closer. The tip is valid in a range
48 * of 5 characters around its popup location.
50 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
52 protected int fInstallOffset;
55 * @see IContextInformationValidator#isContextInformationValid(int)
57 public boolean isContextInformationValid(int offset) {
58 return Math.abs(fInstallOffset - offset) < 5;
62 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
64 public void install(IContextInformation info, ITextViewer viewer, int offset) {
65 fInstallOffset = offset;
69 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
71 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
76 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
78 private final IContextInformation fContextInformation;
79 private int fPosition;
81 public ContextInformationWrapper(IContextInformation contextInformation) {
82 fContextInformation = contextInformation;
86 * @see IContextInformation#getContextDisplayString()
88 public String getContextDisplayString() {
89 return fContextInformation.getContextDisplayString();
93 * @see IContextInformation#getImage()
95 public Image getImage() {
96 return fContextInformation.getImage();
100 * @see IContextInformation#getInformationDisplayString()
102 public String getInformationDisplayString() {
103 return fContextInformation.getInformationDisplayString();
107 * @see IContextInformationExtension#getContextInformationPosition()
109 public int getContextInformationPosition() {
113 public void setContextInformationPosition(int position) {
114 fPosition = position;
118 // public final class VariablesCompletionProposal implements IJavaCompletionProposal {
119 // private String fDisplayString;
120 // private String fReplacementString;
121 // private int fReplacementOffset;
122 // private int fReplacementLength;
123 // private int fCursorPosition;
124 // private Image fImage;
125 // private IContextInformation fContextInformation;
126 // private String fAdditionalProposalInfo;
129 // * Creates a new completion proposal based on the provided information. The replacement string is
130 // * considered being the display string too. All remaining fields are set to <code>null</code>.
132 // * @param replacementString the actual string to be inserted into the document
133 // * @param replacementOffset the offset of the text to be replaced
134 // * @param replacementLength the length of the text to be replaced
135 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
137 // public VariablesCompletionProposal(
138 // String replacementString,
139 // int replacementOffset,
140 // int replacementLength,
141 // int cursorPosition) {
142 // this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
146 // * Creates a new completion proposal. All fields are initialized based on the provided information.
148 // * @param replacementString the actual string to be inserted into the document
149 // * @param replacementOffset the offset of the text to be replaced
150 // * @param replacementLength the length of the text to be replaced
151 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
152 // * @param image the image to display for this proposal
153 // * @param displayString the string to be displayed for the proposal
154 // * @param contentInformation the context information associated with this proposal
155 // * @param additionalProposalInfo the additional information associated with this proposal
157 // public VariablesCompletionProposal(
158 // String replacementString,
159 // int replacementOffset,
160 // int replacementLength,
161 // int cursorPosition,
163 // String displayString,
164 // IContextInformation contextInformation,
165 // String additionalProposalInfo) {
166 // // Assert.isNotNull(replacementString);
167 // // Assert.isTrue(replacementOffset >= 0);
168 // // Assert.isTrue(replacementLength >= 0);
169 // // Assert.isTrue(cursorPosition >= 0);
171 // fReplacementString = replacementString;
172 // fReplacementOffset = replacementOffset;
173 // fReplacementLength = replacementLength;
174 // fCursorPosition = cursorPosition;
176 // fDisplayString = displayString;
177 // fContextInformation = contextInformation;
178 // fAdditionalProposalInfo = additionalProposalInfo;
182 // * @see ICompletionProposal#apply
184 // public void apply(IDocument document) {
186 // document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
187 // } catch (BadLocationException x) {
193 // * @see ICompletionProposal#getSelection
195 // public Point getSelection(IDocument document) {
196 // return new Point(fReplacementOffset + fCursorPosition, 0);
200 // * @see ICompletionProposal#getContextInformation()
202 // public IContextInformation getContextInformation() {
203 // return fContextInformation;
207 // * @see ICompletionProposal#getImage()
209 // public Image getImage() {
214 // * @see ICompletionProposal#getDisplayString()
216 // public String getDisplayString() {
217 // if (fDisplayString != null)
218 // return fDisplayString;
219 // return fReplacementString;
223 // * @see ICompletionProposal#getAdditionalProposalInfo()
225 // public String getAdditionalProposalInfo() {
226 // return fAdditionalProposalInfo;
229 // * Returns the relevance of the proposal.
231 // public int getRelevance() {
236 protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
238 private char[] fProposalAutoActivationSet;
239 protected IContextInformationValidator fValidator = new Validator();
240 private TemplateEngine fTemplateEngine;
241 private PHPCompletionProposalComparator fComparator;
242 private int fNumberOfComputedResults = 0;
244 public PHPCompletionProcessor() {
246 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
247 if (contextType != null)
248 fTemplateEngine = new TemplateEngine(contextType);
250 fComparator = new PHPCompletionProposalComparator();
254 * Tells this processor to order the proposals alphabetically.
256 * @param order <code>true</code> if proposals should be ordered.
258 public void orderProposalsAlphabetically(boolean order) {
259 fComparator.setOrderAlphabetically(order);
263 * Sets this processor's set of characters triggering the activation of the
264 * completion proposal computation.
266 * @param activationSet the activation set
268 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
269 fProposalAutoActivationSet= activationSet;
272 * Method declared on IContentAssistProcessor
274 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
275 // IDocument document = viewer.getDocument();
276 // if (documentOffset > 0) {
278 // ICompletionProposal[] result;
279 // char character = document.getChar(documentOffset - 1);
280 // if (character == '$') {
281 ////viewer. .getActivePage().getActiveEditor();
282 // result = new ICompletionProposal[fgProposals.length];
283 // for (int i = 0; i < fgProposals.length; i++) {
284 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
285 // 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$
289 // } catch (BadLocationException e) {
290 // return new ICompletionProposal[0];
294 // ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
295 // for (int i = 0; i < fgProposals.length; i++) {
296 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
297 // 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$
300 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
301 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
305 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
306 IDocument document = viewer.getDocument();
307 Object[] identifiers = null;
310 PHPEditor editor = null;
311 PHPContentOutlinePage outlinePage = null;
313 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
314 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
315 editor = (PHPEditor) targetEditor;
316 outlinePage = editor.getfOutlinePage();
317 identifiers = outlinePage.getVariables();
321 if (fTemplateEngine != null) {
322 ICompletionProposal[] results;
324 fTemplateEngine.reset();
325 fTemplateEngine.complete(viewer, offset); //, unit);
326 // } catch (JavaModelException x) {
327 // Shell shell= viewer.getTextWidget().getShell();
328 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
331 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
333 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
334 if (identifiers != null) {
335 IdentifierEngine identifierEngine;
338 // for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
339 // proposal = (String) variables[j++];
340 // IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
341 // 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$
344 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
345 if (contextType != null) {
346 identifierEngine = new IdentifierEngine(contextType);
347 identifierEngine.complete(viewer, offset, identifiers);
348 identifierResults = identifierEngine.getResults();
352 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
353 if (PHPFunctionNames.FUNCTION_NAMES != null) {
354 BuiltInEngine builtinEngine;
357 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
358 if (contextType != null) {
359 builtinEngine = new BuiltInEngine(contextType);
360 builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
361 builtinResults = builtinEngine.getResults();
365 // concatenate arrays
366 IPHPCompletionProposal[] total;
367 total = new IPHPCompletionProposal[templateResults.length + identifierResults.length + builtinResults.length];
368 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
369 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
370 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
373 fNumberOfComputedResults = (results == null ? 0 : results.length);
375 * Order here and not in result collector to make sure that the order
376 * applies to all proposals and not just those of the compilation unit.
378 return order(results);
380 return new IPHPCompletionProposal[0];
383 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
384 int contextPosition = offset;
386 IDocument document = viewer.getDocument();
390 // PHPCodeReader reader= new PHPCodeReader();
391 // reader.configureBackwardReader(document, offset, true, true);
393 // int nestingLevel= 0;
395 // int curr= reader.read();
396 // while (curr != PHPCodeReader.EOF) {
398 // if (')' == (char) curr)
401 // else if ('(' == (char) curr) {
404 // if (nestingLevel < 0) {
405 // int start= reader.getOffset();
406 // if (looksLikeMethod(reader))
411 // curr= reader.read();
413 // } catch (IOException e) {
416 return contextPosition;
420 * Method declared on IContentAssistProcessor
422 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
423 // IContextInformation[] result = new IContextInformation[5];
424 // for (int i = 0; i < result.length; i++)
425 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
426 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
430 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
432 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
433 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
434 List result = addContextInformations(viewer, contextInformationPosition);
435 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
438 private List addContextInformations(ITextViewer viewer, int offset) {
439 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
441 List result = new ArrayList();
442 for (int i = 0; i < proposals.length; i++) {
443 IContextInformation contextInformation = proposals[i].getContextInformation();
444 if (contextInformation != null) {
445 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
446 wrapper.setContextInformationPosition(offset);
454 * Order the given proposals.
456 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
457 Arrays.sort(proposals, fComparator);
462 * Method declared on IContentAssistProcessor
464 public char[] getCompletionProposalAutoActivationCharacters() {
465 return fProposalAutoActivationSet;
466 // return null; // new char[] { '$' };
470 * Method declared on IContentAssistProcessor
472 public char[] getContextInformationAutoActivationCharacters() {
478 * Method declared on IContentAssistProcessor
480 public IContextInformationValidator getContextInformationValidator() {
485 * Method declared on IContentAssistProcessor
487 public String getErrorMessage() {