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.PHPeclipsePlugin;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.Region;
22 import org.eclipse.swt.graphics.Point;
24 public class IdentifierEngine {
26 /** The context type. */
27 private JavaContextType fContextType;
29 /** The result proposals. */
30 private ArrayList fProposals = new ArrayList();
33 * Creates the template engine for a particular context type. See
34 * <code>TemplateContext</code> for supported context types.
36 public IdentifierEngine(JavaContextType contextType) {
37 // Assert.isNotNull(contextType);
38 fContextType = contextType;
42 * Empties the collector.
47 * the compilation unit (may be <code>null</code>)
54 * Returns the array of matching templates.
56 public IPHPCompletionProposal[] getResults() {
57 return (IPHPCompletionProposal[]) fProposals
58 .toArray(new IPHPCompletionProposal[fProposals.size()]);
62 * Inspects the context of the compilation unit around
63 * <code>completionPosition</code> and feeds the collector with proposals.
67 * @param completionPosition
68 * the context position in the document of the text viewer
69 * @param compilationUnit
70 * the compilation unit (may be <code>null</code>)
72 public void complete(ITextViewer viewer, int completionPosition,
73 Object[] identifiers, ICompilationUnit compilationUnit)
74 // hrows JavaModelException
76 IDocument document = viewer.getDocument();
78 if (!(fContextType instanceof CompilationUnitContextType))
81 Point selection = viewer.getSelectedRange();
82 // remember selected text
83 String selectedText = null;
84 if (selection.y != 0) {
86 selectedText = document.get(selection.x, selection.y);
87 } catch (BadLocationException e) {
91 // ((CompilationUnitContextType)
92 // fContextType).setContextParameters(document, completionPosition,
93 // selection.y); //mpilationUnit);
95 // JavaContext context = (JavaContext) fContextType.createContext();
96 JavaContext context = (JavaContext) fContextType.createContext(
97 document, completionPosition, selection.y, compilationUnit);
98 context.setVariable("selection", selectedText); //$NON-NLS-1$
100 int start = context.getStart();
101 int end = context.getEnd();
102 IRegion region = new Region(start, end - start);
104 // Template[] templates= Templates.getInstance().getTemplates();
105 String identifier = null;
106 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
108 for (int i = 0; i != identifiers.length; i++) {
109 identifier = (String) identifiers[i];
110 if (context.canEvaluate(identifier)) {
111 if (maxProposals-- < 0) {
114 fProposals.add(new IdentifierProposal(identifier, context,
115 region, viewer, PHPUiImages.get(PHPUiImages.IMG_FUN),
116 PHPUiImages.get(PHPUiImages.IMG_VAR)));