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.text.java.IPHPCompletionProposal;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
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 BuiltInEngine {
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 BuiltInEngine(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 ArrayList identifiers, ICompilationUnit compilationUnit)
75 // hrows JavaModelException
77 IDocument document = viewer.getDocument();
80 // if (LinkedPositionManager.hasActiveManager(document))
83 if (!(fContextType instanceof CompilationUnitContextType))
85 Point selection = viewer.getSelectedRange();
86 // remember selected text
87 String selectedText = null;
88 if (selection.y != 0) {
90 selectedText = document.get(selection.x, selection.y);
91 } catch (BadLocationException e) {
95 // ((CompilationUnitContextType)
96 // fContextType).setContextParameters(document, completionPosition,
97 // selection.y); //mpilationUnit);
98 // JavaContext context = (JavaContext) fContextType.createContext();
99 JavaContext context = (JavaContext) fContextType.createContext(
100 document, completionPosition, selection.y, compilationUnit);
101 context.setVariable("selection", selectedText); //$NON-NLS-1$
102 int start = context.getStart();
103 int end = context.getEnd();
104 IRegion region = new Region(start, end - start);
106 // Template[] templates= Templates.getInstance().getTemplates();
107 String identifier = null;
108 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
109 PHPElement element = null;
110 for (int i = 0; i != identifiers.size(); i++) {
111 element = (PHPElement) identifiers.get(i);
112 if (element instanceof PHPFunction) {
113 identifier = ((PHPFunction) element).getName();
114 if (context.canEvaluate(identifier)) {
115 if (maxProposals-- < 0) {
118 fProposals.add(new BuiltInProposal(identifier,
119 (PHPFunction) element, context, region, viewer));