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.core.compiler.ITerminalSymbols;
12 import net.sourceforge.phpdt.internal.corext.template.ContextType;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
15 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.Region;
25 import org.eclipse.swt.graphics.Point;
26 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
28 public class DeclarationEngine {
30 /** The context type. */
31 private ContextType fContextType;
32 /** The result proposals. */
33 private ArrayList fProposals = new ArrayList();
34 /** Token determines last which declarations are allowed for proposal */
35 private int fLastSignificantToken;
38 private String fFileName;
41 * Creates the template engine for a particular context type.
42 * See <code>TemplateContext</code> for supported context types.
44 public DeclarationEngine(ContextType contextType, int lastSignificantToken, IFile file) {
45 // Assert.isNotNull(contextType);
46 fContextType = contextType;
48 fLastSignificantToken = lastSignificantToken;
51 fFileName = fFile.getFullPath().toString();
58 * Empties the collector.
60 * @param viewer the text viewer
61 * @param unit the compilation unit (may be <code>null</code>)
68 * Returns the array of matching templates.
70 public IPHPCompletionProposal[] getResults() {
71 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
75 * Inspects the context of the compilation unit around <code>completionPosition</code>
76 * and feeds the collector with proposals.
77 * @param viewer the text viewer
78 * @param completionPosition the context position in the document of the text viewer
79 * @param compilationUnit the compilation unit (may be <code>null</code>)
81 public void complete(ITextViewer viewer, int completionPosition, SortedMap map) {
82 IDocument document = viewer.getDocument();
84 if (!(fContextType instanceof CompilationUnitContextType))
87 Point selection = viewer.getSelectedRange();
89 // remember selected text
90 String selectedText = null;
92 if (selection.y != 0) {
94 selectedText = document.get(selection.x, selection.y);
95 } catch (BadLocationException e) {
99 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
101 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
102 int start = context.getStart();
103 int end = context.getEnd();
104 String prefix = context.getKey();
105 IRegion region = new Region(start, end - start);
107 String identifier = null;
109 SortedMap subMap = map.subMap(prefix, prefix + '\255');
110 Iterator iter = subMap.keySet().iterator();
111 PHPIdentifierLocation location;
113 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
114 while (iter.hasNext()) {
115 identifier = (String) iter.next();
116 if (context.canEvaluate(identifier)) {
117 list = (ArrayList) subMap.get(identifier);
118 for (int i = 0; i < list.size(); i++) {
119 location = (PHPIdentifierLocation) list.get(i);
120 int type = location.getType();
121 switch (fLastSignificantToken) {
122 case ITerminalSymbols.TokenNameMINUS_GREATER :
123 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
124 continue; // for loop
127 case ITerminalSymbols.TokenNameVariable :
128 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
129 continue; // for loop
131 if (!fFileName.equals(location.getFilename())) {
132 continue; // for loop
135 case ITerminalSymbols.TokenNamenew :
136 if (type != PHPIdentifierLocation.CLASS && type != PHPIdentifierLocation.CONSTRUCTOR) {
137 continue; // for loop
141 if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.CONSTRUCTOR || type == PHPIdentifierLocation.VARIABLE) {
142 continue; // for loop
145 if (maxProposals-- < 0) {
148 fProposals.add(new DeclarationProposal(identifier, location, context, region, viewer));