Added auto activation for HTML characters (i.e. <&#) in the Preference Page
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCompletionProcessor.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
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;
28
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;
40
41 /**
42  * Example PHP completion processor.
43  */
44 public class PHPCompletionProcessor implements IContentAssistProcessor {
45
46   /**
47    * Simple content assist tip closer. The tip is valid in a range
48    * of 5 characters around its popup location.
49    */
50   protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
51
52     protected int fInstallOffset;
53
54     /*
55      * @see IContextInformationValidator#isContextInformationValid(int)
56      */
57     public boolean isContextInformationValid(int offset) {
58       return Math.abs(fInstallOffset - offset) < 5;
59     }
60
61     /*
62      * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
63      */
64     public void install(IContextInformation info, ITextViewer viewer, int offset) {
65       fInstallOffset = offset;
66     }
67
68     /*
69      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
70      */
71     public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
72       return false;
73     }
74   };
75
76   private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
77
78     private final IContextInformation fContextInformation;
79     private int fPosition;
80
81     public ContextInformationWrapper(IContextInformation contextInformation) {
82       fContextInformation = contextInformation;
83     }
84
85     /*
86      * @see IContextInformation#getContextDisplayString()
87      */
88     public String getContextDisplayString() {
89       return fContextInformation.getContextDisplayString();
90     }
91
92     /*
93     * @see IContextInformation#getImage()
94     */
95     public Image getImage() {
96       return fContextInformation.getImage();
97     }
98
99     /*
100      * @see IContextInformation#getInformationDisplayString()
101      */
102     public String getInformationDisplayString() {
103       return fContextInformation.getInformationDisplayString();
104     }
105
106     /*
107      * @see IContextInformationExtension#getContextInformationPosition()
108      */
109     public int getContextInformationPosition() {
110       return fPosition;
111     }
112
113     public void setContextInformationPosition(int position) {
114       fPosition = position;
115     }
116   };
117
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;
127   //
128   //    /**
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>.
131   //     *
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
136   //     */
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);
143   //    }
144   //
145   //    /**
146   //     * Creates a new completion proposal. All fields are initialized based on the provided information.
147   //     *
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
156   //     */
157   //    public VariablesCompletionProposal(
158   //      String replacementString,
159   //      int replacementOffset,
160   //      int replacementLength,
161   //      int cursorPosition,
162   //      Image image,
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);
170   //
171   //      fReplacementString = replacementString;
172   //      fReplacementOffset = replacementOffset;
173   //      fReplacementLength = replacementLength;
174   //      fCursorPosition = cursorPosition;
175   //      fImage = image;
176   //      fDisplayString = displayString;
177   //      fContextInformation = contextInformation;
178   //      fAdditionalProposalInfo = additionalProposalInfo;
179   //    }
180   //
181   //    /*
182   //     * @see ICompletionProposal#apply
183   //     */
184   //    public void apply(IDocument document) {
185   //      try {
186   //        document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
187   //      } catch (BadLocationException x) {
188   //        // ignore
189   //      }
190   //    }
191   //
192   //    /*
193   //     * @see ICompletionProposal#getSelection
194   //     */
195   //    public Point getSelection(IDocument document) {
196   //      return new Point(fReplacementOffset + fCursorPosition, 0);
197   //    }
198   //
199   //    /*
200   //     * @see ICompletionProposal#getContextInformation()
201   //     */
202   //    public IContextInformation getContextInformation() {
203   //      return fContextInformation;
204   //    }
205   //
206   //    /*
207   //     * @see ICompletionProposal#getImage()
208   //     */
209   //    public Image getImage() {
210   //      return fImage;
211   //    }
212   //
213   //    /*
214   //     * @see ICompletionProposal#getDisplayString()
215   //     */
216   //    public String getDisplayString() {
217   //      if (fDisplayString != null)
218   //        return fDisplayString;
219   //      return fReplacementString;
220   //    }
221   //
222   //    /*
223   //     * @see ICompletionProposal#getAdditionalProposalInfo()
224   //     */
225   //    public String getAdditionalProposalInfo() {
226   //      return fAdditionalProposalInfo;
227   //    }
228   //    /**
229   //    * Returns the relevance of the proposal.
230   //    */
231   //    public int getRelevance() {
232   //      return 0;
233   //    }
234   //  }
235
236   protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
237
238   private char[] fProposalAutoActivationSet;
239   protected IContextInformationValidator fValidator = new Validator();
240   private TemplateEngine fTemplateEngine;
241   private PHPCompletionProposalComparator fComparator;
242   private int fNumberOfComputedResults = 0;
243
244   public PHPCompletionProcessor() {
245
246     ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
247     if (contextType != null)
248       fTemplateEngine = new TemplateEngine(contextType);
249
250     fComparator = new PHPCompletionProposalComparator();
251   }
252   
253   /**
254    * Tells this processor to order the proposals alphabetically.
255    * 
256    * @param order <code>true</code> if proposals should be ordered.
257    */
258   public void orderProposalsAlphabetically(boolean order) {
259     fComparator.setOrderAlphabetically(order);
260   }
261   
262   /**
263    * Sets this processor's set of characters triggering the activation of the
264    * completion proposal computation.
265    * 
266    * @param activationSet the activation set
267    */
268   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
269     fProposalAutoActivationSet= activationSet;
270   }
271   /* (non-Javadoc)
272    * Method declared on IContentAssistProcessor
273    */
274   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
275     //    IDocument document = viewer.getDocument();
276     //    if (documentOffset > 0) {
277     //      try {
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$
286     //          }
287     //          return result;
288     //        }
289     //      } catch (BadLocationException e) {
290     //        return new ICompletionProposal[0];
291     //      }
292     //    }
293     //
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$
298     //    }
299     //    return result;
300     int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
301     return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
302
303   }
304
305   private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
306     IDocument document = viewer.getDocument();
307     Object[] identifiers = null;
308     if (offset > 0) {
309
310       PHPEditor editor = null;
311       PHPContentOutlinePage outlinePage = null;
312
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();
318       }
319     }
320
321     if (fTemplateEngine != null) {
322       ICompletionProposal[] results;
323       //      try {
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$
329       //      }       
330
331       IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
332
333       IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
334       if (identifiers != null) {
335         IdentifierEngine identifierEngine;
336         String proposal;
337         // int j = 0;
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$
342         //        }
343
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();
349         }
350       }
351
352       IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
353       if (PHPFunctionNames.FUNCTION_NAMES != null) {
354         BuiltInEngine builtinEngine;
355         String proposal;
356
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();
362         }
363       }
364
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);
371       results = total;
372
373       fNumberOfComputedResults = (results == null ? 0 : results.length);
374       /*
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. 
377        */
378       return order(results);
379     }
380     return new IPHPCompletionProposal[0];
381   }
382
383   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
384     int contextPosition = offset;
385
386     IDocument document = viewer.getDocument();
387
388     //    try {
389     //
390     //      PHPCodeReader reader= new PHPCodeReader();
391     //      reader.configureBackwardReader(document, offset, true, true);
392     //  
393     //      int nestingLevel= 0;
394     //
395     //      int curr= reader.read();    
396     //      while (curr != PHPCodeReader.EOF) {
397     //
398     //        if (')' == (char) curr)
399     //          ++ nestingLevel;
400     //
401     //        else if ('(' == (char) curr) {
402     //          -- nestingLevel;
403     //        
404     //          if (nestingLevel < 0) {
405     //            int start= reader.getOffset();
406     //            if (looksLikeMethod(reader))
407     //              return start + 1;
408     //          } 
409     //        }
410     //
411     //        curr= reader.read();          
412     //      }
413     //    } catch (IOException e) {
414     //    }
415
416     return contextPosition;
417   }
418
419   /* (non-Javadoc)
420    * Method declared on IContentAssistProcessor
421    */
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$
427   //    return result;
428   //  }
429   /**
430    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
431    */
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()]);
436   }
437
438   private List addContextInformations(ITextViewer viewer, int offset) {
439     ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
440
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);
447         result.add(wrapper);
448       }
449     }
450     return result;
451   }
452
453   /**
454    * Order the given proposals.
455    */
456   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
457     Arrays.sort(proposals, fComparator);
458     return proposals;
459   }
460
461   /* (non-Javadoc)
462    * Method declared on IContentAssistProcessor
463    */
464   public char[] getCompletionProposalAutoActivationCharacters() {
465     return fProposalAutoActivationSet;
466 //    return null; // new char[] { '$' };
467   }
468
469   /* (non-Javadoc)
470    * Method declared on IContentAssistProcessor
471    */
472   public char[] getContextInformationAutoActivationCharacters() {
473     return new char[] {
474     };
475   }
476
477   /* (non-Javadoc)
478    * Method declared on IContentAssistProcessor
479    */
480   public IContextInformationValidator getContextInformationValidator() {
481     return fValidator;
482   }
483
484   /* (non-Javadoc)
485    * Method declared on IContentAssistProcessor
486    */
487   public String getErrorMessage() {
488     return null;
489   }
490 }