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;
17 import net.sourceforge.phpeclipse.ui.WebUI;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.Region;
24 import org.eclipse.swt.graphics.Point;
26 public class BuiltInEngine {
28 /** The context type. */
29 private JavaContextType fContextType;
31 /** The result proposals. */
32 private ArrayList fProposals = new ArrayList();
35 * Creates the template engine for a particular context type. See
36 * <code>TemplateContext</code> for supported context types.
38 public BuiltInEngine(JavaContextType contextType) {
39 // Assert.isNotNull(contextType);
40 fContextType = contextType;
44 * Empties the collector.
49 * the compilation unit (may be <code>null</code>)
56 * Returns the array of matching templates.
58 public IPHPCompletionProposal[] getResults() {
59 return (IPHPCompletionProposal[]) fProposals
60 .toArray(new IPHPCompletionProposal[fProposals.size()]);
64 * Inspects the context of the compilation unit around
65 * <code>completionPosition</code> and feeds the collector with proposals.
69 * @param completionPosition
70 * the context position in the document of the text viewer
71 * @param compilationUnit
72 * the compilation unit (may be <code>null</code>)
74 public void complete(ITextViewer viewer, int completionPosition,
75 ArrayList identifiers, ICompilationUnit compilationUnit)
76 // hrows JavaModelException
78 IDocument document = viewer.getDocument();
81 // if (LinkedPositionManager.hasActiveManager(document))
84 if (!(fContextType instanceof CompilationUnitContextType))
86 Point selection = viewer.getSelectedRange();
87 // remember selected text
88 String selectedText = null;
89 if (selection.y != 0) {
91 selectedText = document.get(selection.x, selection.y);
92 } catch (BadLocationException e) {
96 // ((CompilationUnitContextType)
97 // fContextType).setContextParameters(document, completionPosition,
98 // selection.y); //mpilationUnit);
99 // JavaContext context = (JavaContext) fContextType.createContext();
100 JavaContext context = (JavaContext) fContextType.createContext(
101 document, completionPosition, selection.y, compilationUnit);
102 context.setVariable("selection", selectedText); //$NON-NLS-1$
103 int start = context.getStart();
104 int end = context.getEnd();
105 IRegion region = new Region(start, end - start);
107 // Template[] templates= Templates.getInstance().getTemplates();
108 String identifier = null;
109 int maxProposals = WebUI.MAX_PROPOSALS;
110 PHPElement element = null;
111 for (int i = 0; i != identifiers.size(); i++) {
112 element = (PHPElement) identifiers.get(i);
113 if (element instanceof PHPFunction) {
114 identifier = ((PHPFunction) element).getName();
115 if (context.canEvaluate(identifier)) {
116 if (maxProposals-- < 0) {
119 fProposals.add(new BuiltInProposal(identifier,
120 (PHPFunction) element, context, region, viewer));