first version of 'Code Assist" template engine
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCompletionProcessor.java
index 6fd4ce7..c7ad06a 100644 (file)
@@ -13,7 +13,14 @@ package net.sourceforge.phpeclipse.phpeditor.php;
 
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
+import net.sourceforge.phpdt.internal.corext.template.ContextType;
+import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
+import net.sourceforge.phpdt.internal.ui.text.java.IJavaCompletionProposal;
+import net.sourceforge.phpdt.internal.ui.text.java.JavaCompletionProposalComparator;
+import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextViewer;
@@ -23,9 +30,10 @@ import org.eclipse.jface.text.contentassist.ContextInformation;
 import org.eclipse.jface.text.contentassist.ICompletionProposal;
 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.jface.text.contentassist.IContextInformationExtension;
 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
-import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Image;
 
 /**
  * Example PHP completion processor.
@@ -62,6 +70,48 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
     }
   };
 
+  private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
+
+    private final IContextInformation fContextInformation;
+    private int fPosition;
+
+    public ContextInformationWrapper(IContextInformation contextInformation) {
+      fContextInformation = contextInformation;
+    }
+
+    /*
+     * @see IContextInformation#getContextDisplayString()
+     */
+    public String getContextDisplayString() {
+      return fContextInformation.getContextDisplayString();
+    }
+
+    /*
+    * @see IContextInformation#getImage()
+    */
+    public Image getImage() {
+      return fContextInformation.getImage();
+    }
+
+    /*
+     * @see IContextInformation#getInformationDisplayString()
+     */
+    public String getInformationDisplayString() {
+      return fContextInformation.getInformationDisplayString();
+    }
+
+    /*
+     * @see IContextInformationExtension#getContextInformationPosition()
+     */
+    public int getContextInformationPosition() {
+      return fPosition;
+    }
+
+    public void setContextInformationPosition(int position) {
+      fPosition = position;
+    }
+  };
+
   protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
   //    {
   //      "array",
@@ -101,51 +151,64 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
   //      "var" };
 
   protected IContextInformationValidator fValidator = new Validator();
+  private TemplateEngine fTemplateEngine;
+  private JavaCompletionProposalComparator fComparator;
+  private int fNumberOfComputedResults = 0;
+
+  public PHPCompletionProcessor() {
 
+    ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
+    if (contextType != null)
+      fTemplateEngine = new TemplateEngine(contextType);
+
+    fComparator = new JavaCompletionProposalComparator();
+  }
   /* (non-Javadoc)
    * Method declared on IContentAssistProcessor
    */
   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
-
-    //    ArrayList arrList = new ArrayList(5);
     //    IDocument document = viewer.getDocument();
     //    if (documentOffset > 0) {
     //      try {
+    //        ICompletionProposal[] result;
     //        char character = document.getChar(documentOffset - 1);
-    //        Point point = PHPWordExtractor.findWord(document, documentOffset);
-    //        if (point != null) {
-    //          String word = document.get(point.x, point.y);
+    //        if (character == '$') {
+    ////viewer.  .getActivePage().getActiveEditor();
+    //          result = new ICompletionProposal[fgProposals.length];
     //          for (int i = 0; i < fgProposals.length; i++) {
-    //            if ((fgProposals[i].length() >= point.y) && fgProposals[i].substring(0, point.y).equals(word)) {
-    //              IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
-    //              arrList.add(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$
-    //            }
-    //          }
-    //          if (arrList.size() > 0) {
-    //            ICompletionProposal[] result = new ICompletionProposal[arrList.size()];
-    //            for (int i=0;i<arrList.size();i++) {
-    //              result[i] = (CompletionProposal) arrList.get(i);
-    //            }
-    //            return result;
+    //            IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
+    //            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$
     //          }
+    //          return result;
     //        }
     //      } catch (BadLocationException e) {
+    //        return new ICompletionProposal[0];
     //      }
+    //    }
     //
+    //    ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
+    //    for (int i = 0; i < fgProposals.length; i++) {
+    //      IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
+    //      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$
     //    }
+    //    return result;
+    int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
+    return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
+
+  }
 
-    ArrayList arrList = new ArrayList(5);
+  private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
     IDocument document = viewer.getDocument();
