misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCompletionProcessor.java
index a9b88fc..57e4372 100644 (file)
@@ -12,7 +12,10 @@ Contributors:
 package net.sourceforge.phpeclipse.phpeditor.php;
 
 import java.text.MessageFormat;
+import java.util.ArrayList;
 
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.jface.text.TextPresentation;
 import org.eclipse.jface.text.contentassist.CompletionProposal;
@@ -22,130 +25,176 @@ import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 import org.eclipse.jface.text.contentassist.IContextInformation;
 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
+import org.eclipse.swt.graphics.Point;
 
 /**
  * Example PHP completion processor.
  */
 public class PHPCompletionProcessor implements IContentAssistProcessor {
 
-       /**
-        * Simple content assist tip closer. The tip is valid in a range
-        * of 5 characters around its popup location.
-        */
-       protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
-
-               protected int fInstallOffset;
-
-               /*
-                * @see IContextInformationValidator#isContextInformationValid(int)
-                */
-               public boolean isContextInformationValid(int offset) {
-                       return Math.abs(fInstallOffset - offset) < 5;
-               }
-
-               /*
-                * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
-                */
-               public void install(IContextInformation info, ITextViewer viewer, int offset) {
-                       fInstallOffset = offset;
-               }
-
-               /*
-                * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
-                */
-               public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
-                       return false;
-               }
-       };
-
-       protected final static String[] fgProposals =
-               {
-                       "array",
-                       "break",
-                       "class",
-                       "continue",
-                       "do",
-                       "echo",
-                       "else",
-                       "elseif",
-                       "endfor",
-                       "endif",
-                       "for",
-                       "if",
-                       "while",
-                       "endwhile",
-                       "switch",
-                       "case",
-                       "endswitch",
-                       "return",
-                       "define",
-                       "include",
-                       "include_once",
-                       "require",
-                       "require_once",
-                       "function",
-                       "new",
-                       "old_function",
-                       "default",
-                       "global",
-                       "static",
-                       "foreach",
-                       "endforeach",
-                       "extends",
-                       "empty",
-                       "isset",
-                       "var" };
-      
-       protected IContextInformationValidator fValidator = new Validator();
-
-       /* (non-Javadoc)
-        * Method declared on IContentAssistProcessor
-        */
-       public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
-               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;
-       }
-
-       /* (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$
-               return result;
-       }
-
-       /* (non-Javadoc)
-        * Method declared on IContentAssistProcessor
-        */
-       public char[] getCompletionProposalAutoActivationCharacters() {
-               return new char[] { '.', '(' };
-       }
-
-       /* (non-Javadoc)
-        * Method declared on IContentAssistProcessor
-        */
-       public char[] getContextInformationAutoActivationCharacters() {
-               return new char[] { '#' };
-       }
-
-       /* (non-Javadoc)
-        * Method declared on IContentAssistProcessor
-        */
-       public IContextInformationValidator getContextInformationValidator() {
-               return fValidator;
-       }
-
-       /* (non-Javadoc)
-        * Method declared on IContentAssistProcessor
-        */
-       public String getErrorMessage() {
-               return null;
-       }
+  /**
+   * Simple content assist tip closer. The tip is valid in a range
+   * of 5 characters around its popup location.
+   */
+  protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
+
+    protected int fInstallOffset;
+
+    /*
+     * @see IContextInformationValidator#isContextInformationValid(int)
+     */
+    public boolean isContextInformationValid(int offset) {
+      return Math.abs(fInstallOffset - offset) < 5;
+    }
+
+    /*
+     * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
+     */
+    public void install(IContextInformation info, ITextViewer viewer, int offset) {
+      fInstallOffset = offset;
+    }
+
+    /*
+     * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
+     */
+    public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
+      return false;
+    }
+  };
+
+  protected final static String[] fgProposals = PHPCodeScanner.fgFunctionNames;
+  //    {
+  //      "array",
+  //      "break",
+  //      "class",
+  //      "continue",
+  //      "do",
+  //      "echo",
+  //      "else",
+  //      "elseif",
+  //      "endfor",
+  //      "endif",
+  //      "for",
+  //      "if",
+  //      "while",
+  //      "endwhile",
+  //      "switch",
+  //      "case",
+  //      "endswitch",
+  //      "return",
+  //      "define",
+  //      "include",
+  //      "include_once",
+  //      "require",
+  //      "require_once",
+  //      "function",
+  //      "new",
+  //      "old_function",
+  //      "default",
+  //      "global",
+  //      "static",
+  //      "foreach",
+  //      "endforeach",
+  //      "extends",
+  //      "empty",
+  //      "isset",
+  //      "var" };
+
+  protected IContextInformationValidator fValidator = new Validator();
+
+  /* (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 {
+    //        char character = document.getChar(documentOffset - 1);
+    //        Point point = PHPWordExtractor.findWord(document, documentOffset);
+    //        if (point != null) {
+    //          String word = document.get(point.x, point.y);
+    //          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;
+    //          }
+    //        }
+    //      } catch (BadLocationException e) {
+    //      }
+    //
+    //    }
+
+    //    if (documentOffset > 0) {
+    //      try {
+    //        char character = document.getChar(documentOffset - 1);
+    //        if (character=='$') {
+    //          
+    //        }
+    //        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$
+    //        }
+    //      } catch (BadLocationException e) {
+    //      }
+    //
+    //    } else {
+    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;
+  }
+
+  /* (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$
+    return result;
+  }
+
+  /* (non-Javadoc)
+   * Method declared on IContentAssistProcessor
+   */
+  public char[] getCompletionProposalAutoActivationCharacters() {
+    return new char[] { '$' };
+  }
+
+  /* (non-Javadoc)
+   * Method declared on IContentAssistProcessor
+   */
+  public char[] getContextInformationAutoActivationCharacters() {
+    return new char[] {
+    };
+  }
+
+  /* (non-Javadoc)
+   * Method declared on IContentAssistProcessor
+   */
+  public IContextInformationValidator getContextInformationValidator() {
+    return fValidator;
+  }
+
+  /* (non-Javadoc)
+   * Method declared on IContentAssistProcessor
+   */
+  public String getErrorMessage() {
+    return null;
+  }
 }