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.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;
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;
77 * Example PHP completion processor.
79 public class PHPCompletionProcessor implements IContentAssistProcessor {
81 * Simple content assist tip closer. The tip is valid in a range of 5
82 * characters around its popup location.
84 // protected static class Validator implements IContextInformationValidator,
85 // 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,
99 // public void install(IContextInformation info, ITextViewer viewer, int
101 // fInstallOffset = offset;
106 // org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
109 // public boolean updatePresentation(int documentPosition, TextPresentation
114 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
115 private final IContextInformation fContextInformation;
117 private int fPosition;
119 public ContextInformationWrapper(IContextInformation contextInformation) {
120 fContextInformation = contextInformation;
124 * @see IContextInformation#getContextDisplayString()
126 public String getContextDisplayString() {
127 return fContextInformation.getContextDisplayString();
131 * @see IContextInformation#getImage()
133 public Image getImage() {
134 return fContextInformation.getImage();
138 * @see IContextInformation#getInformationDisplayString()
140 public String getInformationDisplayString() {
141 return fContextInformation.getInformationDisplayString();
145 * @see IContextInformationExtension#getContextInformationPosition()
147 public int getContextInformationPosition() {
151 public void setContextInformationPosition(int position) {
152 fPosition = position;
156 // private class TableName {
157 // String fTableName;
160 // fTableName = null;
164 // * @return Returns the tableName.
166 // public String getTableName() {
167 // if (fTableName == null) {
168 // return "<!--no-table-->";
170 // return fTableName;
174 // * @param tableName
175 // * The tableName to set.
177 // public void setTableName(String tableName) {
178 // fTableName = tableName;
182 private char[] fProposalAutoActivationSet;
184 protected IContextInformationValidator fValidator = null;
186 private TemplateEngine fTemplateEngine;
188 private PHPCompletionProposalComparator fComparator;
190 private IEditorPart fEditor;
192 protected IWorkingCopyManager fManager;
194 public PHPCompletionProcessor(IEditorPart 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();
204 * Tells this processor to order the proposals alphabetically.
207 * <code>true</code> if proposals should be ordered.
209 public void orderProposalsAlphabetically(boolean order) {
210 fComparator.setOrderAlphabetically(order);
214 * Sets this processor's set of characters triggering the activation of the
215 * completion proposal computation.
217 * @param activationSet
220 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
221 fProposalAutoActivationSet = activationSet;
225 * (non-Javadoc) Method declared on IContentAssistProcessor
227 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
228 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
229 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
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();
238 int lastSignificantToken = ITerminalSymbols.TokenNameEOF;
240 // begin search 2 lines behind of this
245 ch = document.getChar(j);
251 ch = document.getChar(j);
258 // scan the line for the dereferencing operator '->'
259 startText = document.get(j, start - j);
261 System.out.println(startText);
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;
273 token = scanner.getNextToken();
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;
284 // System.out.println(scanner.toStringAction(lastToken));
285 token = scanner.getNextToken();
287 } catch (InvalidInputException e1) {
288 } catch (SyntaxError e) {
291 case ITerminalSymbols.TokenNameMINUS_GREATER:
292 // dereferencing operator '->' found
293 lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
294 if (beforeLastToken == ITerminalSymbols.TokenNameVariable) {
295 lastSignificantToken = ITerminalSymbols.TokenNameVariable;
297 } else if (beforeLastToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION) {
298 lastSignificantToken = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
302 case ITerminalSymbols.TokenNamenew:
303 lastSignificantToken = ITerminalSymbols.TokenNamenew;
307 } catch (BadLocationException e) {
309 return lastSignificantToken;
312 String getSQLTableName(String sqlText, int start) {
313 int tableNameStart = -1;
314 int currentCharacterPosition = start + 1;
318 ch = sqlText.charAt(currentCharacterPosition++);
319 if (tableNameStart == -1 && Scanner.isPHPIdentifierStart(ch)) {
320 tableNameStart = currentCharacterPosition - 1;
322 if (!Scanner.isPHPIdentifierPart(ch)) {
323 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
327 } catch (IndexOutOfBoundsException e) {
328 if (tableNameStart >= 0) {
329 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
335 // private String getLastSQLTableName(String startText) {
336 // // scan for sql identifiers
338 // int currentSQLPosition = startText.length();
339 // int identEnd = -1;
340 // String ident = null;
343 // ch = startText.charAt(--currentSQLPosition);
344 // if (Scanner.isSQLIdentifierPart(ch)) {
345 // // if (ch >= 'A' && ch <= 'Z') {
346 // if (identEnd < 0) {
347 // identEnd = currentSQLPosition + 1;
349 // // } else if (ch >= 'a' && ch <= 'z') {
350 // // if (identEnd < 0) {
351 // // identEnd = currentSQLPosition + 1;
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()) {
362 // // if (ident.equals("set")) {
363 // // // System.out.println("set");
364 // // token = ITerminalSymbols.TokenNameSQLset;
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);
378 // if (ident.equals("update")) {
379 // // System.out.println("update");
380 // return getSQLTableName(startText, identEnd);
386 // } else if (Character.isWhitespace(ch)) {
389 // } catch (IndexOutOfBoundsException e) {
391 // return "<!--no-table-->";
395 * Detect the last significant SQL token in the text before the completion
399 // private int getLastSQLToken(String startText) {
401 // // scan for sql identifiers
403 // int currentSQLPosition = startText.length();
404 // int identEnd = -1;
405 // String ident = null;
408 // ch = startText.charAt(--currentSQLPosition);
409 // if (ch >= 'A' && ch <= 'Z') {
410 // if (identEnd < 0) {
411 // identEnd = currentSQLPosition + 1;
413 // } else if (ch >= 'a' && ch <= 'z') {
414 // if (identEnd < 0) {
415 // identEnd = currentSQLPosition + 1;
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()) {
426 // if (ident.equals("set")) {
427 // // System.out.println("set");
428 // token = ITerminalSymbols.TokenNameSQLset;
433 // if (ident.equals("from")) {
434 // // System.out.println("from");
435 // token = ITerminalSymbols.TokenNameSQLfrom;
436 // // getSQLTableName();
438 // } else if (ident.equals("into")) {
439 // // System.out.println("into");
440 // token = ITerminalSymbols.TokenNameSQLinto;
445 // if (ident.equals("where")) {
446 // // System.out.println("where");
447 // token = ITerminalSymbols.TokenNameSQLwhere;
452 // if (ident.equals("select")) {
453 // // System.out.println("select");
454 // token = ITerminalSymbols.TokenNameSQLselect;
456 // } else if (ident.equals("insert")) {
457 // // System.out.println("insert");
458 // token = ITerminalSymbols.TokenNameSQLinsert;
460 // } else if (ident.equals("update")) {
461 // // System.out.println("update");
462 // token = ITerminalSymbols.TokenNameSQLupdate;
464 // } else if (ident.equals("values")) {
465 // // System.out.println("values");
466 // token = ITerminalSymbols.TokenNameSQLvalues;
475 // } catch (IndexOutOfBoundsException e) {
477 // return ITerminalSymbols.TokenNameEOF;
480 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
481 ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
482 IDocument document = viewer.getDocument();
484 IProject project = null;
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();
494 Point selection = viewer.getSelectedRange();
495 // remember selected text
496 String selectedText = null;
497 if (selection.y != 0) {
499 selectedText = document.get(selection.x, selection.y);
500 } catch (BadLocationException e) {
504 if (offset > 2 && fProposalAutoActivationSet != null) {
505 // restrict auto activation for '>' character to '->' token
508 char ch = document.getChar(offset-1);
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);
515 return new IPHPCompletionProposal[0];
521 } catch (BadLocationException e) {
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();
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;
539 IType type = (IType) context.findEnclosingElement(IJavaElement.TYPE);
541 // typeVariables = ((SourceType) type).variables;
543 IMethod method = (IMethod) context.findEnclosingElement(IJavaElement.METHOD);
544 // if (method != null) {
545 // methodVariables = ((SourceMethod) method).variables;
548 boolean emptyPrefix = prefix == null || prefix.equals("");
549 IPHPCompletionProposal[] localVariableResults = new IPHPCompletionProposal[0];
551 if (!emptyPrefix && prefix.length() >= 1 && prefix.charAt(0) == '$') {
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);
559 if (method != null) {
560 methodVariables = getMethodVariables(methodVariables, method);
561 getVariableProposals(localVariables, viewer, project, context, methodVariables, lowerCasePrefix, 99);
563 if (!localVariables.isEmpty()) {
564 localVariableResults = (IPHPCompletionProposal[]) localVariables.toArray(new IPHPCompletionProposal[localVariables.size()]);
568 // TableName sqlTable = new TableName();
569 ArrayList list = new ArrayList();
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);
576 if (fTemplateEngine != null) {
577 IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
578 ICompletionProposal[] results;
580 fTemplateEngine.reset();
581 fTemplateEngine.complete(viewer, offset, unit);
582 templateResults = fTemplateEngine.getResults();
585 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
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);
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);
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);
613 if (typeRef != null) {
614 // complete '$variable->' with type information
615 sortedMap = indexManager.getIdentifiers(typeRef);
616 declarationEngine.completeObject(viewer, offset, sortedMap, unit);
618 // complete '$variable->' without type information
619 sortedMap = indexManager.getIdentifierMap();
620 declarationEngine.complete(viewer, offset, sortedMap, unit);
623 declarationResults = declarationEngine.getResults();
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();
639 // ICompletionProposal[] sqlResults = new ICompletionProposal[0];
640 // if (project != null) {
641 // sqlResults = getSQLProposals(viewer, project, context, prefix,
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);
664 // fNumberOfComputedResults = (results == null ? 0 : results.length);
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.
669 return order(results);
671 return new IPHPCompletionProposal[0];
675 * @param unitVariables
678 private HashMap getUnitVariables(HashMap unitVariables, ICompilationUnit unit) {
679 if (unitVariables == null) {
681 String unitText = unit.getSource();
682 unitVariables = new HashMap();
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);
690 } catch (Exception e) {
691 // TODO Auto-generated catch block
693 PHPeclipsePlugin.log(e);
696 return unitVariables;
700 * @param methodVariables
703 private HashMap getMethodVariables(HashMap methodVariables, IMethod method) {
704 if (methodVariables == null) {
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
716 PHPeclipsePlugin.log(e);
719 return methodVariables;
729 private void getVariableProposals(HashSet localVariables, ITextViewer viewer, IProject project, JavaContext context,
730 HashMap variables, String prefix, int relevance) {
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();
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;
754 if (matchesVarName) {
755 LocalVariableProposal prop;
756 // if (varName.length == prefix.length()) {
757 // prop = new LocalVariableProposal(new String(varName), region,
758 // viewer, relevance-10);
760 prop = new LocalVariableProposal(new String(varName), region, viewer, relevance);
762 localVariables.add(prop);
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;
788 // if (matchesVarName) {
789 // LocalVariableProposal prop = new LocalVariableProposal(new
790 // 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
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;
832 // if (!foundBookmark) {
833 // return sqlResults;
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;
843 // // if (ExternalInterface.isBookmarkConnected(bookmarkString)) {
845 // int start = context.getStart();
846 // int end = context.getEnd();
847 // String foundSQLTableName = sqlTable.getTableName();
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;
856 // IRegion region = new Region(start, end - start);
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)));
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)));
874 // sqlResults = new IPHPCompletionProposal[sqlList.size()];
875 // for (int i = 0; i < sqlList.size(); i++) {
876 // sqlResults[i] = (SQLProposal) sqlList.get(i);
878 // } catch (Exception /* NotConnectedException */ e) {
884 // return sqlResults;
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();
891 if (curr == PHPCodeReader.EOF)
894 return Scanner.isPHPIdentifierPart((char) curr);
897 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
898 int contextPosition = offset;
899 IDocument document = viewer.getDocument();
902 PHPCodeReader reader = new PHPCodeReader();
903 reader.configureBackwardReader(document, offset, true, true);
905 int nestingLevel = 0;
907 int curr = reader.read();
908 while (curr != PHPCodeReader.EOF) {
910 if (')' == (char) curr)
913 else if ('(' == (char) curr) {
916 if (nestingLevel < 0) {
917 int start = reader.getOffset();
918 if (looksLikeMethod(reader))
923 curr = reader.read();
925 } catch (IOException e) {
927 return contextPosition;
931 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
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()]);
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);
954 * Order the given proposals.
956 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
957 Arrays.sort(proposals, fComparator);
958 // int len = proposals.length;
962 // for (int i = 0; i < len; i++) {
963 // System.out.println(proposals[i].getDisplayString());
969 * (non-Javadoc) Method declared on IContentAssistProcessor
971 public char[] getCompletionProposalAutoActivationCharacters() {
972 return fProposalAutoActivationSet;
973 // return null; // new char[] { '$' };
977 * (non-Javadoc) Method declared on IContentAssistProcessor
979 public char[] getContextInformationAutoActivationCharacters() {
984 * (non-Javadoc) Method declared on IContentAssistProcessor
986 public IContextInformationValidator getContextInformationValidator() {
987 if (fValidator == null)
988 fValidator = new JavaParameterListValidator();
993 * (non-Javadoc) Method declared on IContentAssistProcessor
995 public String getErrorMessage() {