2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
9 import net.sourceforge.phpdt.core.ICompilationUnit;
10 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
11 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
12 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
14 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
15 import net.sourceforge.phpeclipse.ui.WebUI;
16 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.Region;
23 import org.eclipse.swt.graphics.Point;
25 public class IdentifierEngine {
27 /** The context type. */
28 private JavaContextType fContextType;
30 /** The result proposals. */
31 private ArrayList fProposals = new ArrayList();
34 * Creates the template engine for a particular context type. See
35 * <code>TemplateContext</code> for supported context types.
37 public IdentifierEngine(JavaContextType contextType) {
38 // Assert.isNotNull(contextType);
39 fContextType = contextType;
43 * Empties the collector.
48 * the compilation unit (may be <code>null</code>)
55 * Returns the array of matching templates.
57 public IPHPCompletionProposal[] getResults() {
58 return (IPHPCompletionProposal[]) fProposals
59 .toArray(new IPHPCompletionProposal[fProposals.size()]);
63 * Inspects the context of the compilation unit around
64 * <code>completionPosition</code> and feeds the collector with proposals.
68 * @param completionPosition
69 * the context position in the document of the text viewer
70 * @param compilationUnit
71 * the compilation unit (may be <code>null</code>)
73 public void complete(ITextViewer viewer, int completionPosition,
74 Object[] identifiers, ICompilationUnit compilationUnit)
75 // hrows JavaModelException
77 IDocument document = viewer.getDocument();
79 if (!(fContextType instanceof CompilationUnitContextType))
82 Point selection = viewer.getSelectedRange();
83 // remember selected text
84 String selectedText = null;
85 if (selection.y != 0) {
87 selectedText = document.get(selection.x, selection.y);
88 } catch (BadLocationException e) {
92 // ((CompilationUnitContextType)
93 // fContextType).setContextParameters(document, completionPosition,
94 // selection.y); //mpilationUnit);
96 // JavaContext context = (JavaContext) fContextType.createContext();
97 JavaContext context = (JavaContext) fContextType.createContext(
98 document, completionPosition, selection.y, compilationUnit);
99 context.setVariable("selection", selectedText); //$NON-NLS-1$
101 int start = context.getStart();
102 int end = context.getEnd();
103 IRegion region = new Region(start, end - start);
105 // Template[] templates= Templates.getInstance().getTemplates();
106 String identifier = null;
107 int maxProposals = WebUI.MAX_PROPOSALS;
109 for (int i = 0; i != identifiers.length; i++) {
110 identifier = (String) identifiers[i];
111 if (context.canEvaluate(identifier)) {
112 if (maxProposals-- < 0) {
115 fProposals.add(new IdentifierProposal(identifier, context,
116 region, viewer, PHPUiImages.get(PHPUiImages.IMG_FUN),
117 PHPUiImages.get(PHPUiImages.IMG_VAR)));