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.sql.Connection;
14 import java.sql.DatabaseMetaData;
15 import java.sql.ResultSet;
16 import java.sql.SQLException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.HashSet;
20 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.ISourceRange;
27 import net.sourceforge.phpdt.core.ToolFactory;
28 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
29 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
30 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
31 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
32 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
33 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
34 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
35 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
36 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
37 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
38 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
39 import net.sourceforge.phpdt.internal.ui.text.template.LocalVariableProposal;
40 import net.sourceforge.phpdt.internal.ui.text.template.SQLProposal;
41 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
42 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
43 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
44 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
45 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
46 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
47 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
48 import net.sourceforge.phpeclipse.ui.overlaypages.Util;
50 import org.eclipse.core.resources.IFile;
51 import org.eclipse.core.resources.IProject;
52 import org.eclipse.jface.text.BadLocationException;
53 import org.eclipse.jface.text.IDocument;
54 import org.eclipse.jface.text.IRegion;
55 import org.eclipse.jface.text.ITextViewer;
56 import org.eclipse.jface.text.Region;
57 import org.eclipse.jface.text.TextPresentation;
58 import org.eclipse.jface.text.contentassist.ICompletionProposal;
59 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
60 import org.eclipse.jface.text.contentassist.IContextInformation;
61 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
62 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
63 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
64 import org.eclipse.jface.text.templates.TemplateContextType;
65 import org.eclipse.swt.graphics.Image;
66 import org.eclipse.swt.graphics.Point;
67 import org.eclipse.ui.IEditorPart;
68 import org.eclipse.ui.IFileEditorInput;
70 import com.quantum.model.Bookmark;
71 import com.quantum.model.BookmarkCollection;
72 import com.quantum.model.NotConnectedException;
73 import com.quantum.util.connection.ConnectionUtil;
76 * Example PHP completion processor.
78 public class PHPCompletionProcessor implements IContentAssistProcessor {
80 * Simple content assist tip closer. The tip is valid in a range of 5 characters around its popup location.
82 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
83 protected int fInstallOffset;
86 * @see IContextInformationValidator#isContextInformationValid(int)
88 public boolean isContextInformationValid(int offset) {
89 return Math.abs(fInstallOffset - offset) < 5;
93 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
95 public void install(IContextInformation info, ITextViewer viewer, int offset) {
96 fInstallOffset = offset;
100 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
102 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
107 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
108 private final IContextInformation fContextInformation;
110 private int fPosition;
112 public ContextInformationWrapper(IContextInformation contextInformation) {
113 fContextInformation = contextInformation;
117 * @see IContextInformation#getContextDisplayString()
119 public String getContextDisplayString() {
120 return fContextInformation.getContextDisplayString();
124 * @see IContextInformation#getImage()
126 public Image getImage() {
127 return fContextInformation.getImage();
131 * @see IContextInformation#getInformationDisplayString()
133 public String getInformationDisplayString() {
134 return fContextInformation.getInformationDisplayString();
138 * @see IContextInformationExtension#getContextInformationPosition()
140 public int getContextInformationPosition() {
144 public void setContextInformationPosition(int position) {
145 fPosition = position;
149 private class TableName {
157 * @return Returns the tableName.
159 public String getTableName() {
160 if (fTableName == null) {
161 return "<!--no-table-->";
168 * The tableName to set.
170 public void setTableName(String tableName) {
171 fTableName = tableName;
175 private char[] fProposalAutoActivationSet;
177 protected IContextInformationValidator fValidator = new Validator();
179 private TemplateEngine fTemplateEngine;
181 private PHPCompletionProposalComparator fComparator;
183 private int fNumberOfComputedResults = 0;
185 private IEditorPart fEditor;
187 protected IWorkingCopyManager fManager;
189 public PHPCompletionProcessor(IEditorPart editor) {
191 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
192 TemplateContextType contextType = PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType("php"); //$NON-NLS-1$
193 if (contextType != null)
194 fTemplateEngine = new TemplateEngine(contextType);
195 fComparator = new PHPCompletionProposalComparator();
199 * Tells this processor to order the proposals alphabetically.
202 * <code>true</code> if proposals should be ordered.
204 public void orderProposalsAlphabetically(boolean order) {
205 fComparator.setOrderAlphabetically(order);
209 * Sets this processor's set of characters triggering the activation of the completion proposal computation.
211 * @param activationSet
214 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
215 fProposalAutoActivationSet = activationSet;
219 * (non-Javadoc) Method declared on IContentAssistProcessor
221 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
222 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
223 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
226 private int getLastToken(ITextViewer viewer, int completionPosition, JavaContext context, TableName tableName) {
227 IDocument document = viewer.getDocument();
228 int start = context.getStart();
229 int end = context.getEnd();
231 int lastSignificantToken = ITerminalSymbols.TokenNameEOF;
233 // begin search 2 lines behind of this
238 ch = document.getChar(j);
244 ch = document.getChar(j);
251 // scan the line for the dereferencing operator '->'
252 startText = document.get(j, start - j);
254 System.out.println(startText);
256 int token = ITerminalSymbols.TokenNameEOF;
257 // token = getLastSQLToken(startText);
258 tableName.setTableName(getLastSQLTableName(startText));
259 Scanner scanner = ToolFactory.createScanner(false, false, false);
260 scanner.setSource(startText.toCharArray());
261 scanner.setPHPMode(true);
262 int beforeLastToken = ITerminalSymbols.TokenNameEOF;
263 int lastToken = ITerminalSymbols.TokenNameEOF;
266 token = scanner.getNextToken();
268 while (token != ITerminalSymbols.TokenNameERROR && token != ITerminalSymbols.TokenNameEOF) {
269 beforeLastToken = lastToken;
270 if (lastToken == ITerminalSymbols.TokenNameVariable) {
271 ident = scanner.getCurrentTokenSource();
272 if (ident.length == 5 && ident[0] == '$' && ident[1] == 't' && ident[2] == 'h' && ident[3] == 'i' && ident[4] == 's') {
273 beforeLastToken = ITerminalSymbols.TokenNamethis_PHP_COMPLETION;
277 // System.out.println(scanner.toStringAction(lastToken));
278 token = scanner.getNextToken();
280 } catch (InvalidInputException e1) {
283 case ITerminalSymbols.TokenNameMINUS_GREATER:
284 // dereferencing operator '->' found
285 lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
286 if (beforeLastToken == ITerminalSymbols.TokenNameVariable) {
287 lastSignificantToken = ITerminalSymbols.TokenNameVariable;
290 case ITerminalSymbols.TokenNamenew:
291 lastSignificantToken = ITerminalSymbols.TokenNamenew;
295 } catch (BadLocationException e) {
297 return lastSignificantToken;
300 String getSQLTableName(String sqlText, int start) {
301 int tableNameStart = -1;
302 int currentCharacterPosition = start + 1;
306 ch = sqlText.charAt(currentCharacterPosition++);
307 if (tableNameStart == -1 && Character.isJavaIdentifierStart(ch)) {
308 tableNameStart = currentCharacterPosition - 1;
310 if (!Character.isJavaIdentifierPart(ch)) {
311 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
315 } catch (IndexOutOfBoundsException e) {
316 if (tableNameStart >= 0) {
317 return sqlText.substring(tableNameStart, currentCharacterPosition - 1);
323 private String getLastSQLTableName(String startText) {
325 // scan for sql identifiers
327 int currentSQLPosition = startText.length();
330 boolean whiteSpace = true;
333 ch = startText.charAt(--currentSQLPosition);
334 if (ch >= 'A' && ch <= 'Z') {
336 identEnd = currentSQLPosition + 1;
338 } else if (ch >= 'a' && ch <= 'z') {
340 identEnd = currentSQLPosition + 1;
342 } else if (identEnd >= 0) {
343 ident = startText.substring(currentSQLPosition + 1, identEnd);
344 // select -- from -- where --
345 // update -- set -- where --
346 // insert into -- ( -- ) values ( -- )
347 if (ident.length() >= 4 && ident.length() <= 6) {
348 ident = ident.toLowerCase();
349 switch (ident.length()) {
351 // if (ident.equals("set")) {
352 // // System.out.println("set");
353 // token = ITerminalSymbols.TokenNameSQLset;
358 if (ident.equals("from")) {
359 // System.out.println("from");
360 token = ITerminalSymbols.TokenNameSQLfrom;
361 return getSQLTableName(startText, identEnd);
362 } else if (ident.equals("into")) {
363 // System.out.println("into");
364 token = ITerminalSymbols.TokenNameSQLinto;
365 return getSQLTableName(startText, identEnd);
369 // if (ident.equals("where")) {
370 // // System.out.println("where");
371 // token = ITerminalSymbols.TokenNameSQLwhere;
376 // if (ident.equals("select")) {
377 // // System.out.println("select");
378 // token = ITerminalSymbols.TokenNameSQLselect;
380 // } else if (ident.equals("insert")) {
381 // // System.out.println("insert");
382 // token = ITerminalSymbols.TokenNameSQLinsert;
385 if (ident.equals("update")) {
386 // System.out.println("update");
387 token = ITerminalSymbols.TokenNameSQLupdate;
388 return getSQLTableName(startText, identEnd);
390 // else if (ident.equals("values")) {
391 // // System.out.println("values");
392 // token = ITerminalSymbols.TokenNameSQLvalues;
400 } else if (Character.isWhitespace(ch)) {
405 } catch (IndexOutOfBoundsException e) {
407 return "<!--no-table-->";
411 * Detect the last significant SQL token in the text before the completion
415 private int getLastSQLToken(String startText) {
417 // scan for sql identifiers
419 int currentSQLPosition = startText.length();
422 boolean whiteSpace = true;
425 ch = startText.charAt(--currentSQLPosition);
426 if (ch >= 'A' && ch <= 'Z') {
428 identEnd = currentSQLPosition + 1;
430 } else if (ch >= 'a' && ch <= 'z') {
432 identEnd = currentSQLPosition + 1;
434 } else if (identEnd >= 0) {
435 ident = startText.substring(currentSQLPosition + 1, identEnd);
436 // select -- from -- where --
437 // update -- set -- where --
438 // insert into -- ( -- ) values ( -- )
439 if (ident.length() >= 3 && ident.length() <= 6) {
440 ident = ident.toLowerCase();
441 switch (ident.length()) {
443 if (ident.equals("set")) {
444 // System.out.println("set");
445 token = ITerminalSymbols.TokenNameSQLset;
450 if (ident.equals("from")) {
451 // System.out.println("from");
452 token = ITerminalSymbols.TokenNameSQLfrom;
455 } else if (ident.equals("into")) {
456 // System.out.println("into");
457 token = ITerminalSymbols.TokenNameSQLinto;
462 if (ident.equals("where")) {
463 // System.out.println("where");
464 token = ITerminalSymbols.TokenNameSQLwhere;
469 if (ident.equals("select")) {
470 // System.out.println("select");
471 token = ITerminalSymbols.TokenNameSQLselect;
473 } else if (ident.equals("insert")) {
474 // System.out.println("insert");
475 token = ITerminalSymbols.TokenNameSQLinsert;
477 } else if (ident.equals("update")) {
478 // System.out.println("update");
479 token = ITerminalSymbols.TokenNameSQLupdate;
481 } else if (ident.equals("values")) {
482 // System.out.println("values");
483 token = ITerminalSymbols.TokenNameSQLvalues;
491 } else if (Character.isWhitespace(ch)) {
496 } catch (IndexOutOfBoundsException e) {
498 return ITerminalSymbols.TokenNameEOF;
501 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
502 ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
503 IDocument document = viewer.getDocument();
504 Object[] identifiers = null;
506 IProject project = null;
508 PHPEditor editor = null;
509 if (fEditor != null && (fEditor instanceof PHPEditor)) {
510 editor = (PHPEditor) fEditor;
511 file = ((IFileEditorInput) editor.getEditorInput()).getFile();
512 project = file.getProject();
516 Point selection = viewer.getSelectedRange();
518 // remember selected text
519 String selectedText = null;
520 if (selection.y != 0) {
522 selectedText = document.get(selection.x, selection.y);
523 } catch (BadLocationException e) {
527 JavaContextType phpContextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
528 "php"); //$NON-NLS-1$
529 JavaContext context = (JavaContext) phpContextType.createContext(document, offset, selection.y, unit);
530 context.setVariable("selection", selectedText); //$NON-NLS-1$
531 String prefix = context.getKey();
532 boolean emptyPrefix = prefix == null || prefix.equals("");
533 IPHPCompletionProposal[] localVariableResults = new IPHPCompletionProposal[0];
534 if (!emptyPrefix && prefix.length() >= 1 && prefix.charAt(0) == '$') { // php Variable ?
535 HashSet localVariables = getLocalVariableProposals(viewer, project, context, prefix);
536 if (localVariables.size() > 0) {
537 localVariableResults = (IPHPCompletionProposal[]) localVariables.toArray(new IPHPCompletionProposal[localVariables.size()]);
541 TableName sqlTable = new TableName();
542 int lastSignificantToken = getLastToken(viewer, offset, context, sqlTable);
543 boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER)
544 || (lastSignificantToken == ITerminalSymbols.TokenNameVariable) || (lastSignificantToken == ITerminalSymbols.TokenNamenew)
545 || (lastSignificantToken == ITerminalSymbols.TokenNamethis_PHP_COMPLETION);
547 if (fTemplateEngine != null) {
548 IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
549 ICompletionProposal[] results;
551 fTemplateEngine.reset();
552 fTemplateEngine.complete(viewer, offset, unit);
553 templateResults = fTemplateEngine.getResults();
555 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
556 if ((!useClassMembers) && identifiers != null) {
557 IdentifierEngine identifierEngine;
558 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
559 "php"); //$NON-NLS-1$
560 if (contextType != null) {
561 identifierEngine = new IdentifierEngine(contextType);
562 identifierEngine.complete(viewer, offset, identifiers, unit);
563 identifierResults = identifierEngine.getResults();
566 // declarations stored in file project.index on project level
567 IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
568 if (project != null) {
569 DeclarationEngine declarationEngine;
570 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
571 "php"); //$NON-NLS-1$
572 if (contextType != null) {
573 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
574 SortedMap sortedMap = indexManager.getIdentifierMap();
575 declarationEngine = new DeclarationEngine(project, contextType, lastSignificantToken, file);
576 declarationEngine.complete(viewer, offset, sortedMap, unit);
577 declarationResults = declarationEngine.getResults();
580 // built in function names from phpsyntax.xml
581 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
582 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
583 if ((!useClassMembers) && syntaxbuffer != null) {
584 BuiltInEngine builtinEngine;
586 JavaContextType contextType = (JavaContextType) PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(
587 "php"); //$NON-NLS-1$
588 if (contextType != null) {
589 builtinEngine = new BuiltInEngine(contextType);
590 builtinEngine.complete(viewer, offset, syntaxbuffer, unit);
591 builtinResults = builtinEngine.getResults();
594 IPHPCompletionProposal[] sqlResults = new IPHPCompletionProposal[0];
595 if (project != null) {
596 sqlResults = getSQLProposals(viewer, project, context, prefix, sqlTable, sqlResults);
598 // concatenate the result arrays
599 IPHPCompletionProposal[] total;
600 total = new IPHPCompletionProposal[localVariableResults.length + templateResults.length + identifierResults.length
601 + builtinResults.length + declarationResults.length + sqlResults.length];
602 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
603 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
604 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
605 System.arraycopy(declarationResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length,
606 declarationResults.length);
607 System.arraycopy(sqlResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
608 + declarationResults.length, sqlResults.length);
609 System.arraycopy(localVariableResults, 0, total, templateResults.length + identifierResults.length + builtinResults.length
610 + declarationResults.length + sqlResults.length, localVariableResults.length);
612 fNumberOfComputedResults = (results == null ? 0 : results.length);
614 * Order here and not in result collector to make sure that the order applies to all proposals and not just those of the
617 return order(results);
619 return new IPHPCompletionProposal[0];
629 private HashSet getLocalVariableProposals(ITextViewer viewer, IProject project, JavaContext context, String prefix) {
630 HashSet localVariables = new HashSet();
632 IMethod method = (IMethod) context.findEnclosingElement(IJavaElement.METHOD);
633 int start = context.getStart();
634 int end = context.getEnd();
635 IRegion region = new Region(start, end - start);
637 boolean matchesVarName;
638 if (method != null) {
639 ISourceRange range = method.getSourceRange();
640 char[] source = method.getSource().toCharArray();
641 Scanner scanner = new Scanner();
642 scanner.setSource(source);
643 scanner.phpMode = true;
644 int token = Scanner.TokenNameWHITESPACE;
645 while ((token = scanner.getNextToken()) != Scanner.TokenNameEOF) {
646 if (token == Scanner.TokenNameVariable) {
647 varName = scanner.getCurrentTokenSource();
648 if (varName.length >= prefix.length()) {
649 matchesVarName = true;
650 for (int i = 0; i < prefix.length(); i++) {
651 if (prefix.charAt(i) != varName[i]) {
652 matchesVarName = false;
656 if (matchesVarName) {
657 LocalVariableProposal prop = new LocalVariableProposal(new String(varName), region, viewer);
658 if (varName.length == prefix.length()) {
659 prop.setRelevance(98);
661 localVariables.add(prop);
667 } catch (Throwable e) {
668 // ignore - Syntax exceptions could occur, if there are syntax errors !
670 return localVariables;
682 private IPHPCompletionProposal[] getSQLProposals(ITextViewer viewer, IProject project, JavaContext context, String prefix,
683 TableName sqlTable, IPHPCompletionProposal[] sqlResults) {
684 // Get The Database bookmark from the Quantum SQL plugin:
685 BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
686 if (sqlBookMarks != null) {
687 String bookmarkString = Util.getMiscProjectsPreferenceValue(project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
688 if (bookmarkString != null && !bookmarkString.equals("")) {
689 Bookmark bookmark = sqlBookMarks.find(bookmarkString);
690 ArrayList sqlList = new ArrayList();
691 if (bookmark != null && !bookmark.isConnected()) {
692 new ConnectionUtil().connect(bookmark, null);
694 if (bookmark != null && bookmark.isConnected()) {
696 Connection connection = bookmark.getConnection();
697 DatabaseMetaData metaData = connection.getMetaData();
699 if (metaData != null) {
700 int start = context.getStart();
701 int end = context.getEnd();
702 String foundSQLTableName = sqlTable.getTableName();
705 String prefixWithoutDollar = prefix;
706 boolean isDollarPrefix = false;
707 if (prefix.length() > 0 && prefix.charAt(0) == '$') {
708 prefixWithoutDollar = prefix.substring(1);
709 isDollarPrefix = true;
711 IRegion region = new Region(start, end - start);
713 if (!isDollarPrefix) {
714 set = metaData.getTables(null, null, prefixWithoutDollar + "%", null);
716 // String tempSchema = set.getString("TABLE_SCHEM");
717 // tempSchema = (tempSchema == null) ? "" :
718 // tempSchema.trim();
719 tableName = set.getString("TABLE_NAME");
720 tableName = (tableName == null) ? "" : tableName.trim();
721 if (tableName != null && tableName.length() > 0) {
722 sqlList.add(new SQLProposal(tableName, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
727 set = metaData.getColumns(null, null, "%", prefixWithoutDollar + "%");
728 SQLProposal sqlProposal;
730 columnName = set.getString("COLUMN_NAME");
731 columnName = (columnName == null) ? "" : columnName.trim();
732 tableName = set.getString("TABLE_NAME");
733 tableName = (tableName == null) ? "" : tableName.trim();
734 if (tableName != null && tableName.length() > 0 && columnName != null && columnName.length() > 0) {
735 if (isDollarPrefix) {
736 sqlProposal = new SQLProposal(tableName, "$" + columnName, context, region, viewer, PHPUiImages
737 .get(PHPUiImages.IMG_COLUMN));
739 sqlProposal = new SQLProposal(tableName, columnName, context, region, viewer, PHPUiImages
740 .get(PHPUiImages.IMG_COLUMN));
742 if (tableName.equals(foundSQLTableName)) {
743 sqlProposal.setRelevance(90);
744 } else if (tableName.indexOf(foundSQLTableName) >= 0) {
745 sqlProposal.setRelevance(75);
747 sqlList.add(sqlProposal);
751 sqlResults = new IPHPCompletionProposal[sqlList.size()];
752 for (int i = 0; i < sqlList.size(); i++) {
753 sqlResults[i] = (SQLProposal) sqlList.get(i);
756 } catch (NotConnectedException e) {
757 // ignore this - not mission critical
758 } catch (SQLException e) {
767 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
768 int contextPosition = offset;
769 IDocument document = viewer.getDocument();
772 // PHPCodeReader reader= new PHPCodeReader();
773 // reader.configureBackwardReader(document, offset, true, true);
775 // int nestingLevel= 0;
777 // int curr= reader.read();
778 // while (curr != PHPCodeReader.EOF) {
780 // if (')' == (char) curr)
783 // else if ('(' == (char) curr) {
786 // if (nestingLevel < 0) {
787 // int start= reader.getOffset();
788 // if (looksLikeMethod(reader))
793 // curr= reader.read();
795 // } catch (IOException e) {
797 return contextPosition;
801 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
803 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
804 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
805 List result = addContextInformations(viewer, contextInformationPosition);
806 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
809 private List addContextInformations(ITextViewer viewer, int offset) {
810 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
811 List result = new ArrayList();
812 for (int i = 0; i < proposals.length; i++) {
813 IContextInformation contextInformation = proposals[i].getContextInformation();
814 if (contextInformation != null) {
815 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
816 wrapper.setContextInformationPosition(offset);
824 * Order the given proposals.
826 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
827 Arrays.sort(proposals, fComparator);
832 * (non-Javadoc) Method declared on IContentAssistProcessor
834 public char[] getCompletionProposalAutoActivationCharacters() {
835 return fProposalAutoActivationSet;
836 // return null; // new char[] { '$' };
840 * (non-Javadoc) Method declared on IContentAssistProcessor
842 public char[] getContextInformationAutoActivationCharacters() {
843 return new char[] {};
847 * (non-Javadoc) Method declared on IContentAssistProcessor
849 public IContextInformationValidator getContextInformationValidator() {
854 * (non-Javadoc) Method declared on IContentAssistProcessor
856 public String getErrorMessage() {