Improved CodeCompletion with information from the project.index file.
[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 import java.util.SortedMap;
18
19 import net.sourceforge.phpdt.internal.corext.template.ContextType;
20 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
21 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
22 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
23 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
24 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
25 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
26 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
27 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
28 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
29 import net.sourceforge.phpeclipse.phpeditor.AbstractContentOutlinePage;
30 import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage;
31 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
32
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IProject;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.ITextViewer;
37 import org.eclipse.jface.text.TextPresentation;
38 import org.eclipse.jface.text.contentassist.ICompletionProposal;
39 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
40 import org.eclipse.jface.text.contentassist.IContextInformation;
41 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
42 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
43 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.ui.IEditorPart;
46 import org.eclipse.ui.IFileEditorInput;
47
48 /**
49  * Example PHP completion processor.
50  */
51 public class PHPCompletionProcessor implements IContentAssistProcessor {
52
53   /**
54    * Simple content assist tip closer. The tip is valid in a range
55    * of 5 characters around its popup location.
56    */
57   protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
58
59     protected int fInstallOffset;
60
61     /*
62      * @see IContextInformationValidator#isContextInformationValid(int)
63      */
64     public boolean isContextInformationValid(int offset) {
65       return Math.abs(fInstallOffset - offset) < 5;
66     }
67
68     /*
69      * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
70      */
71     public void install(IContextInformation info, ITextViewer viewer, int offset) {
72       fInstallOffset = offset;
73     }
74
75     /*
76      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
77      */
78     public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
79       return false;
80     }
81   };
82
83   private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
84
85     private final IContextInformation fContextInformation;
86     private int fPosition;
87
88     public ContextInformationWrapper(IContextInformation contextInformation) {
89       fContextInformation = contextInformation;
90     }
91
92     /*
93      * @see IContextInformation#getContextDisplayString()
94      */
95     public String getContextDisplayString() {
96       return fContextInformation.getContextDisplayString();
97     }
98
99     /*
100     * @see IContextInformation#getImage()
101     */
102     public Image getImage() {
103       return fContextInformation.getImage();
104     }
105
106     /*
107      * @see IContextInformation#getInformationDisplayString()
108      */
109     public String getInformationDisplayString() {
110       return fContextInformation.getInformationDisplayString();
111     }
112
113     /*
114      * @see IContextInformationExtension#getContextInformationPosition()
115      */
116     public int getContextInformationPosition() {
117       return fPosition;
118     }
119
120     public void setContextInformationPosition(int position) {
121       fPosition = position;
122     }
123   };
124
125   //  public final class VariablesCompletionProposal implements IJavaCompletionProposal {
126   //    private String fDisplayString;
127   //    private String fReplacementString;
128   //    private int fReplacementOffset;
129   //    private int fReplacementLength;
130   //    private int fCursorPosition;
131   //    private Image fImage;
132   //    private IContextInformation fContextInformation;
133   //    private String fAdditionalProposalInfo;
134   //
135   //    /**
136   //     * Creates a new completion proposal based on the provided information.  The replacement string is
137   //     * considered being the display string too. All remaining fields are set to <code>null</code>.
138   //     *
139   //     * @param replacementString the actual string to be inserted into the document
140   //     * @param replacementOffset the offset of the text to be replaced
141   //     * @param replacementLength the length of the text to be replaced
142   //     * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
143   //     */
144   //    public VariablesCompletionProposal(
145   //      String replacementString,
146   //      int replacementOffset,
147   //      int replacementLength,
148   //      int cursorPosition) {
149   //      this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
150   //    }
151   //
152   //    /**
153   //     * Creates a new completion proposal. All fields are initialized based on the provided information.
154   //     *
155   //     * @param replacementString the actual string to be inserted into the document
156   //     * @param replacementOffset the offset of the text to be replaced
157   //     * @param replacementLength the length of the text to be replaced
158   //     * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
159   //     * @param image the image to display for this proposal
160   //     * @param displayString the string to be displayed for the proposal
161   //     * @param contentInformation the context information associated with this proposal
162   //     * @param additionalProposalInfo the additional information associated with this proposal
163   //     */
164   //    public VariablesCompletionProposal(
165   //      String replacementString,
166   //      int replacementOffset,
167   //      int replacementLength,
168   //      int cursorPosition,
169   //      Image image,
170   //      String displayString,
171   //      IContextInformation contextInformation,
172   //      String additionalProposalInfo) {
173   //      //      Assert.isNotNull(replacementString);
174   //      //      Assert.isTrue(replacementOffset >= 0);
175   //      //      Assert.isTrue(replacementLength >= 0);
176   //      //      Assert.isTrue(cursorPosition >= 0);
177   //
178   //      fReplacementString = replacementString;
179   //      fReplacementOffset = replacementOffset;
180   //      fReplacementLength = replacementLength;
181   //      fCursorPosition = cursorPosition;
182   //      fImage = image;
183   //      fDisplayString = displayString;
184   //      fContextInformation = contextInformation;
185   //      fAdditionalProposalInfo = additionalProposalInfo;
186   //    }
187   //
188   //    /*
189   //     * @see ICompletionProposal#apply
190   //     */
191   //    public void apply(IDocument document) {
192   //      try {
193   //        document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
194   //      } catch (BadLocationException x) {
195   //        // ignore
196   //      }
197   //    }
198   //
199   //    /*
200   //     * @see ICompletionProposal#getSelection
201   //     */
202   //    public Point getSelection(IDocument document) {
203   //      return new Point(fReplacementOffset + fCursorPosition, 0);
204   //    }
205   //
206   //    /*
207   //     * @see ICompletionProposal#getContextInformation()
208   //     */
209   //    public IContextInformation getContextInformation() {
210   //      return fContextInformation;
211   //    }
212   //
213   //    /*
214   //     * @see ICompletionProposal#getImage()
215   //     */
216   //    public Image getImage() {
217   //      return fImage;
218   //    }
219   //
220   //    /*
221   //     * @see ICompletionProposal#getDisplayString()
222   //     */
223   //    public String getDisplayString() {
224   //      if (fDisplayString != null)
225   //        return fDisplayString;
226   //      return fReplacementString;
227   //    }
228   //
229   //    /*
230   //     * @see ICompletionProposal#getAdditionalProposalInfo()
231   //     */
232   //    public String getAdditionalProposalInfo() {
233   //      return fAdditionalProposalInfo;
234   //    }
235   //    /**
236   //    * Returns the relevance of the proposal.
237   //    */
238   //    public int getRelevance() {
239   //      return 0;
240   //    }
241   //  }
242
243   protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
244
245   private char[] fProposalAutoActivationSet;
246   protected IContextInformationValidator fValidator = new Validator();
247   private TemplateEngine fTemplateEngine;
248   private PHPCompletionProposalComparator fComparator;
249   private int fNumberOfComputedResults = 0;
250
251   public PHPCompletionProcessor() {
252
253     ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
254     if (contextType != null)
255       fTemplateEngine = new TemplateEngine(contextType);
256
257     fComparator = new PHPCompletionProposalComparator();
258   }
259
260   /**
261    * Tells this processor to order the proposals alphabetically.
262    * 
263    * @param order <code>true</code> if proposals should be ordered.
264    */
265   public void orderProposalsAlphabetically(boolean order) {
266     fComparator.setOrderAlphabetically(order);
267   }
268
269   /**
270    * Sets this processor's set of characters triggering the activation of the
271    * completion proposal computation.
272    * 
273    * @param activationSet the activation set
274    */
275   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
276     fProposalAutoActivationSet = activationSet;
277   }
278   /* (non-Javadoc)
279    * Method declared on IContentAssistProcessor
280    */
281   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
282     //    IDocument document = viewer.getDocument();
283     //    if (documentOffset > 0) {
284     //      try {
285     //        ICompletionProposal[] result;
286     //        char character = document.getChar(documentOffset - 1);
287     //        if (character == '$') {
288     ////viewer.  .getActivePage().getActiveEditor();
289     //          result = new ICompletionProposal[fgProposals.length];
290     //          for (int i = 0; i < fgProposals.length; i++) {
291     //            IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
292     //            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$
293     //          }
294     //          return result;
295     //        }
296     //      } catch (BadLocationException e) {
297     //        return new ICompletionProposal[0];
298     //      }
299     //    }
300     //
301     //    ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
302     //    for (int i = 0; i < fgProposals.length; i++) {
303     //      IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
304     //      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$
305     //    }
306     //    return result;
307     int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
308     return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
309
310   }
311
312   private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
313     IDocument document = viewer.getDocument();
314     Object[] identifiers = null;
315     IProject project = null;
316     if (offset > 0) {
317
318       PHPEditor editor = null;
319       AbstractContentOutlinePage outlinePage = null;
320
321       IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
322       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
323         editor = (PHPEditor) targetEditor;
324         IFile f = ((IFileEditorInput) editor.getEditorInput()).getFile();
325         project = f.getProject();
326         outlinePage = editor.getfOutlinePage();
327         if (outlinePage instanceof PHPContentOutlinePage) {
328           identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
329         }
330       }
331     }
332
333     if (fTemplateEngine != null) {
334       ICompletionProposal[] results;
335       //      try {
336       fTemplateEngine.reset();
337       fTemplateEngine.complete(viewer, offset); //, unit);
338       //      } catch (JavaModelException x) {
339       //        Shell shell= viewer.getTextWidget().getShell();
340       //        ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
341       //      }       
342
343       IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
344
345       IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
346       if (identifiers != null) {
347         IdentifierEngine identifierEngine;
348         String proposal;
349         // int j = 0;
350         //        for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
351         //          proposal = (String) variables[j++];
352         //          IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
353         //          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$
354         //        }
355
356         ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
357         if (contextType != null) {
358           identifierEngine = new IdentifierEngine(contextType);
359           identifierEngine.complete(viewer, offset, identifiers);
360           identifierResults = identifierEngine.getResults();
361         }
362       }
363
364       IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
365       if (project != null) {
366         DeclarationEngine identifierEngine;
367         String proposal;
368
369         ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
370         if (contextType != null) {
371           IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
372           SortedMap sortedMap = indexManager.getIdentifierMap();
373
374           identifierEngine = new DeclarationEngine(contextType);
375           identifierEngine.complete(viewer, offset, sortedMap);
376           identifierResults = identifierEngine.getResults();
377         }
378       }
379
380       // built in function names from phpsyntax.xml
381       IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
382       if (PHPFunctionNames.FUNCTION_NAMES != null) {
383         BuiltInEngine builtinEngine;
384         String proposal;
385
386         ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
387         if (contextType != null) {
388           builtinEngine = new BuiltInEngine(contextType);
389           // TODO PHPFunctionNames.FUNCTION_NAMES should be taken from phpsyntax.xml
390           builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
391           builtinResults = builtinEngine.getResults();
392         }
393       }
394
395       // concatenate arrays
396       IPHPCompletionProposal[] total;
397       total =
398         new IPHPCompletionProposal[templateResults.length
399           + identifierResults.length
400           + builtinResults.length
401           + declarationResults.length];
402       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
403       System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
404       System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
405       System.arraycopy(
406         declarationResults,
407         0,
408         total,
409         templateResults.length + identifierResults.length + builtinResults.length,
410         declarationResults.length);
411
412       results = total;
413
414       fNumberOfComputedResults = (results == null ? 0 : results.length);
415       /*
416        * Order here and not in result collector to make sure that the order
417        * applies to all proposals and not just those of the compilation unit. 
418        */
419       return order(results);
420     }
421     return new IPHPCompletionProposal[0];
422   }
423
424   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
425     int contextPosition = offset;
426
427     IDocument document = viewer.getDocument();
428
429     //    try {
430     //
431     //      PHPCodeReader reader= new PHPCodeReader();
432     //      reader.configureBackwardReader(document, offset, true, true);
433     //  
434     //      int nestingLevel= 0;
435     //
436     //      int curr= reader.read();    
437     //      while (curr != PHPCodeReader.EOF) {
438     //
439     //        if (')' == (char) curr)
440     //          ++ nestingLevel;
441     //
442     //        else if ('(' == (char) curr) {
443     //          -- nestingLevel;
444     //        
445     //          if (nestingLevel < 0) {
446     //            int start= reader.getOffset();
447     //            if (looksLikeMethod(reader))
448     //              return start + 1;
449     //          } 
450     //        }
451     //
452     //        curr= reader.read();          
453     //      }
454     //    } catch (IOException e) {
455     //    }
456
457     return contextPosition;
458   }
459
460   /* (non-Javadoc)
461    * Method declared on IContentAssistProcessor
462    */
463   //  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
464   //    IContextInformation[] result = new IContextInformation[5];
465   //    for (int i = 0; i < result.length; i++)
466   //      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
467   //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
468   //    return result;
469   //  }
470   /**
471    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
472    */
473   public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
474     int contextInformationPosition = guessContextInformationPosition(viewer, offset);
475     List result = addContextInformations(viewer, contextInformationPosition);
476     return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
477   }
478
479   private List addContextInformations(ITextViewer viewer, int offset) {
480     ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
481
482     List result = new ArrayList();
483     for (int i = 0; i < proposals.length; i++) {
484       IContextInformation contextInformation = proposals[i].getContextInformation();
485       if (contextInformation != null) {
486         ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
487         wrapper.setContextInformationPosition(offset);
488         result.add(wrapper);
489       }
490     }
491     return result;
492   }
493
494   /**
495    * Order the given proposals.
496    */
497   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
498     Arrays.sort(proposals, fComparator);
499     return proposals;
500   }
501
502   /* (non-Javadoc)
503    * Method declared on IContentAssistProcessor
504    */
505   public char[] getCompletionProposalAutoActivationCharacters() {
506     return fProposalAutoActivationSet;
507     //    return null; // new char[] { '$' };
508   }
509
510   /* (non-Javadoc)
511    * Method declared on IContentAssistProcessor
512    */
513   public char[] getContextInformationAutoActivationCharacters() {
514     return new char[] {
515     };
516   }
517
518   /* (non-Javadoc)
519    * Method declared on IContentAssistProcessor
520    */
521   public IContextInformationValidator getContextInformationValidator() {
522     return fValidator;
523   }
524
525   /* (non-Javadoc)
526    * Method declared on IContentAssistProcessor
527    */
528   public String getErrorMessage() {
529     return null;
530   }
531 }