Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[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  **********************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor.php;
12
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.SortedMap;
22
23 import net.sourceforge.phpdt.core.ICompilationUnit;
24 import net.sourceforge.phpdt.core.IJavaElement;
25 import net.sourceforge.phpdt.core.IMethod;
26 import net.sourceforge.phpdt.core.IType;
27 import net.sourceforge.phpdt.core.JavaCore;
28 import net.sourceforge.phpdt.core.ToolFactory;
29 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
30 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
31 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
32 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
33 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
34 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
35 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
36 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
37 import net.sourceforge.phpdt.internal.compiler.parser.VariableInfo;
38 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
39 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
40 import net.sourceforge.phpdt.internal.core.CompilationUnit;
41 import net.sourceforge.phpdt.internal.core.SourceType;
42 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
43 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
44 import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader;
45 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
46 import net.sourceforge.phpdt.internal.ui.text.java.JavaParameterListValidator;
47 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
48 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
49 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
50 import net.sourceforge.phpdt.internal.ui.text.template.LocalVariableProposal;
51 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
52 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
53 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
54 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
55 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
56 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
57
58 import org.eclipse.core.resources.IFile;
59 import org.eclipse.core.resources.IProject;
60 import org.eclipse.jface.text.BadLocationException;
61 import org.eclipse.jface.text.IDocument;
62 import org.eclipse.jface.text.IRegion;
63 import org.eclipse.jface.text.ITextViewer;
64 import org.eclipse.jface.text.Region;
65 import org.eclipse.jface.text.contentassist.ICompletionProposal;
66 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
67 import org.eclipse.jface.text.contentassist.IContextInformation;
68 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
69 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
70 import org.eclipse.jface.text.templates.TemplateContextType;
71 import org.eclipse.swt.graphics.Image;
72 import org.eclipse.swt.graphics.Point;
73 import org.eclipse.ui.IEditorPart;
74 import org.eclipse.ui.IFileEditorInput;
75
76 /**
77  * Example PHP completion processor.
78  */
79 public class PHPCompletionProcessor implements IContentAssistProcessor {
80         /**
81          * Simple content assist tip closer. The tip is valid in a range of 5
82          * characters around its popup location.
83          */
84         // protected static class Validator implements IContextInformationValidator,
85         // IContextInformationPresenter {
86         // protected int fInstallOffset;
87         //
88         // /*
89         // * @see IContextInformationValidator#isContextInformationValid(int)
90         // */
91         // public boolean isContextInformationValid(int offset) {
92         // return Math.abs(fInstallOffset - offset) < 5;
93         // }
94         //
95         // /*
96         // * @see IContextInformationValidator#install(IContextInformation,
97         // ITextViewer, int)
98         // */
99         // public void install(IContextInformation info, ITextViewer viewer, int
100         // offset) {
101         // fInstallOffset = offset;
102         // }
103         //
104         // /*
105         // * @see
106         // org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
107         // TextPresentation)
108         // */
109         // public boolean updatePresentation(int documentPosition, TextPresentation
110         // presentation) {
111         // return false;
112         // }
113         // };
114         private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
115                 private final IContextInformation fContextInformation;
116
117                 private int fPosition;
118
119                 public ContextInformationWrapper(IContextInformation contextInformation) {
120                         fContextInformation = contextInformation;
121                 }
122
123                 /*
124                  * @see IContextInformation#getContextDisplayString()
125                  */
126                 public String getContextDisplayString() {
127                         return fContextInformation.getContextDisplayString();
128                 }
129
130                 /*
131                  * @see IContextInformation#getImage()
132                  */
133                 public Image getImage() {
134                         return fContextInformation.getImage();
135                 }
136
137                 /*
138                  * @see IContextInformation#getInformationDisplayString()
139                  */
140                 public String getInformationDisplayString() {
141                         return fContextInformation.getInformationDisplayString();
142                 }
143
144                 /*
145                  * @see IContextInformationExtension#getContextInformationPosition()
146                  */
147                 public int getContextInformationPosition() {
148                         return fPosition;
149                 }
150
151                 public void setContextInformationPosition(int position) {
152                         fPosition = position;
153                 }
154         };
155
156 //      private class TableName {
157 //              String fTableName;
158 //
159 //              TableName() {
160 //                      fTableName = null;
161 //              }
162 //
163 //              /**
164 //               * @return Returns the tableName.
165 //               */
166 //              public String getTableName() {
167 //                      if (fTableName == null) {
168 //                              return "<!--no-table-->";
169 //                      }
170 //                      return fTableName;
171 //              }
172 //
173 //              /**
174 //               * @param tableName
175 //               *          The tableName to set.
176 //               */
177 //              public void setTableName(String tableName) {
178 //                      fTableName = tableName;
179 //              }
180 //      }
181
182         private char[] fProposalAutoActivationSet;
183
184         protected IContextInformationValidator fValidator = null;
185
186         private TemplateEngine fTemplateEngine;
187
188         private PHPCompletionProposalComparator fComparator;
189
190         private IEditorPart fEditor;
191
192         protected IWorkingCopyManager fManager;
193
194         public PHPCompletionProcessor(IEditorPart editor) {
195                 fEditor = editor;
196                 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
197                 TemplateContextType contextType = PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType("php"); //$NON-NLS-1$
198                 if (contextType != null)
199                         fTemplateEngine = new TemplateEngine(contextType);
200                 fComparator = new PHPCompletionProposalComparator();
201         }
202
203         /**
204          * Tells this processor to order the proposals alphabetically.
205          *
206          * @param order
207          *          <code>true</code> if proposals should be ordered.
208          */
209         public void orderProposalsAlphabetically(boolean order) {
210                 fComparator.setOrderAlphabetically(order);
211         }
212
213         /**
214          * Sets this processor's set of characters triggering the activation of the
215          * completion proposal computation.
216          *
217          * @param activationSet
218          *          the activation set
219          */
220         public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
221                 fProposalAutoActivationSet = activationSet;
222         }
223
224         /*
225          * (non-Javadoc) Method declared on IContentAssistProcessor
226          */
227         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
228                 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
229                 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
230         }
231
232         private int getLastToken(List list, ITextViewer viewer, int completionPosition, JavaContext context) {
233 //                      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 = null;
272                                 try {
273                                         token = scanner.getNextToken();
274                                         lastToken = token;
275                                         while (token != ITerminalSymbols.TokenNameERROR && token != ITerminalSymbols.TokenNameEOF) {
276                                                 beforeLastToken = lastToken;
277                                                 if (token == ITerminalSymbols.TokenNameVariable) {
278                                                         ident = scanner.getCurrentTokenSource();
279                                                         if (ident.length == 5 && ident[0] == '$' && ident[1] == 't' && ident[2] == 'h' && ident[3] == 'i' && ident[4] == 's') {
280                                                                 token = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
281                                                         }
282                                                 }
283                                                 lastToken = token;
284                                                 // System.out.println(scanner.toStringAction(lastToken));
285                                                 token = scanner.getNextToken();
286                                         }
287                                 } catch (InvalidInputException e1) {
288                                 } catch (SyntaxError e) {
289                                 }
290                                 switch (lastToken) {
291                                 case ITerminalSymbols.TokenNameMINUS_GREATER:
292                                         // dereferencing operator '->' found
293                                         lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
294                                         if (beforeLastToken == ITerminalSymbols.TokenNameVariable) {
295                                                 lastSignificantToken = ITerminalSymbols.TokenNameVariable;
296                                                 list.set(0, ident);
297                                         } else if (beforeLastToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION) {
298                                                 lastSignificantToken = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
299                                                 list.set(0, ident);
300                                         }
301                                         break;
302                                 case ITerminalSymbols.TokenNamenew:
303                                         lastSignificantToken = ITerminalSymbols.TokenNamenew;
304                                         break;
305                                 }
306                         }
307                 } catch (BadLocationException e) {
308                 }
309                 return lastSignificantToken;
310         }
311
312         String getSQLTableName(String sqlText, int start) {
313                 int tableNameStart = -1;
314                 int currentCharacterPosition = start + 1;
315                 char ch;
316                 try {
317                         while (true) {
318                                 ch = sqlText.charAt(currentCharacterPosition++);
319                                 if (tableNameStart == -1 && Scanner.isPHPIdentifierStart(ch)) {
320                                         tableNameStart = currentCharacterPosition - 1;
321                                 } else {
322                                         if (!Scanner.isPHPIdentifierPart(ch)) {
323                                                 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
324                                         }
325                                 }
326                         }
327                 } catch (IndexOutOfBoundsException e) {
328                         if (tableNameStart >= 0) {
329                                 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
330                         }
331                 }
332                 return "";
333         }
334
335 //      private String getLastSQLTableName(String startText) {
336 //              // scan for sql identifiers
337 //              char ch = ' ';
338 //              int currentSQLPosition = startText.length();
339 //              int identEnd = -1;
340 //              String ident = null;
341 //              try {
342 //                      while (true) {
343 //                              ch = startText.charAt(--currentSQLPosition);
344 //                              if (Scanner.isSQLIdentifierPart(ch)) {
345 //                                      // if (ch >= 'A' && ch <= 'Z') {
346 //                                      if (identEnd < 0) {
347 //                                              identEnd = currentSQLPosition + 1;
348 //                                      }
349 //                                      // } else if (ch >= 'a' && ch <= 'z') {
350 //                                      // if (identEnd < 0) {
351 //                                      // identEnd = currentSQLPosition + 1;
352 //                                      // }
353 //                              } else if (identEnd >= 0) {
354 //                                      ident = startText.substring(currentSQLPosition + 1, identEnd);
355 //                                      // select -- from -- where --
356 //                                      // update -- set -- where --
357 //                                      // insert into -- ( -- ) values ( -- )
358 //                                      if (ident.length() >= 4 && ident.length() <= 6) {
359 //                                              ident = ident.toLowerCase();
360 //                                              switch (ident.length()) {
361 //                                              // case 3 :
362 //                                              // if (ident.equals("set")) {
363 //                                              // // System.out.println("set");
364 //                                              // token = ITerminalSymbols.TokenNameSQLset;
365 //                                              // return token;
366 //                                              // }
367 //                                              // break;
368 //                                              case 4:
369 //                                                      if (ident.equals("from")) {
370 //                                                              // System.out.println("from");
371 //                                                              return getSQLTableName(startText, identEnd);
372 //                                                      } else if (ident.equals("into")) {
373 //                                                              // System.out.println("into");
374 //                                                              return getSQLTableName(startText, identEnd);
375 //                                                      }
376 //                                                      break;
377 //                                              case 6:
378 //                                                      if (ident.equals("update")) {
379 //                                                              // System.out.println("update");
380 //                                                              return getSQLTableName(startText, identEnd);
381 //                                                      }
382 //                                                      break;
383 //                                              }
384 //                                      }
385 //                                      identEnd = -1;
386 //                              } else if (Character.isWhitespace(ch)) {
387 //                              }
388 //                      }
389 //              } catch (IndexOutOfBoundsException e) {
390 //              }
391 //              return "<!--no-table-->";
392 //      }
393
394         /**
395          * Detect the last significant SQL token in the text before the completion
396          *
397          * @param startText
398          */
399 //      private int getLastSQLToken(String startText) {
400 //              int token;
401 //              // scan for sql identifiers
402 //              char ch = ' ';
403 //              int currentSQLPosition = startText.length();
404 //              int identEnd = -1;
405 //              String ident = null;
406 //              try {
407 //                      while (true) {
408 //                              ch = startText.charAt(--currentSQLPosition);
409 //                              if (ch >= 'A' && ch <= 'Z') {
410 //                                      if (identEnd < 0) {
411 //                                              identEnd = currentSQLPosition + 1;
412 //                                      }
413 //                              } else if (ch >= 'a' && ch <= 'z') {
414 //                                      if (identEnd < 0) {
415 //                                              identEnd = currentSQLPosition + 1;
416 //                                      }
417 //                              } else if (identEnd >= 0) {
418 //                                      ident = startText.substring(currentSQLPosition + 1, identEnd);
419 //                                      // select -- from -- where --
420 //                                      // update -- set -- where --
421 //                                      // insert into -- ( -- ) values ( -- )
422 //                                      if (ident.length() >= 3 && ident.length() <= 6) {
423 //                                              ident = ident.toLowerCase();
424 //                                              switch (ident.length()) {
425 //                                              case 3:
426 //                                                      if (ident.equals("set")) {
427 //                                                              // System.out.println("set");
428 //                                                              token = ITerminalSymbols.TokenNameSQLset;
429 //                                                              return token;
430 //                                                      }
431 //                                                      break;
432 //                                              case 4:
433 //                                                      if (ident.equals("from")) {
434 //                                                              // System.out.println("from");
435 //                                                              token = ITerminalSymbols.TokenNameSQLfrom;
436 //                                                              // getSQLTableName();
437 //                                                              return token;
438 //                                                      } else if (ident.equals("into")) {
439 //                                                              // System.out.println("into");
440 //                                                              token = ITerminalSymbols.TokenNameSQLinto;
441 //                                                              return token;
442 //                                                      }
443 //                                                      break;
444 //                                              case 5:
445 //                                                      if (ident.equals("where")) {
446 //                                                              // System.out.println("where");
447 //                                                              token = ITerminalSymbols.TokenNameSQLwhere;
448 //                                                              return token;
449 //                                                      }
450 //                                                      break;
451 //                                              case 6:
452 //                                                      if (ident.equals("select")) {
453 //                                                              // System.out.println("select");
454 //                                                              token = ITerminalSymbols.TokenNameSQLselect;
455 //                                                              return token;
456 //                                                      } else if (ident.equals("insert")) {
457 //                                                              // System.out.println("insert");
458 //                                                              token = ITerminalSymbols.TokenNameSQLinsert;
459 //                                                              return token;
460 //                                                      } else if (ident.equals("update")) {
461 //                                                              // System.out.println("update");
462 //                                                              token = ITerminalSymbols.TokenNameSQLupdate;
463 //                                                              return token;
464 //                                                      } else if (ident.equals("values")) {
465 //                                                              // System.out.println("values");
466 //                                                              token = ITerminalSymbols.TokenNameSQLvalues;
467 //                                                              return token;
468 //                                                      }
469 //                                                      break;
470 //                                              }
471 //                                      }
472 //                                      identEnd = -1;
473 //                              }
474 //                      }
475 //              } catch (IndexOutOfBoundsException e) {
476 //              }
477 //              return ITerminalSymbols.TokenNameEOF;
478 //      }
479
480         private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
481                 ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
482                 IDocument document = viewer.getDocument();
483                 IFile file = null;
484                 IProject project = null;
485                 if (offset > 0) {
486                         PHPEditor editor = null;
487                         if (fEditor != null && (fEditor instanceof PHPEditor)) {
488                                 editor = (PHPEditor) fEditor;
489                                 file = ((IFileEditorInput) editor.getEditorInput()).getFile();
490                                 project = file.getProject();
491                         }
492                 }
493
494                 Point selection = viewer.getSelectedRange();
495                 // remember selected text
496                 String selectedText = null;
497                 if (selection.y != 0) {
498                         try {
499                                 selectedText = document.get(selection.x, selection.y);
500                         } catch (BadLocationException e) {
501                         }
502                 }
503
504                 if (offset > 2 && fProposalAutoActivationSet != null) {
505                         // restrict auto activation for '>' character to '->' token
506
507                         try {
508                                 char ch = document.getChar(offset-1);
509                                 if (ch == '>') {
510                                         for (int i = 0; i < fProposalAutoActivationSet.length; i++) {
511                                                 ch = fProposalAutoActivationSet[i];
512                                                 if (ch == '>') { // auto activation enabled
513                                                         ch = document.getChar(offset - 2);
514                                                         if (ch != '-') {
515                                                                 return new IPHPCompletionProposal[0];
516                                                         }
517                                                         break;
518                                                 }
519                                         }
520                                 }
521                         } catch (BadLocationException e) {
522                                 e.printStackTrace();
523                         }
524                 }
525
526                 JavaContextType phpContextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
527                                 "php"); //$NON-NLS-1$
528                 JavaContext context = (JavaContext) phpContextType.createContext(document, offset, selection.y, unit);
529                 context.setVariable("selection", selectedText); //$NON-NLS-1$
530                 String prefix = context.getKey();
531
532                 HashMap methodVariables = null;
533                 // HashMap typeVariables = null;
534                 HashMap unitVariables = null;
535                 ICompilationUnit compilationUnit = (ICompilationUnit) context.findEnclosingElement(IJavaElement.COMPILATION_UNIT);
536 //               if (compilationUnit != null) {
537 //               unitVariables = ((CompilationUnit) compilationUnit).variables;
538 //               }
539                  IType type = (IType) context.findEnclosingElement(IJavaElement.TYPE);
540                  if (type != null) {
541 //               typeVariables = ((SourceType) type).variables;
542                  }
543                 IMethod method = (IMethod) context.findEnclosingElement(IJavaElement.METHOD);
544                 // if (method != null) {
545                 // methodVariables = ((SourceMethod) method).variables;
546                 // }
547
548                 boolean emptyPrefix = prefix == null || prefix.equals("");
549                 IPHPCompletionProposal[] localVariableResults = new IPHPCompletionProposal[0];
550
551                 if (!emptyPrefix && prefix.length() >= 1 && prefix.charAt(0) == '$') {
552                         // php Variable ?
553                         String lowerCasePrefix = prefix.toLowerCase();
554                         HashSet localVariables = new HashSet();
555                         if (compilationUnit != null) {
556                                 unitVariables = getUnitVariables(unitVariables, compilationUnit);
557                                 getVariableProposals(localVariables, viewer, project, context, unitVariables, lowerCasePrefix, 94);
558                         }
559                         if (method != null) {
560                                 methodVariables = getMethodVariables(methodVariables, method);
561                                 getVariableProposals(localVariables, viewer, project, context, methodVariables, lowerCasePrefix, 99);
562                         }
563                         if (!localVariables.isEmpty()) {
564                                 localVariableResults = (IPHPCompletionProposal[]) localVariables.toArray(new IPHPCompletionProposal[localVariables.size()]);
565                         }
566                 }
567
568 //              TableName sqlTable = new TableName();
569                 ArrayList list = new ArrayList();
570                 list.add(null);
571                 int lastSignificantToken = getLastToken(list, viewer, offset, context); //, sqlTable);
572                 boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER)
573                                 || (lastSignificantToken == ITerminalSymbols.TokenNameVariable) || (lastSignificantToken == ITerminalSymbols.TokenNamenew)
574                                 || (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION);
575
576                 if (fTemplateEngine != null) {
577                         IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
578                         ICompletionProposal[] results;
579                         if (!emptyPrefix) {
580                                 fTemplateEngine.reset();
581                                 fTemplateEngine.complete(viewer, offset, unit);
582                                 templateResults = fTemplateEngine.getResults();
583                         }
584                         // TODO delete this
585                         IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
586
587                         // declarations stored in file project.index on project level
588                         IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
589                         if (project != null) {
590                                 DeclarationEngine declarationEngine;
591                                 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
592                                                 "php"); //$NON-NLS-1$
593                                 if (contextType != null) {
594                                         IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
595                                         SortedMap sortedMap;
596                                         declarationEngine = new DeclarationEngine(project, contextType, lastSignificantToken, file);
597                                         if (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION) {
598                                                 // complete '$this->'
599                                                 sortedMap = indexManager.getIdentifiers(file);
600                                                 declarationEngine.completeObject(viewer, offset, sortedMap, unit);
601                                         } else {
602                                                 String typeRef = null;
603                                                 char[] varName = (char[]) list.get(0);
604                                                 if (varName != null) {
605                                                         if (method != null) {
606                                                                 methodVariables = getMethodVariables(methodVariables, method);
607                                                                 VariableInfo info = (VariableInfo) methodVariables.get(new String(varName));
608                                                                 if (info != null && info.typeIdentifier != null) {
609                                                                         typeRef = new String(info.typeIdentifier);
610                                                                 }
611                                                         }
612                                                 }
613                                                 if (typeRef != null) {
614                                                         // complete '$variable->' with type information
615                                                         sortedMap = indexManager.getIdentifiers(typeRef);
616                                                         declarationEngine.completeObject(viewer, offset, sortedMap, unit);
617                                                 } else {
618                                                         // complete '$variable->' without type information
619                                                         sortedMap = indexManager.getIdentifierMap();
620                                                         declarationEngine.complete(viewer, offset, sortedMap, unit);
621                                                 }
622                                         }
623                                         declarationResults = declarationEngine.getResults();
624                                 }
625                         }
626                         // built in function names from phpsyntax.xml
627                         ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
628                         IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
629                         if ((!useClassMembers) && syntaxbuffer != null) {
630                                 BuiltInEngine builtinEngine;
631                                 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
632                                                 "php"); //$NON-NLS-1$
633                                 if (contextType != null) {
634                                         builtinEngine = new BuiltInEngine(contextType);
635                                         builtinEngine.complete(viewer, offset, syntaxbuffer, unit);
636                                         builtinResults = builtinEngine.getResults();
637                                 }
638                         }
639                         // ICompletionProposal[] sqlResults = new ICompletionProposal[0];
640                         // if (project != null) {
641                         // sqlResults = getSQLProposals(viewer, project, context, prefix,
642                         // sqlTable);
643                         // }
644                         // concatenate the result arrays
645                         IPHPCompletionProposal[] total;
646                         total = new IPHPCompletionProposal[localVariableResults.length + templateResults.length + identifierResults.length
647                                         + builtinResults.length + declarationResults.length];// +
648                         // sqlResults.length];
649                         System.arraycopy(templateResults, 0, total, 0, templateResults.length);
650                         System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
651                         System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
652                         System.arraycopy(declarationResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length,
653                                         declarationResults.length);
654                         // System.arraycopy(sqlResults, 0, total, templateResults.length +
655                         // identifierResults.length + builtinResults.length
656                         // + declarationResults.length, sqlResults.length);
657                         // System.arraycopy(localVariableResults, 0, total, templateResults.length
658                         // + identifierResults.length + builtinResults.length
659                         // + declarationResults.length + sqlResults.length,
660                         // localVariableResults.length);
661                         System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
662                                         + declarationResults.length, localVariableResults.length);
663                         results = total;
664 //                      fNumberOfComputedResults = (results == null ? 0 : results.length);
665                         /*
666                          * Order here and not in result collector to make sure that the order
667                          * applies to all proposals and not just those of the compilation unit.
668                          */
669                         return order(results);
670                 }
671                 return new IPHPCompletionProposal[0];
672         }
673
674         /**
675          * @param unitVariables
676          * @param unit
677          */
678         private HashMap getUnitVariables(HashMap unitVariables, ICompilationUnit unit) {
679                 if (unitVariables == null) {
680                         try {
681                                 String unitText = unit.getSource();
682                                 unitVariables = new HashMap();
683
684                                 ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(),
685                                                 new CompilerOptions(JavaCore.getOptions()), new DefaultProblemFactory());
686                                 UnitParser parser = new UnitParser(problemReporter);
687                                 parser.compilationUnit = new CompilationUnitDeclaration(problemReporter, null, unitText.length());
688                                 parser.parse(unitText, unitVariables);
689
690                         } catch (Exception e) {
691                                 // TODO Auto-generated catch block
692                                 e.printStackTrace();
693                                 PHPeclipsePlugin.log(e);
694                         }
695                 }
696                 return unitVariables;
697         }
698
699         /**
700          * @param methodVariables
701          * @param method
702          */
703         private HashMap getMethodVariables(HashMap methodVariables, IMethod method) {
704                 if (methodVariables == null) {
705                         try {
706                                 String methodText = method.getSource();
707                                 methodVariables = new HashMap();
708                                 ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(),
709                                                 new CompilerOptions(JavaCore.getOptions()), new DefaultProblemFactory());
710                                 UnitParser parser = new UnitParser(problemReporter);
711                                 parser.compilationUnit = new CompilationUnitDeclaration(problemReporter, null, methodText.length());
712                                 parser.parseFunction(methodText, methodVariables);
713                         } catch (Exception e) {
714                                 // TODO Auto-generated catch block
715                                 e.printStackTrace();
716                                 PHPeclipsePlugin.log(e);
717                         }
718                 }
719                 return methodVariables;
720         }
721
722         /**
723          * @param viewer
724          * @param project
725          * @param context
726          * @param prefix
727          * @return
728          */
729         private void getVariableProposals(HashSet localVariables, ITextViewer viewer, IProject project, JavaContext context,
730                         HashMap variables, String prefix, int relevance) {
731                 // try {
732                 int start = context.getStart();
733                 int end = context.getEnd();
734                 IRegion region = new Region(start, end - start);
735                 // IMethod method = (IMethod)
736                 // context.findEnclosingElement(IJavaElement.METHOD);
737                 // if (method != null && (method instanceof SourceMethod) && ((SourceMethod)
738                 // method).variables != null) {
739                 // HashMap map = ((SourceMethod) method).variables;
740                 Set set = variables.keySet();
741                 Iterator iter = set.iterator();
742                 String varName;
743                 boolean matchesVarName;
744                 while (iter.hasNext()) {
745                         varName = (String) iter.next();
746                         if (varName.length() >= prefix.length()) {
747                                 matchesVarName = true;
748                                 for (int i = 0; i < prefix.length(); i++) {
749                                         if (prefix.charAt(i) != Character.toLowerCase(varName.charAt(i))) {
750                                                 matchesVarName = false;
751                                                 break;
752                                         }
753                                 }
754                                 if (matchesVarName) {
755                                         LocalVariableProposal prop;
756                                         // if (varName.length == prefix.length()) {
757                                         // prop = new LocalVariableProposal(new String(varName), region,
758                                         // viewer, relevance-10);
759                                         // } else {
760                                         prop = new LocalVariableProposal(new String(varName), region, viewer, relevance);
761                                         // }
762                                         localVariables.add(prop);
763                                 }
764                         }
765                 }
766                 // }
767
768                 // char[] varName;
769                 // boolean matchesVarName;
770                 // if (method != null) {
771                 // ISourceRange range = method.getSourceRange();
772                 // char[] source = method.getSource().toCharArray();
773                 // Scanner scanner = new Scanner();
774                 // scanner.setSource(source);
775                 // scanner.phpMode = true;
776                 // int token = Scanner.TokenNameWHITESPACE;
777                 // while ((token = scanner.getNextToken()) != Scanner.TokenNameEOF) {
778                 // if (token == Scanner.TokenNameVariable) {
779                 // varName = scanner.getCurrentTokenSource();
780                 // if (varName.length >= prefix.length()) {
781                 // matchesVarName = true;
782                 // for (int i = 0; i < prefix.length(); i++) {
783                 // if (prefix.charAt(i) != varName[i]) {
784                 // matchesVarName = false;
785                 // break;
786                 // }
787                 // }
788                 // if (matchesVarName) {
789                 // LocalVariableProposal prop = new LocalVariableProposal(new
790                 // String(varName), region, viewer);
791                 // if (varName.length == prefix.length()) {
792                 // prop.setRelevance(98);
793                 // }
794                 // localVariables.add(prop);
795                 // }
796                 // }
797                 // }
798                 // }
799                 // }
800                 // } catch (Throwable e) {
801                 // // ignore - Syntax exceptions could occur, if there are syntax errors !
802                 // }
803         }
804
805         /**
806          * @param viewer
807          * @param project
808          * @param context
809          * @param prefix
810          * @param sqlTable
811          * @param sqlResults
812          * @return
813          */
814         // private ICompletionProposal[] getSQLProposals(ITextViewer viewer, IProject
815         // project, DocumentTemplateContext context,
816         // String prefix, TableName sqlTable) {
817         // ICompletionProposal[] sqlResults = new ICompletionProposal[0];
818         // // Get The Database bookmark from the Quantum SQL plugin:
819         // // BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
820         // // if (sqlBookMarks != null) {
821         // String bookmarkString =
822         // ProjectPrefUtil.getMiscProjectsPreferenceValue(project,
823         // IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
824         // if (bookmarkString != null && !bookmarkString.equals("")) {
825         // String[] bookmarks = ExternalInterface.getBookmarkNames();
826         // boolean foundBookmark = false;
827         // for (int i = 0; i < bookmarks.length; i++) {
828         // if (bookmarks[i].equals(bookmarkString)) {
829         // foundBookmark = true;
830         // }
831         // }
832         // if (!foundBookmark) {
833         // return sqlResults;
834         // }
835         // // Bookmark bookmark = sqlBookMarks.find(bookmarkString);
836         // ArrayList sqlList = new ArrayList();
837         // if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
838         // ExternalInterface.connectBookmark(bookmarkString, null);
839         // if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
840         // return sqlResults;
841         // }
842         // }
843         // // if (ExternalInterface.isBookmarkConnected(bookmarkString)) {
844         // try {
845         // int start = context.getStart();
846         // int end = context.getEnd();
847         // String foundSQLTableName = sqlTable.getTableName();
848         // String tableName;
849         // String columnName;
850         // String prefixWithoutDollar = prefix;
851         // boolean isDollarPrefix = false;
852         // if (prefix.length() > 0 && prefix.charAt(0) == '$') {
853         // prefixWithoutDollar = prefix.substring(1);
854         // isDollarPrefix = true;
855         // }
856         // IRegion region = new Region(start, end - start);
857         // ResultSet set;
858         // if (!isDollarPrefix) {
859         // String[] tableNames = ExternalInterface.getMatchingTableNames(null,
860         // bookmarkString, prefixWithoutDollar, null, false);
861         // for (int i = 0; i < tableNames.length; i++) {
862         // sqlList.add(new SQLProposal(tableNames[i], context, region, viewer,
863         // PHPUiImages.get(PHPUiImages.IMG_TABLE)));
864         // }
865         // }
866         //
867         // String[] columnNames = ExternalInterface.getMatchingColumnNames(null,
868         // bookmarkString, prefixWithoutDollar, null, false);
869         // for (int i = 0; i < columnNames.length; i++) {
870         // sqlList.add(new SQLProposal(columnNames[i], context, region, viewer,
871         // PHPUiImages.get(PHPUiImages.IMG_TABLE)));
872         // }
873         //
874         // sqlResults = new IPHPCompletionProposal[sqlList.size()];
875         // for (int i = 0; i < sqlList.size(); i++) {
876         // sqlResults[i] = (SQLProposal) sqlList.get(i);
877         // }
878         // } catch (Exception /* NotConnectedException */ e) {
879         //
880         // }
881         // // }
882         // }
883         // // }
884         // return sqlResults;
885         // }
886         private boolean looksLikeMethod(PHPCodeReader reader) throws IOException {
887                 int curr = reader.read();
888                 while (curr != PHPCodeReader.EOF && Character.isWhitespace((char) curr))
889                         curr = reader.read();
890
891                 if (curr == PHPCodeReader.EOF)
892                         return false;
893
894                 return Scanner.isPHPIdentifierPart((char) curr);
895         }
896
897         private int guessContextInformationPosition(ITextViewer viewer, int offset) {
898                 int contextPosition = offset;
899                 IDocument document = viewer.getDocument();
900                 try {
901
902                         PHPCodeReader reader = new PHPCodeReader();
903                         reader.configureBackwardReader(document, offset, true, true);
904
905                         int nestingLevel = 0;
906
907                         int curr = reader.read();
908                         while (curr != PHPCodeReader.EOF) {
909
910                                 if (')' == (char) curr)
911                                         ++nestingLevel;
912
913                                 else if ('(' == (char) curr) {
914                                         --nestingLevel;
915
916                                         if (nestingLevel < 0) {
917                                                 int start = reader.getOffset();
918                                                 if (looksLikeMethod(reader))
919                                                         return start + 1;
920                                         }
921                                 }
922
923                                 curr = reader.read();
924                         }
925                 } catch (IOException e) {
926                 }
927                 return contextPosition;
928         }
929
930         /**
931          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
932          */
933         public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
934                 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
935                 List result = addContextInformations(viewer, contextInformationPosition);
936                 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
937         }
938
939         private List addContextInformations(ITextViewer viewer, int offset) {
940                 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
941                 List result = new ArrayList();
942                 for (int i = 0; i < proposals.length; i++) {
943                         IContextInformation contextInformation = proposals[i].getContextInformation();
944                         if (contextInformation != null) {
945                                 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
946                                 wrapper.setContextInformationPosition(offset);
947                                 result.add(wrapper);
948                         }
949                 }
950                 return result;
951         }
952
953         /**
954          * Order the given proposals.
955          */
956         private ICompletionProposal[] order(ICompletionProposal[] proposals) {
957                 Arrays.sort(proposals, fComparator);
958                 // int len = proposals.length;
959                 // if (len > 10) {
960                 // len = 10;
961                 // }
962                 // for (int i = 0; i < len; i++) {
963                 // System.out.println(proposals[i].getDisplayString());
964                 // }
965                 return proposals;
966         }
967
968         /*
969          * (non-Javadoc) Method declared on IContentAssistProcessor
970          */
971         public char[] getCompletionProposalAutoActivationCharacters() {
972                 return fProposalAutoActivationSet;
973                 // return null; // new char[] { '$' };
974         }
975
976         /*
977          * (non-Javadoc) Method declared on IContentAssistProcessor
978          */
979         public char[] getContextInformationAutoActivationCharacters() {
980                 return null;
981         }
982
983         /*
984          * (non-Javadoc) Method declared on IContentAssistProcessor
985          */
986         public IContextInformationValidator getContextInformationValidator() {
987                 if (fValidator == null)
988                         fValidator = new JavaParameterListValidator();
989                 return fValidator;
990         }
991
992         /*
993          * (non-Javadoc) Method declared on IContentAssistProcessor
994          */
995         public String getErrorMessage() {
996                 return null;
997         }
998 }