misc
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCompletionProcessor.java
index cdb26b9..c0bdd55 100644 (file)
@@ -11,7 +11,6 @@
 package net.sourceforge.phpeclipse.phpeditor.php;
 
 import java.io.IOException;
-import java.sql.ResultSet;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -37,19 +36,15 @@ import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
 import net.sourceforge.phpdt.internal.compiler.parser.VariableInfo;
 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
-import net.sourceforge.phpdt.internal.core.CompilationUnit;
-import net.sourceforge.phpdt.internal.core.SourceMethod;
-import net.sourceforge.phpdt.internal.core.SourceType;
 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
-import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader;
 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+import net.sourceforge.phpdt.internal.ui.text.java.JavaParameterListValidator;
 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
 import net.sourceforge.phpdt.internal.ui.text.template.LocalVariableProposal;
-import net.sourceforge.phpdt.internal.ui.text.template.SQLProposal;
 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
@@ -57,8 +52,6 @@ import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
-import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
-import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
@@ -67,22 +60,19 @@ import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.jface.text.Region;
-import org.eclipse.jface.text.TextPresentation;
 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.jface.text.templates.DocumentTemplateContext;
 import org.eclipse.jface.text.templates.TemplateContextType;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IFileEditorInput;
 
-import com.quantum.ExternalInterface;
-import com.quantum.model.NotConnectedException;
+//import com.quantum.ExternalInterface;
+// import com.quantum.util.connection.NotConnectedException;
 
 /**
  * Example PHP completion processor.
@@ -91,30 +81,30 @@ 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 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;
+//    }
+//  };
 
   private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
     private final IContextInformation fContextInformation;
@@ -186,7 +176,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
 
   private char[] fProposalAutoActivationSet;
 
-  protected IContextInformationValidator fValidator = new Validator();
+  protected IContextInformationValidator fValidator = null;// = new Validator();
 
   private TemplateEngine fTemplateEngine;
 
@@ -348,14 +338,15 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
     try {
       while (true) {
         ch = startText.charAt(--currentSQLPosition);
-        if (ch >= 'A' && ch <= 'Z') {
-          if (identEnd < 0) {
-            identEnd = currentSQLPosition + 1;
-          }
-        } else if (ch >= 'a' && ch <= 'z') {
+        if (Scanner.isSQLIdentifierPart(ch)) {
+//        if (ch >= 'A' && ch <= 'Z') {
           if (identEnd < 0) {
             identEnd = currentSQLPosition + 1;
           }
+//        } else if (ch >= 'a' && ch <= 'z') {
+//          if (identEnd < 0) {
+//            identEnd = currentSQLPosition + 1;
+//          }
         } else if (identEnd >= 0) {
           ident = startText.substring(currentSQLPosition + 1, identEnd);
           // select -- from -- where --
@@ -653,23 +644,25 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
           builtinResults = builtinEngine.getResults();
         }
       }
-      ICompletionProposal[] sqlResults = new ICompletionProposal[0];
-      if (project != null) {
-        sqlResults = getSQLProposals(viewer, project, context, prefix, sqlTable);
-      }
+//      ICompletionProposal[] sqlResults = new ICompletionProposal[0];
+//      if (project != null) {
+//        sqlResults = getSQLProposals(viewer, project, context, prefix, sqlTable);
+//      }
       // concatenate the result arrays
       IPHPCompletionProposal[] total;
       total = new IPHPCompletionProposal[localVariableResults.length + templateResults.length + identifierResults.length
-          + builtinResults.length + declarationResults.length + sqlResults.length];
+          + builtinResults.length + declarationResults.length];// + sqlResults.length];
       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
       System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
       System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
       System.arraycopy(declarationResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length,
           declarationResults.length);
-      System.arraycopy(sqlResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
-          + declarationResults.length, sqlResults.length);
+//      System.arraycopy(sqlResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
+//          + declarationResults.length, sqlResults.length);
+//      System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
+//          + declarationResults.length + sqlResults.length, localVariableResults.length);
       System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
-          + declarationResults.length + sqlResults.length, localVariableResults.length);
+          + declarationResults.length, localVariableResults.length);
       results = total;
       fNumberOfComputedResults = (results == null ? 0 : results.length);
       /*
@@ -817,111 +810,71 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
    * @param sqlResults
    * @return
    */
