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;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.Region;
20 import org.eclipse.swt.graphics.Point;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
23 public class IdentifierEngine {
25 /** The context type. */
26 private ContextType fContextType;
27 /** The result proposals. */
28 private ArrayList fProposals = new ArrayList();
31 * Creates the template engine for a particular context type.
32 * See <code>TemplateContext</code> for supported context types.
34 public IdentifierEngine(ContextType contextType) {
35 // Assert.isNotNull(contextType);
36 fContextType = contextType;
40 * Empties the collector.
42 * @param viewer the text viewer
43 * @param unit the compilation unit (may be <code>null</code>)
50 * Returns the array of matching templates.
52 public IPHPCompletionProposal[] getResults() {
53 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
57 * Inspects the context of the compilation unit around <code>completionPosition</code>
58 * and feeds the collector with proposals.
59 * @param viewer the text viewer
60 * @param completionPosition the context position in the document of the text viewer
61 * @param compilationUnit the compilation unit (may be <code>null</code>)
63 public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
64 //,ICompilationUnit compilationUnit)
65 //hrows JavaModelException
67 IDocument document = viewer.getDocument();
69 if (!(fContextType instanceof CompilationUnitContextType))
72 Point selection = viewer.getSelectedRange();
73 // remember selected text
74 String selectedText = null;
75 if (selection.y != 0) {
77 selectedText = document.get(selection.x, selection.y);
78 } catch (BadLocationException e) {
82 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
84 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
85 int start = context.getStart();
86 int end = context.getEnd();
87 IRegion region = new Region(start, end - start);
89 // Template[] templates= Templates.getInstance().getTemplates();
90 String identifier = null;
91 for (int i = 0; i != identifiers.length; i++) {
92 identifier = (String) identifiers[i];
93 if (context.canEvaluate(identifier)) {
95 new IdentifierProposal(
100 PHPUiImages.get(PHPUiImages.IMG_FUN),
101 PHPUiImages.get(PHPUiImages.IMG_VAR)));