f393c18bc02909d1e7cbacdba7ec681af89686be
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationEngine.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.SortedMap;
10
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;
19
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;
28
29 public class DeclarationEngine {
30
31         /** The context type. */
32         private JavaContextType fContextType;
33
34         /** The result proposals. */
35         private ArrayList fProposals = new ArrayList();
36
37         /** Token determines last which declarations are allowed for proposal */
38         private int fLastSignificantToken;
39
40         private IProject fProject;
41
42         // private IFile fFile;
43         private String fFileName;
44
45         /**
46          * Creates the template engine for a particular context type. See
47          * <code>TemplateContext</code> for supported context types.
48          */
49         public DeclarationEngine(IProject project, JavaContextType contextType,
50                         int lastSignificantToken, IFile file) {
51                 // Assert.isNotNull(contextType);
52                 fProject = project;
53                 fContextType = contextType;
54
55                 fLastSignificantToken = lastSignificantToken;
56                 // fFile = file;
57                 if (file != null) {
58                         fFileName = file.getProjectRelativePath().toString();
59                 } else {
60                         fFileName = "";
61                 }
62         }
63
64         /**
65          * Empties the collector.
66          * 
67          * @param viewer
68          *            the text viewer
69          * @param unit
70          *            the compilation unit (may be <code>null</code>)
71          */
72         public void reset() {
73                 fProposals.clear();
74         }
75
76         /**
77          * Returns the array of matching templates.
78          */
79         public IPHPCompletionProposal[] getResults() {
80                 return (IPHPCompletionProposal[]) fProposals
81                                 .toArray(new IPHPCompletionProposal[fProposals.size()]);
82         }
83
84         /**
85          * Inspects the context of the compilation unit around
86          * <code>completionPosition</code> and feeds the collector with proposals.
87          * 
88          * @param viewer
89          *            the text viewer
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>)
94          */
95         public void completeObject(ITextViewer viewer, int completionPosition,
96                         SortedMap map, ICompilationUnit compilationUnit) {
97                 IDocument document = viewer.getDocument();
98
99                 if (!(fContextType instanceof CompilationUnitContextType))
100                         return;
101
102                 Point selection = viewer.getSelectedRange();
103
104                 // remember selected text
105                 String selectedText = null;
106
107                 if (selection.y != 0) {
108                         try {
109                                 selectedText = document.get(selection.x, selection.y);
110                         } catch (BadLocationException e) {
111                         }
112                 }
113
114                 JavaContext context = (JavaContext) fContextType.createContext(
115                                 document, completionPosition, selection.y, compilationUnit);
116                 context.setVariable("selection", selectedText); //$NON-NLS-1$
117
118                 int start = context.getStart();
119                 int end = context.getEnd();
120                 String prefix = context.getKey();
121                 IRegion region = new Region(start, end - start);
122
123                 String identifier = null;
124
125                 SortedMap subMap = map.subMap(prefix, prefix + '\255');
126                 Iterator iter = subMap.keySet().iterator();
127                 PHPIdentifierLocation location;
128                 ArrayList list;
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
142                                                 }
143                                                 break;
144                                         case ITerminalSymbols.TokenNameVariable:
145                                                 if (type != PHPIdentifierLocation.METHOD
146                                                                 && type != PHPIdentifierLocation.VARIABLE) {
147                                                         continue; // for loop
148                                                 }
149                                                 // check all filenames of the subclasses
150                                                 // if (fFileName.equals(location.getFilename())) {
151                                                 // continue; // for loop
152                                                 // }
153                                                 break;
154                                         case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
155                                                 if (type != PHPIdentifierLocation.METHOD
156                                                                 && type != PHPIdentifierLocation.VARIABLE) {
157                                                         continue; // for loop
158                                                 }
159                                                 // check all filenames of the subclasses
160                                                 // if (!fFileName.equals(location.getFilename())) {
161                                                 // continue; // for loop
162                                                 // }
163                                                 break;
164                                         case ITerminalSymbols.TokenNamenew:
165                                                 if (type != PHPIdentifierLocation.CLASS
166                                                                 && type != PHPIdentifierLocation.CONSTRUCTOR) {
167                                                         continue; // for loop
168                                                 }
169                                                 break;
170                                         default:
171                                                 if (type == PHPIdentifierLocation.METHOD
172                                                                 || type == PHPIdentifierLocation.CONSTRUCTOR
173                                                                 || type == PHPIdentifierLocation.VARIABLE) {
174                                                         continue; // for loop
175                                                 }
176                                         }
177                                         if (maxProposals-- < 0) {
178                                                 return;
179                                         }
180                                         fProposals.add(new DeclarationProposal(fProject,
181                                                         identifier, location, context, region, viewer));
182                                 }
183                         }
184                 }
185
186         }
187
188         /**
189          * Inspects the context of the compilation unit around
190          * <code>completionPosition</code> and feeds the collector with proposals.
191          * 
192          * @param viewer
193          *            the text viewer
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>)
198          */
199         public void complete(ITextViewer viewer, int completionPosition,
200                         SortedMap map, ICompilationUnit compilationUnit) {
201                 IDocument document = viewer.getDocument();
202
203                 if (!(fContextType instanceof CompilationUnitContextType))
204                         return;
205
206                 Point selection = viewer.getSelectedRange();
207
208                 // remember selected text
209                 String selectedText = null;
210
211                 if (selection.y != 0) {
212                         try {
213                                 selectedText = document.get(selection.x, selection.y);
214                         } catch (BadLocationException e) {
215                         }
216                 }
217
218                 // ((CompilationUnitContextType)
219                 // fContextType).setContextParameters(document, completionPosition,
220                 // selection.y);
221
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$
227
228                 int start = context.getStart();
229                 int end = context.getEnd();
230                 String prefix = context.getKey();
231                 IRegion region = new Region(start, end - start);
232
233                 String identifier = null;
234
235                 SortedMap subMap = map.subMap(prefix, prefix + '\255');
236                 Iterator iter = subMap.keySet().iterator();
237                 PHPIdentifierLocation location;
238                 ArrayList list;
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
252                                                 }
253                                                 break;
254                                         case ITerminalSymbols.TokenNameVariable:
255                                                 if (type != PHPIdentifierLocation.METHOD
256                                                                 && type != PHPIdentifierLocation.VARIABLE) {
257                                                         continue; // for loop
258                                                 }
259                                                 // check all filenames of the subclasses
260                                                 if (fFileName.equals(location.getFilename())) {
261                                                         continue; // for loop
262                                                 }
263                                                 break;
264                                         case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
265                                                 if (type != PHPIdentifierLocation.METHOD
266                                                                 && type != PHPIdentifierLocation.VARIABLE) {
267                                                         continue; // for loop
268                                                 }
269                                                 // check all filenames of the subclasses
270                                                 if (!fFileName.equals(location.getFilename())) {
271                                                         continue; // for loop
272                                                 }
273                                                 break;
274                                         case ITerminalSymbols.TokenNamenew:
275                                                 if (type != PHPIdentifierLocation.CLASS
276                                                                 && type != PHPIdentifierLocation.CONSTRUCTOR) {
277                                                         continue; // for loop
278                                                 }
279                                                 break;
280                                         default:
281                                                 if (type == PHPIdentifierLocation.METHOD
282                                                                 || type == PHPIdentifierLocation.CONSTRUCTOR
283                                                                 || type == PHPIdentifierLocation.VARIABLE) {
284                                                         continue; // for loop
285                                                 }
286                                         }
287                                         if (maxProposals-- < 0) {
288                                                 return;
289                                         }
290                                         fProposals.add(new DeclarationProposal(fProject,
291                                                         identifier, location, context, region, viewer));
292                                 }
293                         }
294                 }
295
296         }
297
298 }