-  private ICompletionProposal[] getSQLProposals(ITextViewer viewer, IProject project, DocumentTemplateContext context,
-      String prefix, TableName sqlTable) {
-    ICompletionProposal[] sqlResults = new ICompletionProposal[0];
-    // Get The Database bookmark from the Quantum SQL plugin:
-    //    BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
-    //    if (sqlBookMarks != null) {
-    String bookmarkString = ProjectPrefUtil.getMiscProjectsPreferenceValue(project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
-    if (bookmarkString != null && !bookmarkString.equals("")) {
-      String[] bookmarks = ExternalInterface.getBookmarkNames();
-      boolean foundBookmark = false;
-      for (int i = 0; i < bookmarks.length; i++) {
-        if (bookmarks[i].equals(bookmarkString)) {
-          foundBookmark = true;
-        }
-      }
-      if (!foundBookmark) {
-        return sqlResults;
-      }
-      //        Bookmark bookmark = sqlBookMarks.find(bookmarkString);
-      ArrayList sqlList = new ArrayList();
-      if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
-        ExternalInterface.connectBookmark(bookmarkString, null);
-        if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
-          return sqlResults;
-        }
-      }
-      //      if (ExternalInterface.isBookmarkConnected(bookmarkString)) {
-      try {
-        //            Connection connection = bookmark.getConnection();
-        //            DatabaseMetaData metaData = connection.getMetaData();
-
-        //            if (metaData != null) {
-        int start = context.getStart();
-        int end = context.getEnd();
-        String foundSQLTableName = sqlTable.getTableName();
-        String tableName;
-        String columnName;
-        String prefixWithoutDollar = prefix;
-        boolean isDollarPrefix = false;
-        if (prefix.length() > 0 && prefix.charAt(0) == '$') {
-          prefixWithoutDollar = prefix.substring(1);
-          isDollarPrefix = true;
-        }
-        IRegion region = new Region(start, end - start);
-        ResultSet set;
-        if (!isDollarPrefix) {
-          String[] tableNames = ExternalInterface.getMatchingTableNames(null, bookmarkString, prefixWithoutDollar, null, false);
-          for (int i = 0; i < tableNames.length; i++) {
-            sqlList.add(new SQLProposal(tableNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
-          }
-
-          //                set = metaData.getTables(null, null, prefixWithoutDollar + "%", null);
-          //                while (set.next()) {
-          //                  tableName = set.getString("TABLE_NAME");
-          //                  tableName = (tableName == null) ? "" : tableName.trim();
-          //                  if (tableName != null && tableName.length() > 0) {
-          //                    sqlList.add(new SQLProposal(tableName, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
-          //                  }
-          //                }
-          //                set.close();
-        }
-
-        String[] columnNames = ExternalInterface.getMatchingColumnNames(null, bookmarkString, prefixWithoutDollar, null, false);
-        for (int i = 0; i < columnNames.length; i++) {
-          sqlList.add(new SQLProposal(columnNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
-        }
-        //              set = metaData.getColumns(null, null, "%", prefixWithoutDollar + "%");
-        //              SQLProposal sqlProposal;
-        //              while (set.next()) {
-        //                columnName = set.getString("COLUMN_NAME");
-        //                columnName = (columnName == null) ? "" : columnName.trim();
-        //                tableName = set.getString("TABLE_NAME");
-        //                tableName = (tableName == null) ? "" : tableName.trim();
-        //                if (tableName != null && tableName.length() > 0 && columnName != null && columnName.length() > 0) {
-        //                  if (isDollarPrefix) {
-        //                    sqlProposal = new SQLProposal(tableName, "$" + columnName, context, region, viewer, PHPUiImages
-        //                        .get(PHPUiImages.IMG_COLUMN));
-        //                  } else {
-        //                    sqlProposal = new SQLProposal(tableName, columnName, context, region, viewer, PHPUiImages
-        //                        .get(PHPUiImages.IMG_COLUMN));
-        //                  }
-        //                  if (tableName.equals(foundSQLTableName)) {
-        //                    sqlProposal.setRelevance(90);
-        //                  } else if (tableName.indexOf(foundSQLTableName) >= 0) {
-        //                    sqlProposal.setRelevance(75);
-        //                  }
-        //                  sqlList.add(sqlProposal);
-        //                }
-        //              }
-        //              set.close();
-        sqlResults = new IPHPCompletionProposal[sqlList.size()];
-        for (int i = 0; i < sqlList.size(); i++) {
-          sqlResults[i] = (SQLProposal) sqlList.get(i);
-        }
-        //            }
-      } catch (NotConnectedException e) {
-        // ignore this - not mission critical
-        //        } catch (SQLException e) {
-        //          e.printStackTrace();
-      }
-      //      }
-    }
-    //    }
-    return sqlResults;
-  }
+//  private ICompletionProposal[] getSQLProposals(ITextViewer viewer, IProject project, DocumentTemplateContext context,
+//      String prefix, TableName sqlTable) {
+//    ICompletionProposal[] sqlResults = new ICompletionProposal[0];
+//    // Get The Database bookmark from the Quantum SQL plugin:
+//    //    BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
+//    //    if (sqlBookMarks != null) {
+//    String bookmarkString = ProjectPrefUtil.getMiscProjectsPreferenceValue(project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
+//    if (bookmarkString != null && !bookmarkString.equals("")) {
+//      String[] bookmarks = ExternalInterface.getBookmarkNames();
+//      boolean foundBookmark = false;
+//      for (int i = 0; i < bookmarks.length; i++) {
+//        if (bookmarks[i].equals(bookmarkString)) {
+//          foundBookmark = true;
+//        }
+//      }
+//      if (!foundBookmark) {
+//        return sqlResults;
+//      }
+//      //        Bookmark bookmark = sqlBookMarks.find(bookmarkString);
+//      ArrayList sqlList = new ArrayList();
+//      if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
+//        ExternalInterface.connectBookmark(bookmarkString, null);
+//        if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
+//          return sqlResults;
+//        }
+//      }
+//      //      if (ExternalInterface.isBookmarkConnected(bookmarkString)) {
+//      try {
+//         int start = context.getStart();
+//        int end = context.getEnd();
+//        String foundSQLTableName = sqlTable.getTableName();
+//        String tableName;
+//        String columnName;
+//        String prefixWithoutDollar = prefix;
+//        boolean isDollarPrefix = false;
+//        if (prefix.length() > 0 && prefix.charAt(0) == '$') {
+//          prefixWithoutDollar = prefix.substring(1);
+//          isDollarPrefix = true;
+//        }
+//        IRegion region = new Region(start, end - start);
+//        ResultSet set;
+//        if (!isDollarPrefix) {
+//          String[] tableNames = ExternalInterface.getMatchingTableNames(null, bookmarkString, prefixWithoutDollar, null, false);
+//          for (int i = 0; i < tableNames.length; i++) {
+//            sqlList.add(new SQLProposal(tableNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
+//          }
+//        }
+//
+//        String[] columnNames = ExternalInterface.getMatchingColumnNames(null, bookmarkString, prefixWithoutDollar, null, false);
+//        for (int i = 0; i < columnNames.length; i++) {
+//          sqlList.add(new SQLProposal(columnNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
+//        }
+//        
+//        sqlResults = new IPHPCompletionProposal[sqlList.size()];
+//        for (int i = 0; i < sqlList.size(); i++) {
+//          sqlResults[i] = (SQLProposal) sqlList.get(i);
+//        }
+//      } catch (Exception /* NotConnectedException */ e) {
+//        
+//      }
+//      //      }
+//    }
+//    //    }
+//    return sqlResults;
+//  }
 
   private boolean looksLikeMethod(PHPCodeReader reader) throws IOException {
     int curr = reader.read();
@@ -931,7 +884,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
     if (curr == PHPCodeReader.EOF)
       return false;
 
-    return Scanner.isPHPIdentifierPart((char) curr) || Scanner.isPHPIdentifierStart((char) curr);
+    return Scanner.isPHPIdentifierPart((char) curr);
   }
 
   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
@@ -1017,13 +970,15 @@ public class PHPCompletionProcessor implements IContentAssistProcessor {
    * (non-Javadoc) Method declared on IContentAssistProcessor
    */
   public char[] getContextInformationAutoActivationCharacters() {
-    return new char[] {};
+       return null;
   }
 
   /*
    * (non-Javadoc) Method declared on IContentAssistProcessor
    */
   public IContextInformationValidator getContextInformationValidator() {
+               if (fValidator == null)
+                       fValidator= new JavaParameterListValidator();
     return fValidator;
   }