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.ICompilationUnit;
12 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
15 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
16 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.Region;
27 import org.eclipse.swt.graphics.Point;
29 public class DeclarationEngine {
31 /** The context type. */
32 private JavaContextType fContextType;
34 /** The result proposals. */
35 private ArrayList fProposals = new ArrayList();
37 /** Token determines last which declarations are allowed for proposal */
38 private int fLastSignificantToken;
40 private IProject fProject;
42 // private IFile fFile;
43 private String fFileName;
46 * Creates the template engine for a particular context type. See
47 * <code>TemplateContext</code> for supported context types.
49 public DeclarationEngine(IProject project, JavaContextType contextType,
50 int lastSignificantToken, IFile file) {
51 // Assert.isNotNull(contextType);
53 fContextType = contextType;
55 fLastSignificantToken = lastSignificantToken;
58 fFileName = file.getProjectRelativePath().toString();
65 * Empties the collector.
70 * the compilation unit (may be <code>null</code>)
77 * Returns the array of matching templates.
79 public IPHPCompletionProposal[] getResults() {
80 return (IPHPCompletionProposal[]) fProposals
81 .toArray(new IPHPCompletionProposal[fProposals.size()]);
85 * Inspects the context of the compilation unit around
86 * <code>completionPosition</code> and feeds the collector with proposals.
90 * @param completionPosition
91 * the context position in the document of the text viewer
92 * @param compilationUnit
93 * the compilation unit (may be <code>null</code>)
95 public void completeObject(ITextViewer viewer, int completionPosition,
96 SortedMap map, ICompilationUnit compilationUnit) {
97 IDocument document = viewer.getDocument();
99 if (!(fContextType instanceof CompilationUnitContextType))
102 Point selection = viewer.getSelectedRange();
104 // remember selected text
105 String selectedText = null;
107 if (selection.y != 0) {
109 selectedText = document.get(selection.x, selection.y);
110 } catch (BadLocationException e) {
114 JavaContext context = (JavaContext) fContextType.createContext(
115 document, completionPosition, selection.y, compilationUnit);
116 context.setVariable("selection", selectedText); //$NON-NLS-1$
118 int start = context.getStart();
119 int end = context.getEnd();
120 String prefix = context.getKey();
121 IRegion region = new Region(start, end - start);
123 String identifier = null;
125 SortedMap subMap = map.subMap(prefix, prefix + '\255');
126 Iterator iter = subMap.keySet().iterator();
127 PHPIdentifierLocation location;
129 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
130 while (iter.hasNext()) {
131 identifier = (String) iter.next();
132 if (context.canEvaluate(identifier)) {
133 list = (ArrayList) subMap.get(identifier);
134 for (int i = 0; i < list.size(); i++) {
135 location = (PHPIdentifierLocation) list.get(i);
136 int type = location.getType();
137 switch (fLastSignificantToken) {
138 case ITerminalSymbols.TokenNameMINUS_GREATER:
139 if (type != PHPIdentifierLocation.METHOD
140 && type != PHPIdentifierLocation.VARIABLE) {
141 continue; // for loop
144 case ITerminalSymbols.TokenNameVariable:
145 if (type != PHPIdentifierLocation.METHOD
146 && type != PHPIdentifierLocation.VARIABLE) {
147 continue; // for loop
149 // check all filenames of the subclasses
150 // if (fFileName.equals(location.getFilename())) {
151 // continue; // for loop
154 case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
155 if (type != PHPIdentifierLocation.METHOD
156 && type != PHPIdentifierLocation.VARIABLE) {
157 continue; // for loop
159 // check all filenames of the subclasses
160 // if (!fFileName.equals(location.getFilename())) {
161 // continue; // for loop
164 case ITerminalSymbols.TokenNamenew:
165 if (type != PHPIdentifierLocation.CLASS
166 && type != PHPIdentifierLocation.CONSTRUCTOR) {
167 continue; // for loop
171 if (type == PHPIdentifierLocation.METHOD
172 || type == PHPIdentifierLocation.CONSTRUCTOR
173 || type == PHPIdentifierLocation.VARIABLE) {
174 continue; // for loop
177 if (maxProposals-- < 0) {
180 fProposals.add(new DeclarationProposal(fProject,
181 identifier, location, context, region, viewer));
189 * Inspects the context of the compilation unit around
190 * <code>completionPosition</code> and feeds the collector with proposals.
194 * @param completionPosition
195 * the context position in the document of the text viewer
196 * @param compilationUnit
197 * the compilation unit (may be <code>null</code>)
199 public void complete(ITextViewer viewer, int completionPosition,
200 SortedMap map, ICompilationUnit compilationUnit) {
201 IDocument document = viewer.getDocument();
203 if (!(fContextType instanceof CompilationUnitContextType))
206 Point selection = viewer.getSelectedRange();
208 // remember selected text
209 String selectedText = null;
211 if (selection.y != 0) {
213 selectedText = document.get(selection.x, selection.y);
214 } catch (BadLocationException e) {
218 // ((CompilationUnitContextType)
219 // fContextType).setContextParameters(document, completionPosition,
222 // CompilationUnitContext context = (CompilationUnitContext)
223 // fContextType.createContext();
224 JavaContext context = (JavaContext) fContextType.createContext(
225 document, completionPosition, selection.y, compilationUnit);
226 context.setVariable("selection", selectedText); //$NON-NLS-1$
228 int start = context.getStart();
229 int end = context.getEnd();
230 String prefix = context.getKey();
231 IRegion region = new Region(start, end - start);
233 String identifier = null;
235 SortedMap subMap = map.subMap(prefix, prefix + '\255');
236 Iterator iter = subMap.keySet().iterator();
237 PHPIdentifierLocation location;
239 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
240 while (iter.hasNext()) {
241 identifier = (String) iter.next();
242 if (context.canEvaluate(identifier)) {
243 list = (ArrayList) subMap.get(identifier);
244 for (int i = 0; i < list.size(); i++) {
245 location = (PHPIdentifierLocation) list.get(i);
246 int type = location.getType();
247 switch (fLastSignificantToken) {
248 case ITerminalSymbols.TokenNameMINUS_GREATER:
249 if (type != PHPIdentifierLocation.METHOD
250 && type != PHPIdentifierLocation.VARIABLE) {
251 continue; // for loop
254 case ITerminalSymbols.TokenNameVariable:
255 if (type != PHPIdentifierLocation.METHOD
256 && type != PHPIdentifierLocation.VARIABLE) {
257 continue; // for loop
259 // check all filenames of the subclasses
260 if (fFileName.equals(location.getFilename())) {
261 continue; // for loop
264 case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
265 if (type != PHPIdentifierLocation.METHOD
266 && type != PHPIdentifierLocation.VARIABLE) {
267 continue; // for loop
269 // check all filenames of the subclasses
270 if (!fFileName.equals(location.getFilename())) {
271 continue; // for loop
274 case ITerminalSymbols.TokenNamenew:
275 if (type != PHPIdentifierLocation.CLASS
276 && type != PHPIdentifierLocation.CONSTRUCTOR) {
277 continue; // for loop
281 if (type == PHPIdentifierLocation.METHOD
282 || type == PHPIdentifierLocation.CONSTRUCTOR
283 || type == PHPIdentifierLocation.VARIABLE) {
284 continue; // for loop
287 if (maxProposals-- < 0) {
290 fProposals.add(new DeclarationProposal(fProject,
291 identifier, location, context, region, viewer));