-    if (documentOffset > 0) {
+    if (offset > 0) {
       try {
         ICompletionProposal[] result;
-        char character = document.getChar(documentOffset - 1);
+        char character = document.getChar(offset - 1);
         if (character == '$') {
-//viewer.  .getActivePage().getActiveEditor();
+          //viewer.  .getActivePage().getActiveEditor();
           result = new ICompletionProposal[fgProposals.length];
           for (int i = 0; i < fgProposals.length; i++) {
             IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
-            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$
+            result[i] = new CompletionProposal(fgProposals[i], offset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
           }
           return result;
         }
@@ -154,26 +217,114 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
       }
     }
 
-    ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
-    for (int i = 0; i < fgProposals.length; i++) {
-      IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
-      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$
+    if (fTemplateEngine != null) {
+      IJavaCompletionProposal[] results;
+      //      try {
+      fTemplateEngine.reset();
+      fTemplateEngine.complete(viewer, offset); //, unit);
+      //      } catch (JavaModelException x) {
+      //        Shell shell= viewer.getTextWidget().getShell();
+      //        ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
+      //      }       
+
+      IJavaCompletionProposal[] templateResults = fTemplateEngine.getResults();
+
+      // concatenate arrays
+      IJavaCompletionProposal[] total = new IJavaCompletionProposal[templateResults.length]; // +results.length ];
+      System.arraycopy(templateResults, 0, total, 0, templateResults.length);
+      //      System.arraycopy(results, 0, total, templateResults.length, results.length);
+      results = total;
+      //    }
+
+      fNumberOfComputedResults = (results == null ? 0 : results.length);
+
+      /*
+       * Order here and not in result collector to make sure that the order
+       * applies to all proposals and not just those of the compilation unit. 
+       */
+      return order(results);
     }
+    return new IJavaCompletionProposal[0];
+  }
 
-    return result;
+  private int guessContextInformationPosition(ITextViewer viewer, int offset) {
+    int contextPosition = offset;
+
+    IDocument document = viewer.getDocument();
+
+    //    try {
+    //
+    //      JavaCodeReader reader= new JavaCodeReader();
+    //      reader.configureBackwardReader(document, offset, true, true);
+    //  
+    //      int nestingLevel= 0;
+    //
+    //      int curr= reader.read();    
+    //      while (curr != JavaCodeReader.EOF) {
+    //
+    //        if (')' == (char) curr)
+    //          ++ nestingLevel;
+    //
+    //        else if ('(' == (char) curr) {
+    //          -- nestingLevel;
+    //        
+    //          if (nestingLevel < 0) {
+    //            int start= reader.getOffset();
+    //            if (looksLikeMethod(reader))
+    //              return start + 1;
+    //          } 
+    //        }
+    //
+    //        curr= reader.read();          
+    //      }
+    //    } catch (IOException e) {
+    //    }
+
+    return contextPosition;
   }
 
   /* (non-Javadoc)
    * Method declared on IContentAssistProcessor
    */
-  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
-    IContextInformation[] result = new IContextInformation[5];
-    for (int i = 0; i < result.length; i++)
-      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
-      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
+  //  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
+  //    IContextInformation[] result = new IContextInformation[5];
+  //    for (int i = 0; i < result.length; i++)
+  //      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
+  //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
+  //    return result;
+  //  }
+  /**
+   * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
+   */
+  public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
+    int contextInformationPosition = guessContextInformationPosition(viewer, offset);
+    List result = addContextInformations(viewer, contextInformationPosition);
+    return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
+  }
+
+  private List addContextInformations(ITextViewer viewer, int offset) {
+    ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
+
+    List result = new ArrayList();
+    for (int i = 0; i < proposals.length; i++) {
+      IContextInformation contextInformation = proposals[i].getContextInformation();
+      if (contextInformation != null) {
+        ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
+        wrapper.setContextInformationPosition(offset);
+        result.add(wrapper);
+      }
+    }
     return result;
   }
 
+  /**
+   * Order the given proposals.
+   */
+  private ICompletionProposal[] order(ICompletionProposal[] proposals) {
+    Arrays.sort(proposals, fComparator);
+    return proposals;
+  }
+
   /* (non-Javadoc)
    * Method declared on IContentAssistProcessor
    */