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.internal.corext.template.ContextType;
10 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
11 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
12 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
13 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.Region;
21 import org.eclipse.swt.graphics.Point;
22 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
24 public class BuiltInEngine {
26 /** The context type. */
27 private ContextType fContextType;
28 /** The result proposals. */
29 private ArrayList fProposals = new ArrayList();
32 * Creates the template engine for a particular context type.
33 * See <code>TemplateContext</code> for supported context types.
35 public BuiltInEngine(ContextType contextType) {
36 // Assert.isNotNull(contextType);
37 fContextType = contextType;
41 * Empties the collector.
43 * @param viewer the text viewer
44 * @param unit the compilation unit (may be <code>null</code>)
51 * Returns the array of matching templates.
53 public IPHPCompletionProposal[] getResults() {
54 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
58 * Inspects the context of the compilation unit around <code>completionPosition</code>
59 * and feeds the collector with proposals.
60 * @param viewer the text viewer
61 * @param completionPosition the context position in the document of the text viewer
62 * @param compilationUnit the compilation unit (may be <code>null</code>)
64 public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
65 //,ICompilationUnit compilationUnit)
66 //hrows JavaModelException
68 IDocument document = viewer.getDocument();
71 // if (LinkedPositionManager.hasActiveManager(document))
74 if (!(fContextType instanceof CompilationUnitContextType))
76 Point selection = viewer.getSelectedRange();
77 // remember selected text
78 String selectedText = null;
79 if (selection.y != 0) {
81 selectedText = document.get(selection.x, selection.y);
82 } catch (BadLocationException e) {
86 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
88 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
89 int start = context.getStart();
90 int end = context.getEnd();
91 IRegion region = new Region(start, end - start);
93 // Template[] templates= Templates.getInstance().getTemplates();
94 String identifier = null;
95 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
96 for (int i = 0; i != identifiers.length; i++) {
97 identifier = (String) identifiers[i];
98 if (context.canEvaluate(identifier)) {
99 if (maxProposals-- < 0) {
102 fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN)));