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.core.compiler.ITerminalSymbols.TokenName;
14 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
15 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
16 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
17 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IRegion;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.Region;
28 import org.eclipse.swt.graphics.Point;
30 public class DeclarationEngine {
32 /** The context type. */
33 private JavaContextType fContextType;
35 /** The result proposals. */
36 private ArrayList fProposals = new ArrayList();
38 /** Token determines last which declarations are allowed for proposal */
39 private TokenName fLastSignificantToken;
41 private IProject fProject;
43 // private IFile fFile;
44 private String fFileName;
47 * Creates the template engine for a particular context type. See
48 * <code>TemplateContext</code> for supported context types.
50 public DeclarationEngine(IProject project, JavaContextType contextType,
51 TokenName lastSignificantToken, IFile file) {
52 // Assert.isNotNull(contextType);
54 fContextType = contextType;
56 fLastSignificantToken = lastSignificantToken;
59 fFileName = file.getProjectRelativePath().toString();
66 * Empties the collector.
71 * the compilation unit (may be <code>null</code>)
78 * Returns the array of matching templates.
80 public IPHPCompletionProposal[] getResults() {
81 return (IPHPCompletionProposal[]) fProposals
82 .toArray(new IPHPCompletionProposal[fProposals.size()]);
86 * Inspects the context of the compilation unit around
87 * <code>completionPosition</code> and feeds the collector with proposals.
91 * @param completionPosition
92 * the context position in the document of the text viewer
93 * @param compilationUnit
94 * the compilation unit (may be <code>null</code>)
96 public void completeObject(ITextViewer viewer, int completionPosition,
97 SortedMap map, ICompilationUnit compilationUnit) {
98 IDocument document = viewer.getDocument();
100 if (!(fContextType instanceof CompilationUnitContextType))
103 Point selection = viewer.getSelectedRange();
105 // remember selected text
106 String selectedText = null;
108 if (selection.y != 0) {
110 selectedText = document.get(selection.x, selection.y);
111 } catch (BadLocationException e) {
115 JavaContext context = (JavaContext) fContextType.createContext(
116 document, completionPosition, selection.y, compilationUnit);
117 context.setVariable("selection", selectedText); //$NON-NLS-1$
119 int start = context.getStart();
120 int end = context.getEnd();
121 String prefix = context.getKey();
122 IRegion region = new Region(start, end - start);
124 String identifier = null;
126 SortedMap subMap = map.subMap(prefix, prefix + '\255');
127 Iterator iter = subMap.keySet().iterator();
128 PHPIdentifierLocation location;
130 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
131 while (iter.hasNext()) {
132 identifier = (String) iter.next();
133 if (context.canEvaluate(identifier)) {
134 list = (ArrayList) subMap.get(identifier);
135 for (int i = 0; i < list.size(); i++) {
136 location = (PHPIdentifierLocation) list.get(i);
137 int type = location.getType();
138 switch (fLastSignificantToken) {
140 if (type != PHPIdentifierLocation.METHOD
141 && type != PHPIdentifierLocation.VARIABLE) {
142 continue; // for loop
146 if (type != PHPIdentifierLocation.METHOD
147 && type != PHPIdentifierLocation.VARIABLE) {
148 continue; // for loop
150 // check all filenames of the subclasses
151 // if (fFileName.equals(location.getFilename())) {
152 // continue; // for loop
155 case THIS_PHP_COMPLETION:
156 if (type != PHPIdentifierLocation.METHOD
157 && type != PHPIdentifierLocation.VARIABLE) {
158 continue; // for loop
160 // check all filenames of the subclasses
161 // if (!fFileName.equals(location.getFilename())) {
162 // continue; // for loop
166 if (type != PHPIdentifierLocation.CLASS
167 && type != PHPIdentifierLocation.CONSTRUCTOR) {
168 continue; // for loop
172 if (type == PHPIdentifierLocation.METHOD
173 || type == PHPIdentifierLocation.CONSTRUCTOR
174 || type == PHPIdentifierLocation.VARIABLE) {
175 continue; // for loop
178 if (maxProposals-- < 0) {
181 fProposals.add(new DeclarationProposal(fProject,
182 identifier, location, context, region, viewer));
190 * Inspects the context of the compilation unit around
191 * <code>completionPosition</code> and feeds the collector with proposals.
195 * @param completionPosition
196 * the context position in the document of the text viewer
197 * @param compilationUnit
198 * the compilation unit (may be <code>null</code>)
200 public void complete(ITextViewer viewer, int completionPosition,
201 SortedMap map, ICompilationUnit compilationUnit) {
202 IDocument document = viewer.getDocument();
204 if (!(fContextType instanceof CompilationUnitContextType))
207 Point selection = viewer.getSelectedRange();
209 // remember selected text
210 String selectedText = null;
212 if (selection.y != 0) {
214 selectedText = document.get(selection.x, selection.y);
215 } catch (BadLocationException e) {
219 // ((CompilationUnitContextType)
220 // fContextType).setContextParameters(document, completionPosition,
223 // CompilationUnitContext context = (CompilationUnitContext)
224 // fContextType.createContext();
225 JavaContext context = (JavaContext) fContextType.createContext(
226 document, completionPosition, selection.y, compilationUnit);
227 context.setVariable("selection", selectedText); //$NON-NLS-1$
229 int start = context.getStart();
230 int end = context.getEnd();
231 String prefix = context.getKey();
232 IRegion region = new Region(start, end - start);
234 String identifier = null;
236 SortedMap subMap = map.subMap(prefix, prefix + '\255');
237 Iterator iter = subMap.keySet().iterator();
238 PHPIdentifierLocation location;
240 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
241 while (iter.hasNext()) {
242 identifier = (String) iter.next();
243 if (context.canEvaluate(identifier)) {
244 list = (ArrayList) subMap.get(identifier);
245 for (int i = 0; i < list.size(); i++) {
246 location = (PHPIdentifierLocation) list.get(i);
247 int type = location.getType();
248 switch (fLastSignificantToken) {
250 if (type != PHPIdentifierLocation.METHOD
251 && type != PHPIdentifierLocation.VARIABLE) {
252 continue; // for loop
256 if (type != PHPIdentifierLocation.METHOD
257 && type != PHPIdentifierLocation.VARIABLE) {
258 continue; // for loop
260 // check all filenames of the subclasses
261 if (fFileName.equals(location.getFilename())) {
262 continue; // for loop
265 case THIS_PHP_COMPLETION:
266 if (type != PHPIdentifierLocation.METHOD
267 && type != PHPIdentifierLocation.VARIABLE) {
268 continue; // for loop
270 // check all filenames of the subclasses
271 if (!fFileName.equals(location.getFilename())) {
272 continue; // for loop
276 if (type != PHPIdentifierLocation.CLASS
277 && type != PHPIdentifierLocation.CONSTRUCTOR) {
278 continue; // for loop
282 if (type == PHPIdentifierLocation.METHOD
283 || type == PHPIdentifierLocation.CONSTRUCTOR
284 || type == PHPIdentifierLocation.VARIABLE) {
285 continue; // for loop
288 if (maxProposals-- < 0) {
291 fProposals.add(new DeclarationProposal(fProject,
292 identifier, location, context, region, viewer));