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;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
26 public class BuiltInEngine {
28 /** The context type. */
29 private JavaContextType fContextType;
30 /** The result proposals. */
31 private ArrayList fProposals = new ArrayList();
34 * Creates the template engine for a particular context type.
35 * See <code>TemplateContext</code> for supported context types.
37 public BuiltInEngine(JavaContextType contextType) {
38 // Assert.isNotNull(contextType);
39 fContextType = contextType;
43 * Empties the collector.
45 * @param viewer the text viewer
46 * @param unit the compilation unit (may be <code>null</code>)
53 * Returns the array of matching templates.
55 public IPHPCompletionProposal[] getResults() {
56 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
60 * Inspects the context of the compilation unit around <code>completionPosition</code>
61 * and feeds the collector with proposals.
62 * @param viewer the text viewer
63 * @param completionPosition the context position in the document of the text viewer
64 * @param compilationUnit the compilation unit (may be <code>null</code>)
66 public void complete(ITextViewer viewer, int completionPosition, ArrayList identifiers,ICompilationUnit compilationUnit)
67 //hrows JavaModelException
69 IDocument document = viewer.getDocument();
72 // if (LinkedPositionManager.hasActiveManager(document))
75 if (!(fContextType instanceof CompilationUnitContextType))
77 Point selection = viewer.getSelectedRange();
78 // remember selected text
79 String selectedText = null;
80 if (selection.y != 0) {
82 selectedText = document.get(selection.x, selection.y);
83 } catch (BadLocationException e) {
87 // ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
88 // JavaContext context = (JavaContext) fContextType.createContext();
89 JavaContext context = (JavaContext) fContextType.createContext(document, completionPosition,selection.y,compilationUnit);
90 context.setVariable("selection", selectedText); //$NON-NLS-1$
91 int start = context.getStart();
92 int end = context.getEnd();
93 IRegion region = new Region(start, end - start);
95 // Template[] templates= Templates.getInstance().getTemplates();
96 String identifier = null;
97 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
98 PHPElement element = null;
99 for (int i = 0; i != identifiers.size(); i++) {
100 element = (PHPElement) identifiers.get(i);
101 if (element instanceof PHPFunction) {
102 identifier = ((PHPFunction) element).getName();
103 if (context.canEvaluate(identifier)) {
104 if (maxProposals-- < 0) {
107 fProposals.add(new BuiltInProposal(identifier, (PHPFunction) element, context, region, viewer));