2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.SortedMap;
11 import net.sourceforge.phpdt.internal.corext.template.ContextType;
12 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
13 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
14 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
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 DeclarationEngine {
28 /** The context type. */
29 private ContextType 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 DeclarationEngine(ContextType 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, SortedMap map)
67 //,ICompilationUnit compilationUnit)
68 //hrows JavaModelException
70 IDocument document = viewer.getDocument();
72 if (!(fContextType instanceof CompilationUnitContextType))
75 Point selection = viewer.getSelectedRange();
76 // remember selected text
77 String selectedText = null;
78 if (selection.y != 0) {
80 selectedText = document.get(selection.x, selection.y);
81 } catch (BadLocationException e) {
85 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
87 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
88 int start = context.getStart();
89 int end = context.getEnd();
90 String prefix = context.getKey();
91 IRegion region = new Region(start, end - start);
93 String identifier = null;
95 SortedMap subMap = map.subMap(prefix, prefix + '\255');
96 Iterator iter = subMap.keySet().iterator();
97 PHPIdentifierLocation location;
99 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
100 while (iter.hasNext()) {
101 identifier = (String) iter.next();
102 if (context.canEvaluate(identifier)) {
103 list = (ArrayList) subMap.get(identifier);
104 for (int i = 0; i < list.size(); i++) {
105 if (maxProposals-- < 0) {
108 fProposals.add(new DeclarationProposal(identifier, (PHPIdentifierLocation) list.get(i), context, region, viewer));