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