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.core.resources.IProject;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.swt.graphics.Point;
27 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
29 public class DeclarationEngine {
31 /** The context type. */
32 private ContextType fContextType;
33 /** The result proposals. */
34 private ArrayList fProposals = new ArrayList();
35 /** Token determines last which declarations are allowed for proposal */
36 private int fLastSignificantToken;
38 private IProject fProject;
40 // private String fFileName;
43 * Creates the template engine for a particular context type.
44 * See <code>TemplateContext</code> for supported context types.
46 public DeclarationEngine(IProject project, ContextType contextType, int lastSignificantToken, IFile file) {
47 // Assert.isNotNull(contextType);
49 fContextType = contextType;
51 fLastSignificantToken = lastSignificantToken;
53 // if (fFile != null) {
54 // fFileName = fFile.getFullPath().toString();
61 * Empties the collector.
63 * @param viewer the text viewer
64 * @param unit the compilation unit (may be <code>null</code>)
71 * Returns the array of matching templates.
73 public IPHPCompletionProposal[] getResults() {
74 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
78 * Inspects the context of the compilation unit around <code>completionPosition</code>
79 * and feeds the collector with proposals.
80 * @param viewer the text viewer
81 * @param completionPosition the context position in the document of the text viewer
82 * @param compilationUnit the compilation unit (may be <code>null</code>)
84 public void complete(ITextViewer viewer, int completionPosition, SortedMap map) {
85 IDocument document = viewer.getDocument();
87 if (!(fContextType instanceof CompilationUnitContextType))
90 Point selection = viewer.getSelectedRange();
92 // remember selected text
93 String selectedText = null;
95 if (selection.y != 0) {
97 selectedText = document.get(selection.x, selection.y);
98 } catch (BadLocationException e) {
102 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
104 PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
105 int start = context.getStart();
106 int end = context.getEnd();
107 String prefix = context.getKey();
108 IRegion region = new Region(start, end - start);
110 String identifier = null;
112 SortedMap subMap = map.subMap(prefix, prefix + '\255');
113 Iterator iter = subMap.keySet().iterator();
114 PHPIdentifierLocation location;
116 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
117 while (iter.hasNext()) {
118 identifier = (String) iter.next();
119 if (context.canEvaluate(identifier)) {
120 list = (ArrayList) subMap.get(identifier);
121 for (int i = 0; i < list.size(); i++) {
122 location = (PHPIdentifierLocation) list.get(i);
123 int type = location.getType();
124 switch (fLastSignificantToken) {
125 case ITerminalSymbols.TokenNameMINUS_GREATER :
126 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
127 continue; // for loop
130 case ITerminalSymbols.TokenNameVariable :
131 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
132 continue; // for loop
135 case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
136 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
137 continue; // for loop
139 // check all filenames of the subclasses
140 // if (!fFileName.equals(location.getFilename())) {
141 // continue; // for loop
144 case ITerminalSymbols.TokenNamenew :
145 if (type != PHPIdentifierLocation.CLASS && type != PHPIdentifierLocation.CONSTRUCTOR) {
146 continue; // for loop
150 if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.CONSTRUCTOR || type == PHPIdentifierLocation.VARIABLE) {
151 continue; // for loop
154 if (maxProposals-- < 0) {
157 fProposals.add( new DeclarationProposal(fProject, identifier, location, context, region, viewer));