eae847302e8c709b4182a41fb85882f39d49d692
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / corext / template / php / CompilationUnitCompletion.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 //import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 //import java.util.Vector;
19
20 import net.sourceforge.phpdt.core.CompletionRequestorAdapter;
21 import net.sourceforge.phpdt.core.ICompilationUnit;
22 import net.sourceforge.phpdt.core.compiler.IProblem;
23
24 /**
25  * A completion requestor to collect informations on local variables. This class
26  * is used for guessing variable names like arrays, collections, etc.
27  */
28 class CompilationUnitCompletion extends CompletionRequestorAdapter {
29
30         static class LocalVariable {
31                 String name;
32
33                 String typePackageName;
34
35                 String typeName;
36
37                 LocalVariable(String name, String typePackageName, String typeName) {
38                         this.name = name;
39                         this.typePackageName = typePackageName;
40                         this.typeName = typeName;
41                 }
42         }
43
44         //private ICompilationUnit fUnit;
45
46         private List fLocalVariables = new ArrayList();
47
48         private Map fTypes = new HashMap();
49
50         //private boolean fError;
51
52         /**
53          * Creates a compilation unit completion.
54          * 
55          * @param unit
56          *            the compilation unit, may be <code>null</code>.
57          */
58         public CompilationUnitCompletion(ICompilationUnit unit) {
59                 reset(unit);
60         }
61
62         /**
63          * Resets the completion requestor.
64          * 
65          * @param unit
66          *            the compilation unit, may be <code>null</code>.
67          */
68         public void reset(ICompilationUnit unit) {
69                 //fUnit = unit;
70
71                 fLocalVariables.clear();
72                 fTypes.clear();
73
74                 //fError = false;
75         }
76
77         /*
78          * @see ICompletionRequestor#acceptError(IProblem)
79          */
80         public void acceptError(IProblem error) {
81                 //fError = true;
82         }
83
84         /*
85          * @see ICodeCompletionRequestor#acceptLocalVariable
86          */
87         public void acceptLocalVariable(char[] name, char[] typePackageName,
88                         char[] typeName, int modifiers, int completionStart,
89                         int completionEnd, int relevance) {
90                 fLocalVariables.add(new LocalVariable(new String(name), new String(
91                                 typePackageName), new String(typeName)));
92         }
93
94         // ---
95
96         /**
97          * Tests if the code completion process produced errors.
98          */
99 //      public boolean hasErrors() {
100 //              return fError;
101 //      }
102
103 //      boolean existsLocalName(String name) {
104 //              for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
105 //                      LocalVariable localVariable = (LocalVariable) iterator.next();
106 //
107 //                      if (localVariable.name.equals(name))
108 //                              return true;
109 //              }
110 //
111 //              return false;
112 //      }
113
114 //      String[] getLocalVariableNames() {
115 //              String[] res = new String[fLocalVariables.size()];
116 //              int i = 0;
117 //              for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
118 //                      LocalVariable localVariable = (LocalVariable) iterator.next();
119 //                      res[i++] = localVariable.name;
120 //              }
121 //              return res;
122 //      }
123
124 //      LocalVariable[] findLocalArrays() {
125 //              Vector vector = new Vector();
126 //
127 //              for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
128 //                      LocalVariable localVariable = (LocalVariable) iterator.next();
129 //
130 //                      if (isArray(localVariable.typeName))
131 //                              vector.add(localVariable);
132 //              }
133 //
134 //              return (LocalVariable[]) vector
135 //                              .toArray(new LocalVariable[vector.size()]);
136 //      }
137
138         // LocalVariable[] findLocalCollections() throws JavaModelException {
139         // Vector vector= new Vector();
140         //
141         // for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();)
142         // {
143         // LocalVariable localVariable= (LocalVariable) iterator.next();
144         //
145         // String typeName= qualify(localVariable.typeName);
146         //                      
147         // if (typeName == null)
148         // continue;
149         //                                              
150         // if (isSubclassOf(typeName, "java.util.Collection")) //$NON-NLS-1$
151         // vector.add(localVariable);
152         // }
153         //
154         // return (LocalVariable[]) vector.toArray(new
155         // LocalVariable[vector.size()]);
156         // }
157
158 //      String simplifyTypeName(String qualifiedName) {
159 //              return (String) fTypes.get(qualifiedName);
160 //      }
161
162 //      private static boolean isArray(String type) {
163 //              return type.endsWith("[]"); //$NON-NLS-1$
164 //      }
165
166         // returns fully qualified name if successful
167         // private String qualify(String typeName) throws JavaModelException {
168         // if (fUnit == null)
169         // return null;
170         //
171         // IType[] types= fUnit.getTypes();
172         //
173         // if (types.length == 0)
174         // return null;
175         //              
176         // String[][] resolvedTypeNames= types[0].resolveType(typeName);
177         //
178         // if (resolvedTypeNames == null)
179         // return null;
180         //                      
181         // return resolvedTypeNames[0][0] + '.' + resolvedTypeNames[0][1];
182         // }
183
184         // type names must be fully qualified
185         // private boolean isSubclassOf(String typeName0, String typeName1) throws
186         // JavaModelException {
187         // if (typeName0.equals(typeName1))
188         // return true;
189         //
190         // if (fUnit == null)
191         // return false;
192         //
193         // IJavaProject project= fUnit.getJavaProject();
194         //
195         // IType type0= project.findType(typeName0);
196         // if (type0 == null)
197         // return false;
198         //
199         // IType type1= project.findType(typeName1);
200         // if (type1 == null)
201         // return false;
202         //
203         // ITypeHierarchy hierarchy= type0.newSupertypeHierarchy(null);
204         // IType[] superTypes= hierarchy.getAllSupertypes(type0);
205         //              
206         // for (int i= 0; i < superTypes.length; i++)
207         // if (superTypes[i].equals(type1))
208         // return true;
209         //              
210         // return false;
211         // }
212
213         /*
214          * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptClass(char[],
215          *      char[], char[], int, int, int, int)
216          */
217         public void acceptClass(char[] packageName, char[] className,
218                         char[] completionName, int modifiers, int completionStart,
219                         int completionEnd, int relevance) {
220                 final String qualifiedName = createQualifiedTypeName(packageName,
221                                 className);
222                 fTypes.put(qualifiedName, String.valueOf(completionName));
223         }
224
225         /*
226          * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptInterface(char[],
227          *      char[], char[], int, int, int, int)
228          */
229         public void acceptInterface(char[] packageName, char[] interfaceName,
230                         char[] completionName, int modifiers, int completionStart,
231                         int completionEnd, int relevance) {
232                 final String qualifiedName = createQualifiedTypeName(packageName,
233                                 interfaceName);
234                 fTypes.put(qualifiedName, String.valueOf(completionName));
235         }
236
237         private static String createQualifiedTypeName(char[] packageName,
238                         char[] className) {
239                 StringBuffer buffer = new StringBuffer();
240
241                 if (packageName.length != 0) {
242                         buffer.append(packageName);
243                         buffer.append('.');
244                 }
245                 buffer.append(className);
246
247                 return buffer.toString();
248         }
249
250 }