84bfadb2443229108c97aaabea6454a7f8b87d28
[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.sql.Connection;
15 import java.sql.DatabaseMetaData;
16 import java.sql.ResultSet;
17 import java.sql.SQLException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.SortedMap;
22
23 import net.sourceforge.phpdt.core.ToolFactory;
24 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
25 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
26 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
27 import net.sourceforge.phpdt.internal.corext.template.ContextType;
28 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
29 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
30 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
31 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
32 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
33 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
34 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
35 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
36 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
37 import net.sourceforge.phpdt.internal.ui.text.template.SQLProposal;
38 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
39 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
40 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
41 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
42 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
43 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
44 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
45 import net.sourceforge.phpeclipse.ui.overlaypages.Util;
46
47 import org.eclipse.core.resources.IFile;
48 import org.eclipse.core.resources.IProject;
49 import org.eclipse.jface.text.BadLocationException;
50 import org.eclipse.jface.text.IDocument;
51 import org.eclipse.jface.text.IRegion;
52 import org.eclipse.jface.text.ITextViewer;
53 import org.eclipse.jface.text.Region;
54 import org.eclipse.jface.text.TextPresentation;
55 import org.eclipse.jface.text.contentassist.ICompletionProposal;
56 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
57 import org.eclipse.jface.text.contentassist.IContextInformation;
58 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
59 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
60 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
61 import org.eclipse.swt.graphics.Image;
62 import org.eclipse.ui.IEditorPart;
63 import org.eclipse.ui.IFileEditorInput;
64
65 import com.quantum.model.Bookmark;
66 import com.quantum.model.BookmarkCollection;
67 import com.quantum.model.NotConnectedException;
68 import com.quantum.util.connection.ConnectionUtil;
69
70 /**
71  * Example PHP completion processor.
72  */
73 public class PHPCompletionProcessor implements IContentAssistProcessor {
74   /**
75    * Simple content assist tip closer. The tip is valid in a range of 5
76    * characters around its popup location.
77    */
78   protected static class Validator implements IContextInformationValidator,
79       IContextInformationPresenter {
80     protected int fInstallOffset;
81
82     /*
83      * @see IContextInformationValidator#isContextInformationValid(int)
84      */
85     public boolean isContextInformationValid(int offset) {
86       return Math.abs(fInstallOffset - offset) < 5;
87     }
88
89     /*
90      * @see IContextInformationValidator#install(IContextInformation,
91      *      ITextViewer, int)
92      */
93     public void install(IContextInformation info, ITextViewer viewer, int offset) {
94       fInstallOffset = offset;
95     }
96
97     /*
98      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
99      *      TextPresentation)
100      */
101     public boolean updatePresentation(int documentPosition,
102         TextPresentation presentation) {
103       return false;
104     }
105   };
106
107   private static class ContextInformationWrapper implements
108       IContextInformation, IContextInformationExtension {
109     private final IContextInformation fContextInformation;
110
111     private int fPosition;
112
113     public ContextInformationWrapper(IContextInformation contextInformation) {
114       fContextInformation = contextInformation;
115     }
116
117     /*
118      * @see IContextInformation#getContextDisplayString()
119      */
120     public String getContextDisplayString() {
121       return fContextInformation.getContextDisplayString();
122     }
123
124     /*
125      * @see IContextInformation#getImage()
126      */
127     public Image getImage() {
128       return fContextInformation.getImage();
129     }
130
131     /*
132      * @see IContextInformation#getInformationDisplayString()
133      */
134     public String getInformationDisplayString() {
135       return fContextInformation.getInformationDisplayString();
136     }
137
138     /*
139      * @see IContextInformationExtension#getContextInformationPosition()
140      */
141     public int getContextInformationPosition() {
142       return fPosition;
143     }
144
145     public void setContextInformationPosition(int position) {
146       fPosition = position;
147     }
148   };
149
150   private class TableName {
151     String fTableName;
152
153     TableName() {
154       fTableName = null;
155     }
156
157     /**
158      * @return Returns the tableName.
159      */
160     public String getTableName() {
161       if (fTableName == null) {
162         return "<!--no-table-->";
163       }
164       return fTableName;
165     }
166
167     /**
168      * @param tableName
169      *          The tableName to set.
170      */
171     public void setTableName(String tableName) {
172       fTableName = tableName;
173     }
174   }
175
176   private char[] fProposalAutoActivationSet;
177
178   protected IContextInformationValidator fValidator = new Validator();
179
180   private TemplateEngine fTemplateEngine;
181
182   private PHPCompletionProposalComparator fComparator;
183
184   private int fNumberOfComputedResults = 0;
185
186   private IEditorPart fEditor;
187
188   protected IWorkingCopyManager fManager;
189
190   public PHPCompletionProcessor(IEditorPart editor) {
191     fEditor = editor;
192     fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
193     ContextType contextType = ContextTypeRegistry.getInstance().getContextType(
194         "php"); //$NON-NLS-1$
195     if (contextType != null)
196       fTemplateEngine = new TemplateEngine(contextType);
197     fComparator = new PHPCompletionProposalComparator();
198   }
199
200   /**
201    * Tells this processor to order the proposals alphabetically.
202    * 
203    * @param order
204    *          <code>true</code> if proposals should be ordered.
205    */
206   public void orderProposalsAlphabetically(boolean order) {
207     fComparator.setOrderAlphabetically(order);
208   }
209
210   /**
211    * Sets this processor's set of characters triggering the activation of the
212    * completion proposal computation.
213    * 
214    * @param activationSet
215    *          the activation set
216    */
217   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
218     fProposalAutoActivationSet = activationSet;
219   }
220
221   /*
222    * (non-Javadoc) Method declared on IContentAssistProcessor
223    */
224   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
225       int documentOffset) {
226     int contextInformationPosition = guessContextInformationPosition(viewer,
227         documentOffset);
228     return internalComputeCompletionProposals(viewer, documentOffset,
229         contextInformationPosition);
230   }
231
232   private int getLastToken(ITextViewer viewer, int completionPosition,
233       PHPUnitContext context, TableName tableName) {
234     IDocument document = viewer.getDocument();
235     int start = context.getStart();
236     int end = context.getEnd();
237     String startText;
238     int lastSignificantToken = ITerminalSymbols.TokenNameEOF;
239     try {
240       // begin search 2 lines behind of this
241       int j = start;
242       if (j != 0) {
243         char ch;
244         while (j-- > 0) {
245           ch = document.getChar(j);
246           if (ch == '\n') {
247             break;
248           }
249         }
250         while (j-- > 0) {
251           ch = document.getChar(j);
252           if (ch == '\n') {
253             break;
254           }
255         }
256       }
257       if (j != start) {
258         // scan the line for the dereferencing operator '->'
259         startText = document.get(j, start - j);
260         if (Scanner.DEBUG) {
261           System.out.println(startText);
262         }
263         int token = ITerminalSymbols.TokenNameEOF;
264         //        token = getLastSQLToken(startText);
265         tableName.setTableName(getLastSQLTableName(startText));
266         Scanner scanner = ToolFactory.createScanner(false, false, false);
267         scanner.setSource(startText.toCharArray());
268         scanner.setPHPMode(true);
269         int beforeLastToken = ITerminalSymbols.TokenNameEOF;
270         int lastToken = ITerminalSymbols.TokenNameEOF;
271         char[] ident;
272         try {
273           token = scanner.getNextToken();
274           lastToken = token;
275           while (token != ITerminalSymbols.TokenNameERROR
276               && token != ITerminalSymbols.TokenNameEOF) {
277             beforeLastToken = lastToken;
278             if (lastToken==ITerminalSymbols.TokenNameVariable) {
279               ident = scanner.getCurrentTokenSource();
280               if (ident.length==5 &&
281                   ident[0]=='$' &&
282                   ident[1]=='t' &&
283                   ident[2]=='h' &&
284                   ident[3]=='i' &&
285                   ident[4]=='s') {
286                 beforeLastToken = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
287               }
288             }
289             lastToken = token;
290             //                                                          System.out.println(scanner.toStringAction(lastToken));
291             token = scanner.getNextToken();
292           }
293         } catch (InvalidInputException e1) {
294         }
295         switch (lastToken) {
296         case ITerminalSymbols.TokenNameMINUS_GREATER:
297           // dereferencing operator '->' found
298           lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
299           if (beforeLastToken == ITerminalSymbols.TokenNameVariable) {
300             lastSignificantToken = ITerminalSymbols.TokenNameVariable;
301           }
302           break;
303         case ITerminalSymbols.TokenNamenew:
304           lastSignificantToken = ITerminalSymbols.TokenNamenew;
305           break;
306         }
307       }
308     } catch (BadLocationException e) {
309     }
310     return lastSignificantToken;
311   }
312
313   String getSQLTableName(String sqlText, int start) {
314     int tableNameStart = -1;
315     int currentCharacterPosition = start + 1;
316     char ch;
317     try {
318       while (true) {
319         ch = sqlText.charAt(currentCharacterPosition++);
320         if (tableNameStart == -1 && Character.isJavaIdentifierStart(ch)) {
321           tableNameStart = currentCharacterPosition - 1;
322         } else {
323           if (!Character.isJavaIdentifierPart(ch)) {
324             return sqlText.substring(tableNameStart,
325                 currentCharacterPosition - 1);
326           }
327         }
328       }
329     } catch (IndexOutOfBoundsException e) {
330       if (tableNameStart >= 0) {
331         return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
332       }
333     }
334     return "";
335   }
336
337   private String getLastSQLTableName(String startText) {
338     int token;
339     // scan for sql identifiers
340     char ch = ' ';
341     int currentSQLPosition = startText.length();
342     int identEnd = -1;
343     String ident = null;
344     boolean whiteSpace = true;
345     try {
346       while (true) {
347         ch = startText.charAt(--currentSQLPosition);
348         if (ch >= 'A' && ch <= 'Z') {
349           if (identEnd < 0) {
350             identEnd = currentSQLPosition + 1;
351           }
352         } else if (ch >= 'a' && ch <= 'z') {
353           if (identEnd < 0) {
354             identEnd = currentSQLPosition + 1;
355           }
356         } else if (identEnd >= 0) {
357           ident = startText.substring(currentSQLPosition + 1, identEnd);
358           // select -- from -- where --
359           // update -- set -- where --
360           // insert into -- ( -- ) values ( -- )
361           if (ident.length() >= 4 && ident.length() <= 6) {
362             ident = ident.toLowerCase();
363             switch (ident.length()) {
364             //              case 3 :
365             //                if (ident.equals("set")) {
366             //                  // System.out.println("set");
367             //                  token = ITerminalSymbols.TokenNameSQLset;
368             //                  return token;
369             //                }
370             //                break;
371             case 4:
372               if (ident.equals("from")) {
373                 //                  System.out.println("from");
374                 token = ITerminalSymbols.TokenNameSQLfrom;
375                 return getSQLTableName(startText, identEnd);
376               } else if (ident.equals("into")) {
377                 //                System.out.println("into");
378                 token = ITerminalSymbols.TokenNameSQLinto;
379                 return getSQLTableName(startText, identEnd);
380               }
381               break;
382             //              case 5 :
383             //                if (ident.equals("where")) {
384             //                  // System.out.println("where");
385             //                  token = ITerminalSymbols.TokenNameSQLwhere;
386             //                  return token;
387             //                }
388             //                break;
389             case 6:
390               //                if (ident.equals("select")) {
391               //                  // System.out.println("select");
392               //                  token = ITerminalSymbols.TokenNameSQLselect;
393               //                  return token;
394               //                } else if (ident.equals("insert")) {
395               //                  // System.out.println("insert");
396               //                  token = ITerminalSymbols.TokenNameSQLinsert;
397               //                  return token;
398               //                } else
399               if (ident.equals("update")) {
400                 //                  System.out.println("update");
401                 token = ITerminalSymbols.TokenNameSQLupdate;
402                 return getSQLTableName(startText, identEnd);
403               }
404               //                else if (ident.equals("values")) {
405               //                  // System.out.println("values");
406               //                  token = ITerminalSymbols.TokenNameSQLvalues;
407               //                  return token;
408               //                }
409               break;
410             }
411           }
412           whiteSpace = false;
413           identEnd = -1;
414         } else if (Character.isWhitespace(ch)) {
415         } else {
416           whiteSpace = false;
417         }
418       }
419     } catch (IndexOutOfBoundsException e) {
420     }
421     return "<!--no-table-->";
422   }
423
424   /**
425    * Detect the last significant SQL token in the text before the completion
426    * 
427    * @param startText
428    */
429   private int getLastSQLToken(String startText) {
430     int token;
431     // scan for sql identifiers
432     char ch = ' ';
433     int currentSQLPosition = startText.length();
434     int identEnd = -1;
435     String ident = null;
436     boolean whiteSpace = true;
437     try {
438       while (true) {
439         ch = startText.charAt(--currentSQLPosition);
440         if (ch >= 'A' && ch <= 'Z') {
441           if (identEnd < 0) {
442             identEnd = currentSQLPosition + 1;
443           }
444         } else if (ch >= 'a' && ch <= 'z') {
445           if (identEnd < 0) {
446             identEnd = currentSQLPosition + 1;
447           }
448         } else if (identEnd >= 0) {
449           ident = startText.substring(currentSQLPosition + 1, identEnd);
450           // select -- from -- where --
451           // update -- set -- where --
452           // insert into -- ( -- ) values ( -- )
453           if (ident.length() >= 3 && ident.length() <= 6) {
454             ident = ident.toLowerCase();
455             switch (ident.length()) {
456             case 3:
457               if (ident.equals("set")) {
458                 //                  System.out.println("set");
459                 token = ITerminalSymbols.TokenNameSQLset;
460                 return token;
461               }
462               break;
463             case 4:
464               if (ident.equals("from")) {
465                 //                  System.out.println("from");
466                 token = ITerminalSymbols.TokenNameSQLfrom;
467                 //getSQLTableName();
468                 return token;
469               } else if (ident.equals("into")) {
470                 //                System.out.println("into");
471                 token = ITerminalSymbols.TokenNameSQLinto;
472                 return token;
473               }
474               break;
475             case 5:
476               if (ident.equals("where")) {
477                 //                  System.out.println("where");
478                 token = ITerminalSymbols.TokenNameSQLwhere;
479                 return token;
480               }
481               break;
482             case 6:
483               if (ident.equals("select")) {
484                 //                  System.out.println("select");
485                 token = ITerminalSymbols.TokenNameSQLselect;
486                 return token;
487               } else if (ident.equals("insert")) {
488                 //                  System.out.println("insert");
489                 token = ITerminalSymbols.TokenNameSQLinsert;
490                 return token;
491               } else if (ident.equals("update")) {
492                 //                  System.out.println("update");
493                 token = ITerminalSymbols.TokenNameSQLupdate;
494                 return token;
495               } else if (ident.equals("values")) {
496                 //                System.out.println("values");
497                 token = ITerminalSymbols.TokenNameSQLvalues;
498                 return token;
499               }
500               break;
501             }
502           }
503           whiteSpace = false;
504           identEnd = -1;
505         } else if (Character.isWhitespace(ch)) {
506         } else {
507           whiteSpace = false;
508         }
509       }
510     } catch (IndexOutOfBoundsException e) {
511     }
512     return ITerminalSymbols.TokenNameEOF;
513   }
514
515   private ICompletionProposal[] internalComputeCompletionProposals(
516       ITextViewer viewer, int offset, int contextOffset) {
517
518     IDocument document = viewer.getDocument();
519     Object[] identifiers = null;
520     IFile file = null;
521     IProject project = null;
522     if (offset > 0) {
523       PHPEditor editor = null;
524       if (fEditor != null && (fEditor instanceof PHPEditor)) {
525         editor = (PHPEditor) fEditor;
526         file = ((IFileEditorInput) editor.getEditorInput()).getFile();
527         project = file.getProject();
528         //        outlinePage = editor.getfOutlinePage();
529         // TODO: get the identifiers from the new model
530         //        if (outlinePage instanceof PHPContentOutlinePage) {
531         //          identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
532         //        }
533       }
534     }
535     
536     ContextType phpContextType = ContextTypeRegistry.getInstance()
537         .getContextType("php"); //$NON-NLS-1$
538     ((CompilationUnitContextType) phpContextType).setContextParameters(
539         document, offset, 0);
540     PHPUnitContext context = (PHPUnitContext) phpContextType.createContext();
541     String prefix = context.getKey();
542     TableName sqlTable = new TableName();
543     int lastSignificantToken = getLastToken(viewer, offset, context, sqlTable);
544     boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER)
545         || (lastSignificantToken == ITerminalSymbols.TokenNameVariable)
546         || (lastSignificantToken == ITerminalSymbols.TokenNamenew)
547         || (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION);
548     boolean emptyPrefix = prefix == null || prefix.equals("");
549     if (fTemplateEngine != null) {
550       IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
551       ICompletionProposal[] results;
552       if (!emptyPrefix) {
553         fTemplateEngine.reset();
554         fTemplateEngine.complete(viewer, offset); //, unit);
555         templateResults = fTemplateEngine.getResults();
556       }
557       IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
558       if ((!useClassMembers) && identifiers != null) {
559         IdentifierEngine identifierEngine;
560         ContextType contextType = ContextTypeRegistry.getInstance()
561             .getContextType("php"); //$NON-NLS-1$
562         if (contextType != null) {
563           identifierEngine = new IdentifierEngine(contextType);
564           identifierEngine.complete(viewer, offset, identifiers);
565           identifierResults = identifierEngine.getResults();
566         }
567       }
568       // declarations stored in file project.index on project level
569       IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
570       if (project != null) {
571         DeclarationEngine declarationEngine;
572         ContextType contextType = ContextTypeRegistry.getInstance()
573             .getContextType("php"); //$NON-NLS-1$
574         if (contextType != null) {
575           IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
576               .getIndexManager(project);
577           SortedMap sortedMap = indexManager.getIdentifierMap();
578           declarationEngine = new DeclarationEngine(project, contextType,
579               lastSignificantToken, file);
580           declarationEngine.complete(viewer, offset, sortedMap);
581           declarationResults = declarationEngine.getResults();
582         }
583       }
584       // built in function names from phpsyntax.xml
585       ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
586       IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
587       if ((!useClassMembers) && syntaxbuffer != null) {
588         BuiltInEngine builtinEngine;
589         String proposal;
590         ContextType contextType = ContextTypeRegistry.getInstance()
591             .getContextType("php"); //$NON-NLS-1$
592         if (contextType != null) {
593           builtinEngine = new BuiltInEngine(contextType);
594           builtinEngine.complete(viewer, offset, syntaxbuffer);
595           builtinResults = builtinEngine.getResults();
596         }
597       }
598       IPHPCompletionProposal[] sqlResults = new IPHPCompletionProposal[0];
599       if (project != null) {
600         // Get The Database bookmark from the Quantum SQL plugin:
601         BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
602         if (sqlBookMarks != null) {
603           String bookmarkString = Util.getMiscProjectsPreferenceValue(project,
604               IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
605           if (bookmarkString != null && !bookmarkString.equals("")) {
606             Bookmark bookmark = sqlBookMarks.find(bookmarkString);
607             ArrayList sqlList = new ArrayList();
608             if (bookmark != null && !bookmark.isConnected()) {
609               new ConnectionUtil().connect(bookmark, null);
610             }
611             if (bookmark != null && bookmark.isConnected()) {
612               try {
613                 Connection connection = bookmark.getConnection();
614                 DatabaseMetaData metaData = connection.getMetaData();
615
616                 if (metaData != null) {
617                   int start = context.getStart();
618                   int end = context.getEnd();
619                   String foundSQLTableName = sqlTable.getTableName();
620                   String tableName;
621                   String columnName;
622                   String prefixWithoutDollar = prefix;
623                   boolean isDollarPrefix = false;
624                   if (prefix.length() > 0 && prefix.charAt(0) == '$') {
625                     prefixWithoutDollar = prefix.substring(1);
626                     isDollarPrefix = true;
627                   }
628                   IRegion region = new Region(start, end - start);
629                   ResultSet set;
630                   if (!isDollarPrefix) {
631                     set = metaData.getTables(null, null, prefixWithoutDollar
632                         + "%", null);
633                     while (set.next()) {
634                       //                  String tempSchema = set.getString("TABLE_SCHEM");
635                       //                  tempSchema = (tempSchema == null) ? "" :
636                       // tempSchema.trim();
637                       tableName = set.getString("TABLE_NAME");
638                       tableName = (tableName == null) ? "" : tableName.trim();
639                       if (tableName != null && tableName.length() > 0) {
640                         sqlList.add(new SQLProposal(tableName, context, region,
641                             viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
642                       }
643                     }
644                     set.close();
645                   }
646                   set = metaData.getColumns(null, null, "%",
647                       prefixWithoutDollar + "%");
648                   SQLProposal sqlProposal;
649                   while (set.next()) {
650                     columnName = set.getString("COLUMN_NAME");
651                     columnName = (columnName == null) ? "" : columnName.trim();
652                     tableName = set.getString("TABLE_NAME");
653                     tableName = (tableName == null) ? "" : tableName.trim();
654                     if (tableName != null && tableName.length() > 0
655                         && columnName != null && columnName.length() > 0) {
656                       if (isDollarPrefix) {
657                         sqlProposal = new SQLProposal(tableName, "$"
658                             + columnName, context, region, viewer, PHPUiImages
659                             .get(PHPUiImages.IMG_COLUMN));
660                       } else {
661                         sqlProposal = new SQLProposal(tableName, columnName,
662                             context, region, viewer, PHPUiImages
663                                 .get(PHPUiImages.IMG_COLUMN));
664                       }
665                       if (tableName.equals(foundSQLTableName)) {
666                         sqlProposal.setRelevance(90);
667                       } else if (tableName.indexOf(foundSQLTableName) >= 0) {
668                         sqlProposal.setRelevance(75);
669                       }
670                       sqlList.add(sqlProposal);
671                     }
672                   }
673                   set.close();
674                   sqlResults = new IPHPCompletionProposal[sqlList.size()];
675                   for (int i = 0; i < sqlList.size(); i++) {
676                     sqlResults[i] = (SQLProposal) sqlList.get(i);
677                   }
678                 }
679               } catch (NotConnectedException e) {
680                 // ignore this - not mission critical
681               } catch (SQLException e) {
682                 e.printStackTrace();
683               }
684             }
685           }
686         }
687       }
688       // concatenate the result arrays
689       IPHPCompletionProposal[] total;
690       total = new IPHPCompletionProposal[templateResults.length
691           + identifierResults.length + builtinResults.length
692           + declarationResults.length + sqlResults.length];
693       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
694       System.arraycopy(identifierResults, 0, total, templateResults.length,
695           identifierResults.length);
696       System.arraycopy(builtinResults, 0, total, templateResults.length
697           + identifierResults.length, builtinResults.length);
698       System.arraycopy(declarationResults, 0, total, templateResults.length
699           + identifierResults.length + builtinResults.length,
700           declarationResults.length);
701       System.arraycopy(sqlResults, 0, total, templateResults.length
702           + identifierResults.length + builtinResults.length
703           + declarationResults.length, sqlResults.length);
704       results = total;
705       fNumberOfComputedResults = (results == null ? 0 : results.length);
706       /*
707        * Order here and not in result collector to make sure that the order
708        * applies to all proposals and not just those of the compilation unit.
709        */
710       return order(results);
711     }
712     return new IPHPCompletionProposal[0];
713   }
714
715   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
716     int contextPosition = offset;
717     IDocument document = viewer.getDocument();
718     //    try {
719     //
720     //      PHPCodeReader reader= new PHPCodeReader();
721     //      reader.configureBackwardReader(document, offset, true, true);
722     //  
723     //      int nestingLevel= 0;
724     //
725     //      int curr= reader.read();
726     //      while (curr != PHPCodeReader.EOF) {
727     //
728     //        if (')' == (char) curr)
729     //          ++ nestingLevel;
730     //
731     //        else if ('(' == (char) curr) {
732     //          -- nestingLevel;
733     //        
734     //          if (nestingLevel < 0) {
735     //            int start= reader.getOffset();
736     //            if (looksLikeMethod(reader))
737     //              return start + 1;
738     //          }
739     //        }
740     //
741     //        curr= reader.read();
742     //      }
743     //    } catch (IOException e) {
744     //    }
745     return contextPosition;
746   }
747
748   /*
749    * (non-Javadoc) Method declared on IContentAssistProcessor
750    */
751   //  public IContextInformation[] computeContextInformation(ITextViewer viewer,
752   // int documentOffset) {
753   //    IContextInformation[] result = new IContextInformation[5];
754   //    for (int i = 0; i < result.length; i++)
755   //      result[i] = new
756   // ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"),
757   // new Object[] { new Integer(i), new Integer(documentOffset)}),
758   // //$NON-NLS-1$
759   //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"),
760   // new Object[] { new Integer(i), new Integer(documentOffset - 5), new
761   // Integer(documentOffset + 5)})); //$NON-NLS-1$
762   //    return result;
763   //  }
764   /**
765    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
766    */
767   public IContextInformation[] computeContextInformation(ITextViewer viewer,
768       int offset) {
769     int contextInformationPosition = guessContextInformationPosition(viewer,
770         offset);
771     List result = addContextInformations(viewer, contextInformationPosition);
772     return (IContextInformation[]) result
773         .toArray(new IContextInformation[result.size()]);
774   }
775
776   private List addContextInformations(ITextViewer viewer, int offset) {
777     ICompletionProposal[] proposals = internalComputeCompletionProposals(
778         viewer, offset, -1);
779     List result = new ArrayList();
780     for (int i = 0; i < proposals.length; i++) {
781       IContextInformation contextInformation = proposals[i]
782           .getContextInformation();
783       if (contextInformation != null) {
784         ContextInformationWrapper wrapper = new ContextInformationWrapper(
785             contextInformation);
786         wrapper.setContextInformationPosition(offset);
787         result.add(wrapper);
788       }
789     }
790     return result;
791   }
792
793   /**
794    * Order the given proposals.
795    */
796   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
797     Arrays.sort(proposals, fComparator);
798     return proposals;
799   }
800
801   /*
802    * (non-Javadoc) Method declared on IContentAssistProcessor
803    */
804   public char[] getCompletionProposalAutoActivationCharacters() {
805     return fProposalAutoActivationSet;
806     //    return null; // new char[] { '$' };
807   }
808
809   /*
810    * (non-Javadoc) Method declared on IContentAssistProcessor
811    */
812   public char[] getContextInformationAutoActivationCharacters() {
813     return new char[] {};
814   }
815
816   /*
817    * (non-Javadoc) Method declared on IContentAssistProcessor
818    */
819   public IContextInformationValidator getContextInformationValidator() {
820     return fValidator;
821   }
822
823   /*
824    * (non-Javadoc) Method declared on IContentAssistProcessor
825    */
826   public String getErrorMessage() {
827     return null;
828   }
829 }