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
9 IBM Corporation - Initial implementation
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor.php;
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;
21 import java.util.SortedMap;
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.impl.CompilerOptions;
33 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
34 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
35 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
36 import net.sourceforge.phpdt.internal.compiler.parser.VariableInfo;
37 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
38 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
39 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
40 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
41 import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader;
42 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
43 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
44 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
45 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
46 import net.sourceforge.phpdt.internal.ui.text.template.LocalVariableProposal;
47 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
48 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
49 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
50 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
51 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
52 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
53 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
55 import org.eclipse.core.resources.IFile;
56 import org.eclipse.core.resources.IProject;
57 import org.eclipse.jface.text.BadLocationException;
58 import org.eclipse.jface.text.IDocument;
59 import org.eclipse.jface.text.IRegion;
60 import org.eclipse.jface.text.ITextViewer;
61 import org.eclipse.jface.text.Region;
62 import org.eclipse.jface.text.TextPresentation;
63 import org.eclipse.jface.text.contentassist.ICompletionProposal;
64 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
65 import org.eclipse.jface.text.contentassist.IContextInformation;
66 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
67 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
68 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
69 import org.eclipse.jface.text.templates.TemplateContextType;
70 import org.eclipse.swt.graphics.Image;
71 import org.eclipse.swt.graphics.Point;
72 import org.eclipse.ui.IEditorPart;
73 import org.eclipse.ui.IFileEditorInput;
75 //import com.quantum.ExternalInterface;
76 // import com.quantum.util.connection.NotConnectedException;
79 * Example PHP completion processor.
81 public class PHPCompletionProcessor implements IContentAssistProcessor {
83 * Simple content assist tip closer. The tip is valid in a range of 5 characters around its popup location.
85 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
86 protected int fInstallOffset;
89 * @see IContextInformationValidator#isContextInformationValid(int)
91 public boolean isContextInformationValid(int offset) {
92 return Math.abs(fInstallOffset - offset) < 5;
96 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
98 public void install(IContextInformation info, ITextViewer viewer, int offset) {
99 fInstallOffset = offset;
103 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
105 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
110 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
111 private final IContextInformation fContextInformation;
113 private int fPosition;
115 public ContextInformationWrapper(IContextInformation contextInformation) {
116 fContextInformation = contextInformation;
120 * @see IContextInformation#getContextDisplayString()
122 public String getContextDisplayString() {
123 return fContextInformation.getContextDisplayString();
127 * @see IContextInformation#getImage()
129 public Image getImage() {
130 return fContextInformation.getImage();
134 * @see IContextInformation#getInformationDisplayString()
136 public String getInformationDisplayString() {
137 return fContextInformation.getInformationDisplayString();
141 * @see IContextInformationExtension#getContextInformationPosition()
143 public int getContextInformationPosition() {
147 public void setContextInformationPosition(int position) {
148 fPosition = position;
152 private class TableName {
160 * @return Returns the tableName.
162 public String getTableName() {
163 if (fTableName == null) {
164 return "<!--no-table-->";
171 * The tableName to set.
173 public void setTableName(String tableName) {
174 fTableName = tableName;
178 private char[] fProposalAutoActivationSet;
180 protected IContextInformationValidator fValidator = new Validator();
182 private TemplateEngine fTemplateEngine;
184 private PHPCompletionProposalComparator fComparator;
186 private int fNumberOfComputedResults = 0;
188 private IEditorPart fEditor;
190 protected IWorkingCopyManager fManager;
192 public PHPCompletionProcessor(IEditorPart editor) {
194 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
195 TemplateContextType contextType = PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType("php"); //$NON-NLS-1$
196 if (contextType != null)
197 fTemplateEngine = new TemplateEngine(contextType);
198 fComparator = new PHPCompletionProposalComparator();
202 * Tells this processor to order the proposals alphabetically.
205 * <code>true</code> if proposals should be ordered.
207 public void orderProposalsAlphabetically(boolean order) {
208 fComparator.setOrderAlphabetically(order);
212 * Sets this processor's set of characters triggering the activation of the completion proposal computation.
214 * @param activationSet
217 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
218 fProposalAutoActivationSet = activationSet;
222 * (non-Javadoc) Method declared on IContentAssistProcessor
224 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
225 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
226 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
229 private int getLastToken(List list, ITextViewer viewer, int completionPosition, JavaContext context, TableName tableName) {
230 IDocument document = viewer.getDocument();
231 int start = context.getStart();
232 int end = context.getEnd();
234 int lastSignificantToken = ITerminalSymbols.TokenNameEOF;
236 // begin search 2 lines behind of this
241 ch = document.getChar(j);
247 ch = document.getChar(j);
254 // scan the line for the dereferencing operator '->'
255 startText = document.get(j, start - j);
257 System.out.println(startText);
259 int token = ITerminalSymbols.TokenNameEOF;
260 // token = getLastSQLToken(startText);
261 tableName.setTableName(getLastSQLTableName(startText));
262 Scanner scanner = ToolFactory.createScanner(false, false, false);
263 scanner.setSource(startText.toCharArray());
264 scanner.setPHPMode(true);
265 int beforeLastToken = ITerminalSymbols.TokenNameEOF;
266 int lastToken = ITerminalSymbols.TokenNameEOF;
269 token = scanner.getNextToken();
271 while (token != ITerminalSymbols.TokenNameERROR && token != ITerminalSymbols.TokenNameEOF) {
272 beforeLastToken = lastToken;
273 if (token == ITerminalSymbols.TokenNameVariable) {
274 ident = scanner.getCurrentTokenSource();
275 if (ident.length == 5 && ident[0] == '$' && ident[1] == 't' && ident[2] == 'h' && ident[3] == 'i' && ident[4] == 's') {
276 token = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
280 // System.out.println(scanner.toStringAction(lastToken));
281 token = scanner.getNextToken();
283 } catch (InvalidInputException e1) {
284 } catch (SyntaxError e) {
287 case ITerminalSymbols.TokenNameMINUS_GREATER:
288 // dereferencing operator '->' found
289 lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
290 if (beforeLastToken == ITerminalSymbols.TokenNameVariable) {
291 lastSignificantToken = ITerminalSymbols.TokenNameVariable;
293 } else if (beforeLastToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION) {
294 lastSignificantToken = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
298 case ITerminalSymbols.TokenNamenew:
299 lastSignificantToken = ITerminalSymbols.TokenNamenew;
303 } catch (BadLocationException e) {
305 return lastSignificantToken;
308 String getSQLTableName(String sqlText, int start) {
309 int tableNameStart = -1;
310 int currentCharacterPosition = start + 1;
314 ch = sqlText.charAt(currentCharacterPosition++);
315 if (tableNameStart == -1 && Scanner.isPHPIdentifierStart(ch)) {
316 tableNameStart = currentCharacterPosition - 1;
318 if (!Scanner.isPHPIdentifierPart(ch)) {
319 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
323 } catch (IndexOutOfBoundsException e) {
324 if (tableNameStart >= 0) {
325 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
331 private String getLastSQLTableName(String startText) {
333 // scan for sql identifiers
335 int currentSQLPosition = startText.length();
338 boolean whiteSpace = true;
341 ch = startText.charAt(--currentSQLPosition);
342 if (Scanner.isSQLIdentifierPart(ch)) {
343 // if (ch >= 'A' && ch <= 'Z') {
345 identEnd = currentSQLPosition + 1;
347 // } else if (ch >= 'a' && ch <= 'z') {
348 // if (identEnd < 0) {
349 // identEnd = currentSQLPosition + 1;
351 } else if (identEnd >= 0) {
352 ident = startText.substring(currentSQLPosition + 1, identEnd);
353 // select -- from -- where --
354 // update -- set -- where --
355 // insert into -- ( -- ) values ( -- )
356 if (ident.length() >= 4 && ident.length() <= 6) {
357 ident = ident.toLowerCase();
358 switch (ident.length()) {
360 // if (ident.equals("set")) {
361 // // System.out.println("set");
362 // token = ITerminalSymbols.TokenNameSQLset;
367 if (ident.equals("from")) {
368 // System.out.println("from");
369 token = ITerminalSymbols.TokenNameSQLfrom;
370 return getSQLTableName(startText, identEnd);
371 } else if (ident.equals("into")) {
372 // System.out.println("into");
373 token = ITerminalSymbols.TokenNameSQLinto;
374 return getSQLTableName(startText, identEnd);
378 // if (ident.equals("where")) {
379 // // System.out.println("where");
380 // token = ITerminalSymbols.TokenNameSQLwhere;
385 // if (ident.equals("select")) {
386 // // System.out.println("select");
387 // token = ITerminalSymbols.TokenNameSQLselect;
389 // } else if (ident.equals("insert")) {
390 // // System.out.println("insert");
391 // token = ITerminalSymbols.TokenNameSQLinsert;
394 if (ident.equals("update")) {
395 // System.out.println("update");
396 token = ITerminalSymbols.TokenNameSQLupdate;
397 return getSQLTableName(startText, identEnd);
399 // else if (ident.equals("values")) {
400 // // System.out.println("values");
401 // token = ITerminalSymbols.TokenNameSQLvalues;
409 } else if (Character.isWhitespace(ch)) {
414 } catch (IndexOutOfBoundsException e) {
416 return "<!--no-table-->";
420 * Detect the last significant SQL token in the text before the completion
424 private int getLastSQLToken(String startText) {
426 // scan for sql identifiers
428 int currentSQLPosition = startText.length();
431 boolean whiteSpace = true;
434 ch = startText.charAt(--currentSQLPosition);
435 if (ch >= 'A' && ch <= 'Z') {
437 identEnd = currentSQLPosition + 1;
439 } else if (ch >= 'a' && ch <= 'z') {
441 identEnd = currentSQLPosition + 1;
443 } else if (identEnd >= 0) {
444 ident = startText.substring(currentSQLPosition + 1, identEnd);
445 // select -- from -- where --
446 // update -- set -- where --
447 // insert into -- ( -- ) values ( -- )
448 if (ident.length() >= 3 && ident.length() <= 6) {
449 ident = ident.toLowerCase();
450 switch (ident.length()) {
452 if (ident.equals("set")) {
453 // System.out.println("set");
454 token = ITerminalSymbols.TokenNameSQLset;
459 if (ident.equals("from")) {
460 // System.out.println("from");
461 token = ITerminalSymbols.TokenNameSQLfrom;
464 } else if (ident.equals("into")) {
465 // System.out.println("into");
466 token = ITerminalSymbols.TokenNameSQLinto;
471 if (ident.equals("where")) {
472 // System.out.println("where");
473 token = ITerminalSymbols.TokenNameSQLwhere;
478 if (ident.equals("select")) {
479 // System.out.println("select");
480 token = ITerminalSymbols.TokenNameSQLselect;
482 } else if (ident.equals("insert")) {
483 // System.out.println("insert");
484 token = ITerminalSymbols.TokenNameSQLinsert;
486 } else if (ident.equals("update")) {
487 // System.out.println("update");
488 token = ITerminalSymbols.TokenNameSQLupdate;
490 } else if (ident.equals("values")) {
491 // System.out.println("values");
492 token = ITerminalSymbols.TokenNameSQLvalues;
500 } else if (Character.isWhitespace(ch)) {
505 } catch (IndexOutOfBoundsException e) {
507 return ITerminalSymbols.TokenNameEOF;
510 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
511 ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
512 IDocument document = viewer.getDocument();
514 IProject project = null;
516 PHPEditor editor = null;
517 if (fEditor != null && (fEditor instanceof PHPEditor)) {
518 editor = (PHPEditor) fEditor;
519 file = ((IFileEditorInput) editor.getEditorInput()).getFile();
520 project = file.getProject();
524 Point selection = viewer.getSelectedRange();
525 // remember selected text
526 String selectedText = null;
527 if (selection.y != 0) {
529 selectedText = document.get(selection.x, selection.y);
530 } catch (BadLocationException e) {
534 JavaContextType phpContextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
535 "php"); //$NON-NLS-1$
536 JavaContext context = (JavaContext) phpContextType.createContext(document, offset, selection.y, unit);
537 context.setVariable("selection", selectedText); //$NON-NLS-1$
538 String prefix = context.getKey();
540 HashMap methodVariables = null;
541 HashMap typeVariables = null;
542 HashMap unitVariables = null;
543 ICompilationUnit compilationUnit = (ICompilationUnit) context.findEnclosingElement(IJavaElement.COMPILATION_UNIT);
544 // if (compilationUnit != null) {
545 // unitVariables = ((CompilationUnit) compilationUnit).variables;
547 IType type = (IType) context.findEnclosingElement(IJavaElement.TYPE);
548 // if (type != null) {
549 // typeVariables = ((SourceType) type).variables;
551 IMethod method = (IMethod) context.findEnclosingElement(IJavaElement.METHOD);
552 // if (method != null) {
553 // methodVariables = ((SourceMethod) method).variables;
556 boolean emptyPrefix = prefix == null || prefix.equals("");
557 IPHPCompletionProposal[] localVariableResults = new IPHPCompletionProposal[0];
559 if (!emptyPrefix && prefix.length() >= 1 && prefix.charAt(0) == '$') { // php Variable ?
560 String lowerCasePrefix = prefix.toLowerCase();
561 HashSet localVariables = new HashSet();
562 if (compilationUnit != null) {
563 unitVariables = getUnitVariables(unitVariables, compilationUnit);
564 getVariableProposals(localVariables, viewer, project, context, unitVariables, lowerCasePrefix, 94);
566 if (method != null) {
567 methodVariables = getMethodVariables(methodVariables, method);
568 getVariableProposals(localVariables, viewer, project, context, methodVariables, lowerCasePrefix, 99);
570 if (!localVariables.isEmpty()) {
571 localVariableResults = (IPHPCompletionProposal[]) localVariables.toArray(new IPHPCompletionProposal[localVariables.size()]);
575 TableName sqlTable = new TableName();
576 ArrayList list = new ArrayList();
578 int lastSignificantToken = getLastToken(list, viewer, offset, context, sqlTable);
579 boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER)
580 || (lastSignificantToken == ITerminalSymbols.TokenNameVariable) || (lastSignificantToken == ITerminalSymbols.TokenNamenew)
581 || (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION);
583 if (fTemplateEngine != null) {
584 IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
585 ICompletionProposal[] results;
587 fTemplateEngine.reset();
588 fTemplateEngine.complete(viewer, offset, unit);
589 templateResults = fTemplateEngine.getResults();
592 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
594 // declarations stored in file project.index on project level
595 IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
596 if (project != null) {
597 DeclarationEngine declarationEngine;
598 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
599 "php"); //$NON-NLS-1$
600 if (contextType != null) {
601 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
603 declarationEngine = new DeclarationEngine(project, contextType, lastSignificantToken, file);
604 if (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION) {
605 // complete '$this->'
606 sortedMap = indexManager.getIdentifiers(file);
607 declarationEngine.completeObject(viewer, offset, sortedMap, unit);
609 String typeRef = null;
610 char[] varName = (char[]) list.get(0);
611 if (varName != null) {
612 if (method != null) {
613 methodVariables = getMethodVariables(methodVariables, method);
614 VariableInfo info = (VariableInfo) methodVariables.get(new String(varName));
615 if (info != null && info.typeIdentifier != null) {
616 typeRef = new String(info.typeIdentifier);
621 if (typeRef != null) {
622 // complete '$variable->' with type information
623 sortedMap = indexManager.getIdentifiers(typeRef);
624 declarationEngine.completeObject(viewer, offset, sortedMap, unit);
626 // complete '$variable->' without type information
627 sortedMap = indexManager.getIdentifierMap();
628 declarationEngine.complete(viewer, offset, sortedMap, unit);
631 declarationResults = declarationEngine.getResults();
634 // built in function names from phpsyntax.xml
635 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
636 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
637 if ((!useClassMembers) && syntaxbuffer != null) {
638 BuiltInEngine builtinEngine;
640 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
641 "php"); //$NON-NLS-1$
642 if (contextType != null) {
643 builtinEngine = new BuiltInEngine(contextType);
644 builtinEngine.complete(viewer, offset, syntaxbuffer, unit);
645 builtinResults = builtinEngine.getResults();
648 // ICompletionProposal[] sqlResults = new ICompletionProposal[0];
649 // if (project != null) {
650 // sqlResults = getSQLProposals(viewer, project, context, prefix, sqlTable);
652 // concatenate the result arrays
653 IPHPCompletionProposal[] total;
654 total = new IPHPCompletionProposal[localVariableResults.length + templateResults.length + identifierResults.length
655 + builtinResults.length + declarationResults.length];// + sqlResults.length];
656 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
657 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
658 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
659 System.arraycopy(declarationResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length,
660 declarationResults.length);
661 // System.arraycopy(sqlResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
662 // + declarationResults.length, sqlResults.length);
663 // System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
664 // + declarationResults.length + sqlResults.length, localVariableResults.length);
665 System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
666 + declarationResults.length, localVariableResults.length);
668 fNumberOfComputedResults = (results == null ? 0 : results.length);
670 * Order here and not in result collector to make sure that the order applies to all proposals and not just those of the
673 return order(results);
675 return new IPHPCompletionProposal[0];
679 * @param unitVariables
682 private HashMap getUnitVariables(HashMap unitVariables, ICompilationUnit unit) {
683 if (unitVariables == null) {
685 String unitText = unit.getSource();
686 unitVariables = new HashMap();
688 ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(),
689 new CompilerOptions(JavaCore.getOptions()), new DefaultProblemFactory());
690 UnitParser parser = new UnitParser(problemReporter);
691 parser.compilationUnit = new CompilationUnitDeclaration(problemReporter, null, unitText.length());
692 parser.parse(unitText, unitVariables);
694 } catch (Exception e) {
695 // TODO Auto-generated catch block
697 PHPeclipsePlugin.log(e);
700 return unitVariables;
704 * @param methodVariables
707 private HashMap getMethodVariables(HashMap methodVariables, IMethod method) {
708 if (methodVariables == null) {
710 String methodText = method.getSource();
711 methodVariables = new HashMap();
712 ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(),
713 new CompilerOptions(JavaCore.getOptions()), new DefaultProblemFactory());
714 UnitParser parser = new UnitParser(problemReporter);
715 parser.compilationUnit = new CompilationUnitDeclaration(problemReporter, null, methodText.length());
716 parser.parseFunction(methodText, methodVariables);
717 } catch (Exception e) {
718 // TODO Auto-generated catch block
720 PHPeclipsePlugin.log(e);
723 return methodVariables;
733 private void getVariableProposals(HashSet localVariables, ITextViewer viewer, IProject project, JavaContext context,
734 HashMap variables, String prefix, int relevance) {
736 int start = context.getStart();
737 int end = context.getEnd();
738 IRegion region = new Region(start, end - start);
739 // IMethod method = (IMethod) context.findEnclosingElement(IJavaElement.METHOD);
740 // if (method != null && (method instanceof SourceMethod) && ((SourceMethod) method).variables != null) {
741 // HashMap map = ((SourceMethod) method).variables;
742 Set set = variables.keySet();
743 Iterator iter = set.iterator();
745 boolean matchesVarName;
746 while (iter.hasNext()) {
747 varName = (String) iter.next();
748 if (varName.length() >= prefix.length()) {
749 matchesVarName = true;
750 for (int i = 0; i < prefix.length(); i++) {
751 if (prefix.charAt(i) != Character.toLowerCase(varName.charAt(i))) {
752 matchesVarName = false;
756 if (matchesVarName) {
757 LocalVariableProposal prop;
758 // if (varName.length == prefix.length()) {
759 // prop = new LocalVariableProposal(new String(varName), region, viewer, relevance-10);
761 prop = new LocalVariableProposal(new String(varName), region, viewer, relevance);
763 localVariables.add(prop);
770 // boolean matchesVarName;
771 // if (method != null) {
772 // ISourceRange range = method.getSourceRange();
773 // char[] source = method.getSource().toCharArray();
774 // Scanner scanner = new Scanner();
775 // scanner.setSource(source);
776 // scanner.phpMode = true;
777 // int token = Scanner.TokenNameWHITESPACE;
778 // while ((token = scanner.getNextToken()) != Scanner.TokenNameEOF) {
779 // if (token == Scanner.TokenNameVariable) {
780 // varName = scanner.getCurrentTokenSource();
781 // if (varName.length >= prefix.length()) {
782 // matchesVarName = true;
783 // for (int i = 0; i < prefix.length(); i++) {
784 // if (prefix.charAt(i) != varName[i]) {
785 // matchesVarName = false;
789 // if (matchesVarName) {
790 // LocalVariableProposal prop = new LocalVariableProposal(new String(varName), region, viewer);
791 // if (varName.length == prefix.length()) {
792 // prop.setRelevance(98);
794 // localVariables.add(prop);
800 // } catch (Throwable e) {
801 // // ignore - Syntax exceptions could occur, if there are syntax errors !
814 // private ICompletionProposal[] getSQLProposals(ITextViewer viewer, IProject project, DocumentTemplateContext context,
815 // String prefix, TableName sqlTable) {
816 // ICompletionProposal[] sqlResults = new ICompletionProposal[0];
817 // // Get The Database bookmark from the Quantum SQL plugin:
818 // // BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
819 // // if (sqlBookMarks != null) {
820 // String bookmarkString = ProjectPrefUtil.getMiscProjectsPreferenceValue(project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
821 // if (bookmarkString != null && !bookmarkString.equals("")) {
822 // String[] bookmarks = ExternalInterface.getBookmarkNames();
823 // boolean foundBookmark = false;
824 // for (int i = 0; i < bookmarks.length; i++) {
825 // if (bookmarks[i].equals(bookmarkString)) {
826 // foundBookmark = true;
829 // if (!foundBookmark) {
830 // return sqlResults;
832 // // Bookmark bookmark = sqlBookMarks.find(bookmarkString);
833 // ArrayList sqlList = new ArrayList();
834 // if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
835 // ExternalInterface.connectBookmark(bookmarkString, null);
836 // if (!ExternalInterface.isBookmarkConnected(bookmarkString)) {
837 // return sqlResults;
840 // // if (ExternalInterface.isBookmarkConnected(bookmarkString)) {
842 // int start = context.getStart();
843 // int end = context.getEnd();
844 // String foundSQLTableName = sqlTable.getTableName();
846 // String columnName;
847 // String prefixWithoutDollar = prefix;
848 // boolean isDollarPrefix = false;
849 // if (prefix.length() > 0 && prefix.charAt(0) == '$') {
850 // prefixWithoutDollar = prefix.substring(1);
851 // isDollarPrefix = true;
853 // IRegion region = new Region(start, end - start);
855 // if (!isDollarPrefix) {
856 // String[] tableNames = ExternalInterface.getMatchingTableNames(null, bookmarkString, prefixWithoutDollar, null, false);
857 // for (int i = 0; i < tableNames.length; i++) {
858 // sqlList.add(new SQLProposal(tableNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
862 // String[] columnNames = ExternalInterface.getMatchingColumnNames(null, bookmarkString, prefixWithoutDollar, null, false);
863 // for (int i = 0; i < columnNames.length; i++) {
864 // sqlList.add(new SQLProposal(columnNames[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
867 // sqlResults = new IPHPCompletionProposal[sqlList.size()];
868 // for (int i = 0; i < sqlList.size(); i++) {
869 // sqlResults[i] = (SQLProposal) sqlList.get(i);
871 // } catch (Exception /* NotConnectedException */ e) {
877 // return sqlResults;
880 private boolean looksLikeMethod(PHPCodeReader reader) throws IOException {
881 int curr = reader.read();
882 while (curr != PHPCodeReader.EOF && Character.isWhitespace((char) curr))
883 curr = reader.read();
885 if (curr == PHPCodeReader.EOF)
888 return Scanner.isPHPIdentifierPart((char) curr);
891 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
892 int contextPosition = offset;
893 IDocument document = viewer.getDocument();
896 PHPCodeReader reader = new PHPCodeReader();
897 reader.configureBackwardReader(document, offset, true, true);
899 int nestingLevel = 0;
901 int curr = reader.read();
902 while (curr != PHPCodeReader.EOF) {
904 if (')' == (char) curr)
907 else if ('(' == (char) curr) {
910 if (nestingLevel < 0) {
911 int start = reader.getOffset();
912 if (looksLikeMethod(reader))
917 curr = reader.read();
919 } catch (IOException e) {
921 return contextPosition;
925 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
927 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
928 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
929 List result = addContextInformations(viewer, contextInformationPosition);
930 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
933 private List addContextInformations(ITextViewer viewer, int offset) {
934 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
935 List result = new ArrayList();
936 for (int i = 0; i < proposals.length; i++) {
937 IContextInformation contextInformation = proposals[i].getContextInformation();
938 if (contextInformation != null) {
939 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
940 wrapper.setContextInformationPosition(offset);
948 * Order the given proposals.
950 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
951 Arrays.sort(proposals, fComparator);
952 // int len = proposals.length;
956 // for (int i = 0; i < len; i++) {
957 // System.out.println(proposals[i].getDisplayString());
963 * (non-Javadoc) Method declared on IContentAssistProcessor
965 public char[] getCompletionProposalAutoActivationCharacters() {
966 return fProposalAutoActivationSet;
967 // return null; // new char[] { '$' };
971 * (non-Javadoc) Method declared on IContentAssistProcessor
973 public char[] getContextInformationAutoActivationCharacters() {
974 return new char[] {};
978 * (non-Javadoc) Method declared on IContentAssistProcessor
980 public IContextInformationValidator getContextInformationValidator() {
985 * (non-Javadoc) Method declared on IContentAssistProcessor
987 public String getErrorMessage() {