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