Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.
26  * This class 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                 String typePackageName;
33                 String typeName;
34                 
35                 LocalVariable(String name, String typePackageName, String typeName) {
36                         this.name= name;
37                         this.typePackageName= typePackageName;
38                         this.typeName= typeName;
39                 }
40         }
41
42         private ICompilationUnit fUnit;
43
44         private List fLocalVariables= new ArrayList();
45         private Map fTypes= new HashMap();
46
47
48         private boolean fError;
49
50         /**
51          * Creates a compilation unit completion.
52          * 
53          * @param unit the compilation unit, may be <code>null</code>.
54          */
55         public CompilationUnitCompletion(ICompilationUnit unit) {
56                 reset(unit);
57         }
58         
59         /**
60          * Resets the completion requestor.
61          * 
62          * @param unit the compilation unit, may be <code>null</code>.
63          */
64         public void reset(ICompilationUnit unit) {
65                 fUnit= unit;
66                 
67                 fLocalVariables.clear();
68                 fTypes.clear();
69                 
70                 fError= false;
71         }
72
73         /*
74          * @see ICompletionRequestor#acceptError(IProblem)
75          */
76         public void acceptError(IProblem error) {
77                 fError= true;
78         }
79
80
81         /*
82          * @see ICodeCompletionRequestor#acceptLocalVariable
83          */
84         public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName,
85                 int modifiers, int completionStart,     int completionEnd, int relevance)
86         {
87                 fLocalVariables.add(new LocalVariable(
88                         new String(name), new String(typePackageName), new String(typeName)));
89         }
90
91
92         // ---
93
94         /**
95          * Tests if the code completion process produced errors.
96          */
97         public boolean hasErrors() {
98                 return fError;
99         }
100         
101
102         boolean existsLocalName(String name) {
103                 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
104                         LocalVariable localVariable = (LocalVariable) iterator.next();
105
106                         if (localVariable.name.equals(name))
107                                 return true;
108                 }
109
110                 return false;
111         }
112         
113         String[] getLocalVariableNames() {
114                 String[] res= new String[fLocalVariables.size()];
115                 int i= 0;
116                 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
117                         LocalVariable localVariable = (LocalVariable) iterator.next();
118                         res[i++]= localVariable.name;
119                 }               
120                 return res;
121         }       
122
123         LocalVariable[] findLocalArrays() {
124                 Vector vector= new Vector();
125
126                 for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
127                         LocalVariable localVariable= (LocalVariable) iterator.next();
128
129                         if (isArray(localVariable.typeName))
130                                 vector.add(localVariable);
131                 }
132
133                 return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
134         }
135         
136 //      LocalVariable[] findLocalCollections() throws JavaModelException {
137 //              Vector vector= new Vector();
138 //
139 //              for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
140 //                      LocalVariable localVariable= (LocalVariable) iterator.next();
141 //
142 //                      String typeName= qualify(localVariable.typeName);
143 //                      
144 //                      if (typeName == null)
145 //                              continue;
146 //                                              
147 //                      if (isSubclassOf(typeName, "java.util.Collection")) //$NON-NLS-1$                       
148 //                              vector.add(localVariable);
149 //              }
150 //
151 //              return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
152 //      }
153
154         String simplifyTypeName(String qualifiedName) {
155                 return (String) fTypes.get(qualifiedName);      
156         }
157
158         private static boolean isArray(String type) {
159                 return type.endsWith("[]"); //$NON-NLS-1$
160         }
161         
162         // returns fully qualified name if successful
163 //      private String qualify(String typeName) throws JavaModelException {
164 //              if (fUnit == null)
165 //                      return null;
166 //
167 //              IType[] types= fUnit.getTypes();
168 //
169 //              if (types.length == 0)
170 //                      return null;
171 //              
172 //              String[][] resolvedTypeNames= types[0].resolveType(typeName);
173 //
174 //              if (resolvedTypeNames == null)
175 //                      return null;
176 //                      
177 //              return resolvedTypeNames[0][0] + '.' + resolvedTypeNames[0][1];
178 //      }       
179         
180         // type names must be fully qualified
181 //      private boolean isSubclassOf(String typeName0, String typeName1) throws JavaModelException {
182 //              if (typeName0.equals(typeName1))
183 //                      return true;
184 //
185 //              if (fUnit == null)
186 //                      return false;
187 //
188 //              IJavaProject project= fUnit.getJavaProject();
189 //
190 //              IType type0= project.findType(typeName0);
191 //              if (type0 == null)
192 //                      return false;
193 //
194 //              IType type1= project.findType(typeName1);
195 //              if (type1 == null)
196 //                      return false;
197 //
198 //              ITypeHierarchy hierarchy= type0.newSupertypeHierarchy(null);
199 //              IType[] superTypes= hierarchy.getAllSupertypes(type0);
200 //              
201 //              for (int i= 0; i < superTypes.length; i++)
202 //                      if (superTypes[i].equals(type1))
203 //                              return true;                    
204 //              
205 //              return false;
206 //      }
207
208         /*
209          * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptClass(char[], char[], char[], int, int, int, int)
210          */
211         public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers,
212                 int completionStart, int completionEnd, int relevance)
213         {
214                 final String qualifiedName= createQualifiedTypeName(packageName, className);
215                 fTypes.put(qualifiedName, String.valueOf(completionName));
216         }
217
218         /*
219          * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptInterface(char[], char[], char[], int, int, int, int)
220          */
221         public void acceptInterface(char[] packageName, char[] interfaceName, char[] completionName,
222                 int modifiers, int completionStart, int completionEnd, int relevance)
223         {
224                 final String qualifiedName= createQualifiedTypeName(packageName, interfaceName);
225                 fTypes.put(qualifiedName, String.valueOf(completionName));
226         }
227
228         private static String createQualifiedTypeName(char[] packageName, char[] className) {
229                 StringBuffer buffer= new StringBuffer();
230
231                 if (packageName.length != 0) {
232                         buffer.append(packageName);
233                         buffer.append('.');
234                 }
235                 buffer.append(className);
236                 
237                 return buffer.toString();
238         }
239
240 }
241