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();
32 /** Use only methods or variables inside a class*/
33 private boolean fUseClassEntries;
36 * Creates the template engine for a particular context type.
37 * See <code>TemplateContext</code> for supported context types.
39 public DeclarationEngine(ContextType contextType, boolean useClassEntries) {
40 // Assert.isNotNull(contextType);
41 fContextType = contextType;
42 fUseClassEntries = useClassEntries;
46 * Empties the collector.
48 * @param viewer the text viewer
49 * @param unit the compilation unit (may be <code>null</code>)
56 * Returns the array of matching templates.
58 public IPHPCompletionProposal[] getResults() {
59 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
63 * Inspects the context of the compilation unit around <code>completionPosition</code>
64 * and feeds the collector with proposals.
65 * @param viewer the text viewer
66 * @param completionPosition the context position in the document of the text viewer
67 * @param compilationUnit the compilation unit (may be <code>null</code>)
69 public void complete(ITextViewer viewer, int completionPosition, SortedMap map) {
70 IDocument document = viewer.getDocument();
72 if (!(fContextType instanceof CompilationUnitContextType))
75 Point selection = viewer.getSelectedRange();
76 // remember selected text
77 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);
88 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
89 int start = context.getStart();
90 int end = context.getEnd();
91 String prefix = context.getKey();
92 IRegion region = new Region(start, end - start);
94 String identifier = null;
96 SortedMap subMap = map.subMap(prefix, prefix + '\255');
97 Iterator iter = subMap.keySet().iterator();
98 PHPIdentifierLocation location;
100 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
101 while (iter.hasNext()) {
102 identifier = (String) iter.next();
103 if (context.canEvaluate(identifier)) {
104 list = (ArrayList) subMap.get(identifier);
105 for (int i = 0; i < list.size(); i++) {
106 location = (PHPIdentifierLocation) list.get(i);
107 int type = location.getType();
108 if (fUseClassEntries) {
109 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
110 continue; // for loop
113 if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.VARIABLE) {
114 continue; // for loop
117 if (maxProposals-- < 0) {
120 fProposals.add(new DeclarationProposal(identifier, location, context, region, viewer));