From: khartlage Date: Thu, 18 Sep 2003 20:58:55 +0000 (+0000) Subject: Improved CodeCompletion with information from the project.index file. X-Git-Url: http://git.phpeclipse.com?hp=02a7713c3b138a5e0817714f41ff3e6fd5f1e2a8 Improved CodeCompletion with information from the project.index file. --- diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java new file mode 100644 index 0000000..56e309e --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java @@ -0,0 +1,113 @@ +/* + * (c) Copyright IBM Corp. 2000, 2001. + * All Rights Reserved. + */ +package net.sourceforge.phpdt.internal.ui.text.template; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.SortedMap; + +import net.sourceforge.phpdt.internal.corext.template.ContextType; +import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType; +import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext; +import net.sourceforge.phpdt.internal.ui.PHPUiImages; +import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal; + +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.Region; +import org.eclipse.swt.graphics.Point; +//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager; + +public class DeclarationEngine { + + /** The context type. */ + private ContextType fContextType; + /** The result proposals. */ + private ArrayList fProposals = new ArrayList(); + + /** + * Creates the template engine for a particular context type. + * See TemplateContext for supported context types. + */ + public DeclarationEngine(ContextType contextType) { + // Assert.isNotNull(contextType); + fContextType = contextType; + } + + /** + * Empties the collector. + * + * @param viewer the text viewer + * @param unit the compilation unit (may be null) + */ + public void reset() { + fProposals.clear(); + } + + /** + * Returns the array of matching templates. + */ + public IPHPCompletionProposal[] getResults() { + return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]); + } + + /** + * Inspects the context of the compilation unit around completionPosition + * and feeds the collector with proposals. + * @param viewer the text viewer + * @param completionPosition the context position in the document of the text viewer + * @param compilationUnit the compilation unit (may be null) + */ + public void complete(ITextViewer viewer, int completionPosition, SortedMap map) + //,ICompilationUnit compilationUnit) + //hrows JavaModelException + { + IDocument document = viewer.getDocument(); + + if (!(fContextType instanceof CompilationUnitContextType)) + return; + + Point selection = viewer.getSelectedRange(); + // remember selected text + String selectedText = null; + if (selection.y != 0) { + try { + selectedText = document.get(selection.x, selection.y); + } catch (BadLocationException e) { + } + } + + ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit); + + PHPUnitContext context = (PHPUnitContext) fContextType.createContext(); + int start = context.getStart(); + int end = context.getEnd(); + String prefix = context.getKey(); + IRegion region = new Region(start, end - start); + + String identifier = null; + + SortedMap subMap = map.subMap(prefix,prefix+'\255'); + Iterator iter = subMap.keySet().iterator(); + + while (iter.hasNext()) { + identifier = (String) iter.next(); + if (context.canEvaluate(identifier)) { + fProposals.add( + new DeclarationProposal( + identifier, + context, + region, + viewer, + PHPUiImages.get(PHPUiImages.IMG_FUN), + PHPUiImages.get(PHPUiImages.IMG_VAR))); + } + } + + } + +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationProposal.java new file mode 100644 index 0000000..c448d44 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationProposal.java @@ -0,0 +1,246 @@ +/* + * (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.TemplateMessages; +import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext; +import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal; +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.core.runtime.CoreException; +import org.eclipse.jface.dialogs.MessageDialog; +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; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Shell; +//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 DeclarationProposal implements IPHPCompletionProposal { + + private final String fTemplate; + private final TemplateContext fContext; + private final ITextViewer fViewer; + private final Image fImage_fun; + private final Image fImage_var; + private final IRegion fRegion; + + //private TemplateBuffer fTemplateBuffer; + private String fOldText; + private IRegion fSelectedRegion; // initialized by apply() + + /** + * 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 DeclarationProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) { + // Assert.isNotNull(template); + // Assert.isNotNull(context); + // Assert.isNotNull(region); + // Assert.isNotNull(viewer); + + fTemplate = template; + fContext = context; + fViewer = viewer; + fImage_fun= image_fun; + fImage_var= image_var; + fRegion = region; + } + + /* + * @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(); + + // insert template string + // String templateString = fTemplate; // fTemplateBuffer.getString(); + document.replace(start, end - start, fTemplate); + + // 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(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); + // } + } + +// private static int getCaretOffset(TemplateBuffer buffer) { +// TemplatePosition[] variables = buffer.getVariables(); +// for (int i = 0; i != variables.length; i++) { +// TemplatePosition variable = variables[i]; +// +// if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$ +// return variable.getOffsets()[0]; +// } +// +// return buffer.getString().length(); +// } + + /* + * @see ICompletionProposal#getSelection(IDocument) + */ + public Point getSelection(IDocument document) { + return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); + // return null; + } + + /* + * @see ICompletionProposal#getAdditionalProposalInfo() + */ + public String getAdditionalProposalInfo() { + // try { + // if (fTemplateBuffer == null) + // fTemplateBuffer= fContext.evaluate(fTemplate); + + return textToHTML(fTemplate); // fTemplateBuffer.getString()); + + // } catch (CoreException e) { + // handleException(e); + // 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$ + } + + /* + * @see ICompletionProposal#getImage() + */ + public Image getImage() { + if (fTemplate.charAt(0)=='$') { + return fImage_var; + } + return fImage_fun; + } + + /* + * @see ICompletionProposal#getContextInformation() + */ + public IContextInformation getContextInformation() { + return null; + } + + private static String textToHTML(String string) { + StringBuffer buffer = new StringBuffer(string.length()); + buffer.append("
"); //$NON-NLS-1$
+
+    for (int i = 0; i != string.length(); i++) {
+      char ch = string.charAt(i);
+
+      switch (ch) {
+        case '&' :
+          buffer.append("&"); //$NON-NLS-1$
+          break;
+
+        case '<' :
+          buffer.append("<"); //$NON-NLS-1$
+          break;
+
+        case '>' :
+          buffer.append(">"); //$NON-NLS-1$
+          break;
+
+        case '\t' :
+          buffer.append("    "); //$NON-NLS-1$
+          break;
+
+        case '\n' :
+          buffer.append("
"); //$NON-NLS-1$ + break; + + default : + buffer.append(ch); + break; + } + } + + buffer.append("
"); //$NON-NLS-1$ + return buffer.toString(); + } + + private void openErrorDialog(BadLocationException e) { + Shell shell = fViewer.getTextWidget().getShell(); + MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$ + } + + private void handleException(CoreException e) { + Shell shell = fViewer.getTextWidget().getShell(); + PHPeclipsePlugin.log(e); + // ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$ + } + + /* + * @see IJavaCompletionProposal#getRelevance() + */ + public int getRelevance() { + + if (fContext instanceof PHPUnitContext) { + PHPUnitContext context = (PHPUnitContext) fContext; + switch (context.getCharacterBeforeStart()) { + // high relevance after whitespace + case ' ' : + case '\r' : + case '\n' : + case '\t' : + return 90; + + default : + return 0; + } + } else { + return 90; + } + } + +} \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierEngine.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierEngine.java index 0658df7..de2ce2e 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierEngine.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierEngine.java @@ -22,82 +22,85 @@ import org.eclipse.swt.graphics.Point; public class IdentifierEngine { - /** The context type. */ - private ContextType fContextType; - /** The result proposals. */ - private ArrayList fProposals= new ArrayList(); + /** The context type. */ + private ContextType fContextType; + /** The result proposals. */ + private ArrayList fProposals = new ArrayList(); - /** - * Creates the template engine for a particular context type. - * See TemplateContext for supported context types. - */ - public IdentifierEngine(ContextType contextType) { - // Assert.isNotNull(contextType); - fContextType= contextType; - } + /** + * Creates the template engine for a particular context type. + * See TemplateContext for supported context types. + */ + public IdentifierEngine(ContextType contextType) { + // Assert.isNotNull(contextType); + fContextType = contextType; + } - /** - * Empties the collector. - * - * @param viewer the text viewer - * @param unit the compilation unit (may be null) - */ - public void reset() { - fProposals.clear(); - } + /** + * Empties the collector. + * + * @param viewer the text viewer + * @param unit the compilation unit (may be null) + */ + public void reset() { + fProposals.clear(); + } - /** - * Returns the array of matching templates. - */ - public IPHPCompletionProposal[] getResults() { - return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]); - } + /** + * Returns the array of matching templates. + */ + public IPHPCompletionProposal[] getResults() { + return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]); + } - /** - * Inspects the context of the compilation unit around completionPosition - * and feeds the collector with proposals. - * @param viewer the text viewer - * @param completionPosition the context position in the document of the text viewer - * @param compilationUnit the compilation unit (may be null) - */ - public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers) + /** + * Inspects the context of the compilation unit around completionPosition + * and feeds the collector with proposals. + * @param viewer the text viewer + * @param completionPosition the context position in the document of the text viewer + * @param compilationUnit the compilation unit (may be null) + */ + public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers) //,ICompilationUnit compilationUnit) - //hrows JavaModelException - { - IDocument document= viewer.getDocument(); - - // prohibit recursion -// if (LinkedPositionManager.hasActiveManager(document)) -// return; + //hrows JavaModelException + { + IDocument document = viewer.getDocument(); - if (!(fContextType instanceof CompilationUnitContextType)) - return; - - Point selection= viewer.getSelectedRange(); + if (!(fContextType instanceof CompilationUnitContextType)) + return; + + Point selection = viewer.getSelectedRange(); // remember selected text - String selectedText= null; + String selectedText = null; if (selection.y != 0) { try { - selectedText= document.get(selection.x, selection.y); - } catch (BadLocationException e) {} + selectedText = document.get(selection.x, selection.y); + } catch (BadLocationException e) { + } } - - ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit); - PHPUnitContext context= (PHPUnitContext) fContextType.createContext(); - int start= context.getStart(); - int end= context.getEnd(); - IRegion region= new Region(start, end - start); + ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit); -// Template[] templates= Templates.getInstance().getTemplates(); + PHPUnitContext context = (PHPUnitContext) fContextType.createContext(); + int start = context.getStart(); + int end = context.getEnd(); + IRegion region = new Region(start, end - start); + + // Template[] templates= Templates.getInstance().getTemplates(); String identifier = null; - for (int i= 0; i != identifiers.length; i++) { + for (int i = 0; i != identifiers.length; i++) { identifier = (String) identifiers[i]; - if (context.canEvaluate(identifier)) { - fProposals.add(new IdentifierProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_FUN), PHPUiImages.get(PHPUiImages.IMG_VAR))); + if (context.canEvaluate(identifier)) { + fProposals.add( + new IdentifierProposal( + identifier, + context, + region, + viewer, + PHPUiImages.get(PHPUiImages.IMG_FUN), + PHPUiImages.get(PHPUiImages.IMG_VAR))); } } - } + } } - diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java index 1b0be9e..1047779 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java @@ -8,10 +8,13 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.SortedMap; import java.util.StringTokenizer; +import java.util.TreeMap; import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; import net.sourceforge.phpdt.core.compiler.InvalidInputException; @@ -236,9 +239,22 @@ public class IdentifierIndexManager { } } + class StringComparator implements Comparator { + public int compare(Object o1, Object o2) { + String s1 = (String)o1; + String s2 = (String)o2; + return s1.compareTo(s2); + // return s1.toUpperCase().compareTo(s2.toUpperCase()); + } + public boolean equals(Object o) { + String s = (String)o; + return compare(this, o)==0; + } + } + private HashMap fFileMap; private String fFilename; - private HashMap fIndentifierMap; + private TreeMap fIndentifierMap; public IdentifierIndexManager(String filename) { fFilename = filename; @@ -405,7 +421,7 @@ public class IdentifierIndexManager { * */ public void initialize() { - fIndentifierMap = new HashMap(); + fIndentifierMap = new TreeMap(new StringComparator()); fFileMap = new HashMap(); } @@ -543,4 +559,14 @@ public class IdentifierIndexManager { e.printStackTrace(); } } + + /** + * @param fromKey + * @param toKey + * @return + */ + public SortedMap getIdentifierMap() { + return fIndentifierMap; + } + } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java index dee6cca..c4ce375 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java @@ -14,19 +14,24 @@ package net.sourceforge.phpeclipse.phpeditor.php; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.SortedMap; import net.sourceforge.phpdt.internal.corext.template.ContextType; import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry; import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal; import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator; 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.TemplateEngine; import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import net.sourceforge.phpeclipse.builder.IdentifierIndexManager; import net.sourceforge.phpeclipse.phpeditor.AbstractContentOutlinePage; import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage; import net.sourceforge.phpeclipse.phpeditor.PHPEditor; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.TextPresentation; @@ -38,6 +43,7 @@ import org.eclipse.jface.text.contentassist.IContextInformationPresenter; import org.eclipse.jface.text.contentassist.IContextInformationValidator; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IFileEditorInput; /** * Example PHP completion processor. @@ -306,6 +312,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) { IDocument document = viewer.getDocument(); Object[] identifiers = null; + IProject project = null; if (offset > 0) { PHPEditor editor = null; @@ -314,6 +321,8 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if (targetEditor != null && (targetEditor instanceof PHPEditor)) { editor = (PHPEditor) targetEditor; + IFile f = ((IFileEditorInput) editor.getEditorInput()).getFile(); + project = f.getProject(); outlinePage = editor.getfOutlinePage(); if (outlinePage instanceof PHPContentOutlinePage) { identifiers = ((PHPContentOutlinePage) outlinePage).getVariables(); @@ -352,6 +361,23 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { } } + IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0]; + if (project != null) { + DeclarationEngine identifierEngine; + String proposal; + + ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$ + if (contextType != null) { + IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project); + SortedMap sortedMap = indexManager.getIdentifierMap(); + + identifierEngine = new DeclarationEngine(contextType); + identifierEngine.complete(viewer, offset, sortedMap); + identifierResults = identifierEngine.getResults(); + } + } + + // built in function names from phpsyntax.xml IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0]; if (PHPFunctionNames.FUNCTION_NAMES != null) { BuiltInEngine builtinEngine; @@ -360,6 +386,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$ if (contextType != null) { builtinEngine = new BuiltInEngine(contextType); + // TODO PHPFunctionNames.FUNCTION_NAMES should be taken from phpsyntax.xml builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES); builtinResults = builtinEngine.getResults(); } @@ -367,10 +394,21 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { // concatenate arrays IPHPCompletionProposal[] total; - total = new IPHPCompletionProposal[templateResults.length + identifierResults.length + builtinResults.length]; + total = + new IPHPCompletionProposal[templateResults.length + + identifierResults.length + + builtinResults.length + + declarationResults.length]; System.arraycopy(templateResults, 0, total, 0, templateResults.length); System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length); System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length); + System.arraycopy( + declarationResults, + 0, + total, + templateResults.length + identifierResults.length + builtinResults.length, + declarationResults.length); + results = total; fNumberOfComputedResults = (results == null ? 0 : results.length);