* All Rights Reserved.
*/
package net.sourceforge.phpdt.internal.ui.text.template;
-
import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
-
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
//import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
-
/**
* A PHP identifier proposal.
*/
-public class IdentifierProposal extends AbstractProposal { //implements IPHPCompletionProposal {
+public class IdentifierProposal extends AbstractProposal { //implements
+ // IPHPCompletionProposal
+ // {
private final TemplateContext fContext;
private final Image fImage_fun;
private final Image fImage_var;
-
//private TemplateBuffer fTemplateBuffer;
private String fOldText;
private final IRegion fRegion;
-// private IRegion fSelectedRegion; // initialized by apply()
-
+ // private IRegion fSelectedRegion; // initialized by apply()
private final String fTemplate;
-// private final ITextViewer fViewer;
-
+ // private final ITextViewer fViewer;
/**
* Creates a template proposal with a template and its context.
- * @param template the template
- * @param context the context in which the template was requested.
- * @param image the icon of the proposal.
+ *
+ * @param template
+ * the template
+ * @param context
+ * the context in which the template was requested.
+ * @param image
+ * the icon of the proposal.
*/
- public IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
+ public IdentifierProposal(String template, TemplateContext context,
+ IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
super(viewer);
-
fTemplate = template;
fContext = context;
-// fViewer = viewer;
- fImage_fun= image_fun;
- fImage_var= image_var;
+ // fViewer = viewer;
+ fImage_fun = image_fun;
+ fImage_var = image_var;
fRegion = region;
}
-
-
/*
* @see ICompletionProposal#apply(IDocument)
*/
try {
// if (fTemplateBuffer == null)
// fTemplateBuffer= fContext.evaluate(fTemplate);
-
int start = fRegion.getOffset();
int end = fRegion.getOffset() + fRegion.getLength();
-
// insert template string
- // String templateString = fTemplate; // fTemplateBuffer.getString();
+ // String templateString = fTemplate; // fTemplateBuffer.getString();
document.replace(start, end - start, fTemplate);
-
// translate positions
LinkedPositionManager manager = new LinkedPositionManager(document);
// TemplatePosition[] variables= fTemplateBuffer.getVariables();
// for (int j= 0; j != offsets.length; j++)
// manager.addPosition(offsets[j] + start, length);
// }
-
LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
- editor.setFinalCaretOffset(fTemplate.length()+start);
- // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
+ editor.setFinalCaretOffset(fTemplate.length() + start);
+ // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
editor.enter();
-
fSelectedRegion = editor.getSelectedRegion();
-
} catch (BadLocationException e) {
PHPeclipsePlugin.log(e);
openErrorDialog(e);
-
}
// catch (CoreException e) {
// handleException(e);
- // }
+ // }
}
-
/*
* @see ICompletionProposal#getAdditionalProposalInfo()
*/
// try {
// if (fTemplateBuffer == null)
// fTemplateBuffer= fContext.evaluate(fTemplate);
-
return textToHTML(fTemplate); // fTemplateBuffer.getString());
-
// } catch (CoreException e) {
- // handleException(e);
+ // handleException(e);
// return null;
// }
}
-
/*
* @see ICompletionProposal#getContextInformation()
*/
public IContextInformation getContextInformation() {
return null;
}
-
/*
* @see ICompletionProposal#getDisplayString()
*/
public String getDisplayString() {
- return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
- // return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
+ return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$
}
-
/*
* @see ICompletionProposal#getImage()
*/
public Image getImage() {
- if (fTemplate.charAt(0)=='$') {
+ if (fTemplate.charAt(0) == '$') {
return fImage_var;
}
- return fImage_fun;
+ return fImage_fun;
}
-
/*
* @see IJavaCompletionProposal#getRelevance()
*/
public int getRelevance() {
-
if (fContext instanceof PHPUnitContext) {
PHPUnitContext context = (PHPUnitContext) fContext;
switch (context.getCharacterBeforeStart()) {
case '\r' :
case '\n' :
case '\t' :
- return 90;
-
+ return 50;
default :
return 0;
}
} else {
- return 90;
+ return 50;
}
}
-
}
\ No newline at end of file
--- /dev/null
+/*
+ * (c) Copyright IBM Corp. 2000, 2001.
+ * All Rights Reserved.
+ */
+package net.sourceforge.phpdt.internal.ui.text.template;
+import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
+import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
+import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
+import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.swt.graphics.Image;
+/**
+ * A PHP identifier proposal.
+ */
+public class SQLProposal extends AbstractProposal { //implements
+ // IPHPCompletionProposal
+ // {
+ private final TemplateContext fContext;
+ private final Image fImage_var;
+ private final IRegion fRegion;
+ private final String fColumnName;
+ private final String fTableName;
+
+ private int fRelevance;
+ /**
+ * Creates a template proposal with a template and its context.
+ *
+ * @param template
+ * the template
+ * @param context
+ * the context in which the template was requested.
+ * @param image
+ * the icon of the proposal.
+ */
+ public SQLProposal(String tableName, TemplateContext context, IRegion region,
+ ITextViewer viewer, Image image_var) {
+ super(viewer);
+ fTableName = tableName;
+ fColumnName = null;
+ fContext = context;
+ fImage_var = image_var;
+ fRegion = region;
+ fRelevance = 0;
+ }
+
+ public SQLProposal(String tableName, String columnName, TemplateContext context, IRegion region,
+ ITextViewer viewer, Image image_var) {
+ super(viewer);
+ fTableName = tableName;
+ fColumnName = columnName;
+ fContext = context;
+ fImage_var = image_var;
+ fRegion = region;
+ fRelevance = 0;
+ }
+ /*
+ * @see ICompletionProposal#apply(IDocument)
+ */
+ public void apply(IDocument document) {
+ try {
+ // if (fTemplateBuffer == null)
+ // fTemplateBuffer= fContext.evaluate(fTemplate);
+ int start = fRegion.getOffset();
+ int end = fRegion.getOffset() + fRegion.getLength();
+ String resultString = fTableName;
+ if (fColumnName!=null) {
+ resultString = fColumnName;
+ }
+ // insert template string
+ // String templateString = fTemplate; // fTemplateBuffer.getString();
+ document.replace(start, end - start, resultString);
+ // translate positions
+ LinkedPositionManager manager = new LinkedPositionManager(document);
+ // TemplatePosition[] variables= fTemplateBuffer.getVariables();
+ // for (int i= 0; i != variables.length; i++) {
+ // TemplatePosition variable= variables[i];
+ //
+ // if (variable.isResolved())
+ // continue;
+ //
+ // int[] offsets= variable.getOffsets();
+ // int length= variable.getLength();
+ //
+ // for (int j= 0; j != offsets.length; j++)
+ // manager.addPosition(offsets[j] + start, length);
+ // }
+ LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
+ editor.setFinalCaretOffset(resultString.length() + start);
+ // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
+ editor.enter();
+ fSelectedRegion = editor.getSelectedRegion();
+ } catch (BadLocationException e) {
+ PHPeclipsePlugin.log(e);
+ openErrorDialog(e);
+ }
+ // catch (CoreException e) {
+ // handleException(e);
+ // }
+ }
+ /*
+ * @see ICompletionProposal#getAdditionalProposalInfo()
+ */
+ public String getAdditionalProposalInfo() {
+ if (fColumnName==null) {
+ return textToHTML(fTableName);
+ }
+ return fColumnName+" (Table: "+fTableName+")";
+ }
+
+ /*
+ * @see ICompletionProposal#getContextInformation()
+ */
+ public IContextInformation getContextInformation() {
+ return null;
+ }
+ /*
+ * @see ICompletionProposal#getDisplayString()
+ */
+ public String getDisplayString() {
+ if (fColumnName==null) {
+ return fTableName;
+ }
+ return fColumnName+" (Table: "+fTableName+")"; // $NON-NLS-1$
+ }
+ /*
+ * @see ICompletionProposal#getImage()
+ */
+ public Image getImage() {
+ return fImage_var;
+ }
+ /*
+ * @see IJavaCompletionProposal#getRelevance()
+ */
+ public int getRelevance() {
+ return fRelevance;
+ }
+ /**
+ * @param relevance The relevance to set.
+ */
+ public void setRelevance(int relevance) {
+ fRelevance = relevance;
+ }
+}
\ No newline at end of file
import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
-import net.sourceforge.phpdt.internal.ui.text.template.IdentifierProposal;
+import net.sourceforge.phpdt.internal.ui.text.template.SQLProposal;
import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
import net.sourceforge.phpeclipse.IPreferenceConstants;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
+import net.sourceforge.phpeclipse.overlaypages.Util;
import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
-import net.sourceforge.phpeclipse.overlaypages.Util;
import com.quantum.model.Bookmark;
import com.quantum.model.BookmarkCollection;
import com.quantum.model.NotConnectedException;
fPosition = position;
}
};
+ private class TableName {
+ String fTableName;
+ TableName() {
+ fTableName = null;
+ }
+ /**
+ * @return Returns the tableName.
+ */
+ public String getTableName() {
+ if (fTableName==null) {
+ return "<!--no-table-->";
+ }
+ return fTableName;
+ }
+ /**
+ * @param tableName The tableName to set.
+ */
+ public void setTableName(String tableName) {
+ fTableName = tableName;
+ }
+ }
private char[] fProposalAutoActivationSet;
protected IContextInformationValidator fValidator = new Validator();
private TemplateEngine fTemplateEngine;
contextInformationPosition);
}
private int getLastToken(ITextViewer viewer, int completionPosition,
- PHPUnitContext context) {
+ PHPUnitContext context, TableName tableName) {
IDocument document = viewer.getDocument();
int start = context.getStart();
int end = context.getEnd();
System.out.println(startText);
}
int token = ITerminalSymbols.TokenNameEOF;
-
- token = getLastSQLToken(startText);
-
+// token = getLastSQLToken(startText);
+ tableName.setTableName(getLastSQLTableName(startText));
Scanner scanner = ToolFactory.createScanner(false, false, false);
scanner.setSource(startText.toCharArray());
scanner.setPHPMode(true);
-
int beforeLastToken = ITerminalSymbols.TokenNameEOF;
int lastToken = ITerminalSymbols.TokenNameEOF;
try {
return lastSignificantToken;
}
+ String getSQLTableName(String sqlText, int start) {
+ int tableNameStart = -1;
+ int currentCharacterPosition = start+1;
+ char ch;
+ try {
+ while(true) {
+ ch = sqlText.charAt(currentCharacterPosition++);
+ if (tableNameStart==-1 && Character.isJavaIdentifierStart(ch)) {
+ tableNameStart = currentCharacterPosition-1;
+ } else {
+ if (!Character.isJavaIdentifierPart(ch)) {
+ return sqlText.substring(tableNameStart, currentCharacterPosition-1);
+ }
+ }
+ }
+ } catch (IndexOutOfBoundsException e) {
+ if (tableNameStart>=0) {
+ return sqlText.substring(tableNameStart, currentCharacterPosition-1);
+ }
+ }
+ return "";
+ }
+ private String getLastSQLTableName(String startText) {
+ int token;
+ // scan for sql identifiers
+ char ch = ' ';
+ int currentSQLPosition = startText.length();
+ int identEnd = -1;
+ String ident = null;
+ boolean whiteSpace = true;
+ try {
+ while (true) {
+ ch = startText.charAt(--currentSQLPosition);
+ if (ch >= 'A' && ch <= 'Z') {
+ if (identEnd < 0) {
+ identEnd = currentSQLPosition + 1;
+ }
+ } else if (ch >= 'a' && ch <= 'z') {
+ if (identEnd < 0) {
+ identEnd = currentSQLPosition + 1;
+ }
+ } else if (identEnd >= 0) {
+ ident = startText.substring(currentSQLPosition + 1, identEnd);
+ // select -- from -- where --
+ // update -- set -- where --
+ // insert into -- ( -- ) values ( -- )
+ if (ident.length() >= 4 && ident.length() <= 6) {
+ ident = ident.toLowerCase();
+ switch (ident.length()) {
+// case 3 :
+// if (ident.equals("set")) {
+// // System.out.println("set");
+// token = ITerminalSymbols.TokenNameSQLset;
+// return token;
+// }
+// break;
+ case 4 :
+ if (ident.equals("from")) {
+ // System.out.println("from");
+ token = ITerminalSymbols.TokenNameSQLfrom;
+ return getSQLTableName(startText, identEnd);
+ } else if (ident.equals("into")) {
+ // System.out.println("into");
+ token = ITerminalSymbols.TokenNameSQLinto;
+ return getSQLTableName(startText, identEnd);
+ }
+ break;
+// case 5 :
+// if (ident.equals("where")) {
+// // System.out.println("where");
+// token = ITerminalSymbols.TokenNameSQLwhere;
+// return token;
+// }
+// break;
+ case 6 :
+// if (ident.equals("select")) {
+// // System.out.println("select");
+// token = ITerminalSymbols.TokenNameSQLselect;
+// return token;
+// } else if (ident.equals("insert")) {
+// // System.out.println("insert");
+// token = ITerminalSymbols.TokenNameSQLinsert;
+// return token;
+// } else
+ if (ident.equals("update")) {
+ // System.out.println("update");
+ token = ITerminalSymbols.TokenNameSQLupdate;
+ return getSQLTableName(startText, identEnd);
+ }
+// else if (ident.equals("values")) {
+// // System.out.println("values");
+// token = ITerminalSymbols.TokenNameSQLvalues;
+// return token;
+// }
+ break;
+ }
+ }
+ whiteSpace = false;
+ identEnd = -1;
+ } else if (Character.isWhitespace(ch)) {
+ } else {
+ whiteSpace = false;
+ }
+ }
+ } catch (IndexOutOfBoundsException e) {
+ }
+ return "<!--no-table-->";
+ }
/**
* Detect the last significant SQL token in the text before the completion
*
switch (ident.length()) {
case 3 :
if (ident.equals("set")) {
-// System.out.println("set");
+ // System.out.println("set");
token = ITerminalSymbols.TokenNameSQLset;
return token;
}
break;
case 4 :
if (ident.equals("from")) {
-// System.out.println("from");
+ // System.out.println("from");
token = ITerminalSymbols.TokenNameSQLfrom;
+ //getSQLTableName();
return token;
} else if (ident.equals("into")) {
-// System.out.println("into");
+ // System.out.println("into");
token = ITerminalSymbols.TokenNameSQLinto;
return token;
}
break;
case 5 :
if (ident.equals("where")) {
-// System.out.println("where");
+ // System.out.println("where");
token = ITerminalSymbols.TokenNameSQLwhere;
return token;
}
break;
case 6 :
if (ident.equals("select")) {
-// System.out.println("select");
+ // System.out.println("select");
token = ITerminalSymbols.TokenNameSQLselect;
return token;
} else if (ident.equals("insert")) {
-// System.out.println("insert");
+ // System.out.println("insert");
token = ITerminalSymbols.TokenNameSQLinsert;
return token;
} else if (ident.equals("update")) {
-// System.out.println("update");
+ // System.out.println("update");
token = ITerminalSymbols.TokenNameSQLupdate;
return token;
} else if (ident.equals("values")) {
-// System.out.println("values");
+ // System.out.println("values");
token = ITerminalSymbols.TokenNameSQLvalues;
return token;
}
}
return ITerminalSymbols.TokenNameEOF;
}
-
private ICompletionProposal[] internalComputeCompletionProposals(
ITextViewer viewer, int offset, int contextOffset) {
IDocument document = viewer.getDocument();
document, offset, 0);
PHPUnitContext context = (PHPUnitContext) phpContextType.createContext();
String prefix = context.getKey();
- int lastSignificantToken = getLastToken(viewer, offset, context);
+ TableName sqlTable = new TableName();
+ int lastSignificantToken = getLastToken(viewer, offset, context, sqlTable);
boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER)
|| (lastSignificantToken == ITerminalSymbols.TokenNameVariable)
|| (lastSignificantToken == ITerminalSymbols.TokenNamenew);
IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
if (bookmarkString != null && !bookmarkString.equals("")) {
Bookmark bookmark = sqlBookMarks.find(bookmarkString);
- ArrayList list = new ArrayList();
+ ArrayList sqlList = new ArrayList();
if (bookmark != null && !bookmark.isConnected()) {
new ConnectionUtil().connect(bookmark, null);
}
try {
Connection connection = bookmark.getConnection();
DatabaseMetaData metaData = connection.getMetaData();
+
if (metaData != null) {
int start = context.getStart();
int end = context.getEnd();
+ String foundSQLTableName = sqlTable.getTableName();
+ String tableName;
+ String columnName;
+ String prefixWithoutDollar = prefix;
+ boolean isDollarPrefix = false;
+ if (prefix.length() > 0 && prefix.charAt(0) == '$') {
+ prefixWithoutDollar = prefix.substring(1);
+ isDollarPrefix = true;
+ }
IRegion region = new Region(start, end - start);
- ResultSet set = metaData.getTables(null, null, prefix + "%",
- null);
- while (set.next()) {
- // String tempSchema = set.getString("TABLE_SCHEM");
- // tempSchema = (tempSchema == null) ? "" :
- // tempSchema.trim();
- String tableName = set.getString("TABLE_NAME");
- tableName = (tableName == null) ? "" : tableName.trim();
- if (tableName != null && tableName.length() > 0) {
- list.add(tableName);
+ ResultSet set;
+ if (!isDollarPrefix) {
+ set = metaData.getTables(null, null, prefixWithoutDollar
+ + "%", null);
+ while (set.next()) {
+ // String tempSchema = set.getString("TABLE_SCHEM");
+ // tempSchema = (tempSchema == null) ? "" :
+ // tempSchema.trim();
+ tableName = set.getString("TABLE_NAME");
+ tableName = (tableName == null) ? "" : tableName.trim();
+ if (tableName != null && tableName.length() > 0) {
+ sqlList.add(new SQLProposal(tableName, context, region,
+ viewer, PHPUiImages.get(PHPUiImages.IMG_TABLE)));
+ }
}
+ set.close();
}
- set.close();
- set = metaData.getColumns(null, null, "%", prefix + "%");
+ set = metaData.getColumns(null, null, "%",
+ prefixWithoutDollar + "%");
+ SQLProposal sqlProposal;
while (set.next()) {
- String tableName = set.getString("COLUMN_NAME");
- tableName = (tableName == null) ? "" : tableName.trim();
- if (tableName != null && tableName.length() > 0) {
- list.add(tableName);
- }
+ columnName = set.getString("COLUMN_NAME");
+ columnName = (columnName == null) ? "" : columnName.trim();
tableName = set.getString("TABLE_NAME");
tableName = (tableName == null) ? "" : tableName.trim();
- if (tableName != null && tableName.length() > 0) {
- list.add(tableName);
+ if (tableName != null && tableName.length() > 0
+ && columnName != null && columnName.length() > 0) {
+ if (isDollarPrefix) {
+ sqlProposal = new SQLProposal(tableName,
+ "$" + columnName, context, region, viewer,
+ PHPUiImages.get(PHPUiImages.IMG_COLUMN));
+ } else {
+ sqlProposal = new SQLProposal(tableName, columnName,
+ context, region, viewer, PHPUiImages
+ .get(PHPUiImages.IMG_COLUMN));
+ }
+ if (tableName.equals(foundSQLTableName)) {
+ sqlProposal.setRelevance(90);
+ } else if (tableName.indexOf(foundSQLTableName)>=0) {
+ sqlProposal.setRelevance(75);
+ }
+ sqlList.add(sqlProposal);
}
}
set.close();
- sqlResults = new IPHPCompletionProposal[list.size()];
- for (int i = 0; i < list.size(); i++) {
- sqlResults[i] = new IdentifierProposal(
- (String) list.get(i), context, region, viewer,
- PHPUiImages.get(PHPUiImages.IMG_FUN), PHPUiImages
- .get(PHPUiImages.IMG_VAR));
+ sqlResults = new IPHPCompletionProposal[sqlList.size()];
+ for (int i = 0; i < sqlList.size(); i++) {
+ sqlResults[i] = (SQLProposal) sqlList.get(i);
}
}
} catch (NotConnectedException e) {