Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / InternalNamingConventions.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.core;
12
13 import java.util.Map;
14
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.core.IJavaProject;
17 import net.sourceforge.phpdt.core.JavaConventions;
18 import net.sourceforge.phpdt.core.compiler.CharOperation;
19 import net.sourceforge.phpdt.internal.codeassist.impl.AssistOptions;
20 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
21 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
22
23 public class InternalNamingConventions {
24         private static final char[] DEFAULT_NAME = "name".toCharArray(); //$NON-NLS-1$
25         
26         private static Scanner getNameScanner(CompilerOptions compilerOptions) {
27                 return
28                         new Scanner(
29                                 false /*comment*/, 
30                                 false /*whitespace*/, 
31                                 false /*nls*/, 
32 //                              compilerOptions.sourceLevel /*sourceLevel*/, 
33                                 false,
34                                 false,
35                                 null /*taskTags*/, 
36                                 null/*taskPriorities*/, false);
37                   
38         }
39         public static void suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
40                 Map options = javaProject.getOptions(true);
41                 CompilerOptions compilerOptions = new CompilerOptions(options);
42                 AssistOptions assistOptions = new AssistOptions(options);
43
44                 suggestNames(
45                         packageName,
46                         qualifiedTypeName,
47                         dim,
48                         assistOptions.argumentPrefixes,
49                         assistOptions.argumentSuffixes,
50                         excludedNames,
51                         getNameScanner(compilerOptions),
52                         requestor);
53         }
54         public static void suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames, INamingRequestor requestor) {
55                 boolean isStatic = Flags.isStatic(modifiers);
56                 
57                 Map options = javaProject.getOptions(true);
58                 CompilerOptions compilerOptions = new CompilerOptions(options);
59                 AssistOptions assistOptions = new AssistOptions(options);
60
61                 suggestNames(
62                         packageName,
63                         qualifiedTypeName,
64                         dim,
65                         isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
66                         isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes,
67                         excludedNames,
68                         getNameScanner(compilerOptions),
69                         requestor);
70         }
71         public static void suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
72                 Map options = javaProject.getOptions(true);
73                 CompilerOptions compilerOptions = new CompilerOptions(options);
74                 AssistOptions assistOptions = new AssistOptions(options);
75
76                 suggestNames(
77                         packageName,
78                         qualifiedTypeName,
79                         dim,
80                         assistOptions.localPrefixes,
81                         assistOptions.localSuffixes,
82                         excludedNames,
83                         getNameScanner(compilerOptions),
84                         requestor);
85         }
86         
87         private static void suggestNames(
88                 char[] packageName,
89                 char[] qualifiedTypeName,
90                 int dim,
91                 char[][] prefixes,
92                 char[][] suffixes,
93                 char[][] excludedNames,
94                 Scanner nameScanner,
95                 INamingRequestor requestor){
96                 
97                 if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
98                         return;
99                 
100                 char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
101         
102                 if(prefixes == null || prefixes.length == 0) {
103                         prefixes = new char[1][0];
104                 } else {
105                         int length = prefixes.length;
106                         System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length);
107                         prefixes[length] = CharOperation.NO_CHAR;
108                 }
109         
110                 if(suffixes == null || suffixes.length == 0) {
111                         suffixes = new char[1][0];
112                 } else {
113                         int length = suffixes.length;
114                         System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length);
115                         suffixes[length] = CharOperation.NO_CHAR;
116                 }
117         
118                 char[][] tempNames = null;
119         
120                 // compute variable name for base type
121 //              try{
122 //                      nameScanner.setSource(typeName);
123 //                      switch (nameScanner.getNextToken()) {
124 //                              case TerminalTokens.TokenNameint :
125 //                              case TerminalTokens.TokenNamebyte :
126 //                              case TerminalTokens.TokenNameshort :
127 //                              case TerminalTokens.TokenNamechar :
128 //                              case TerminalTokens.TokenNamelong :
129 //                              case TerminalTokens.TokenNamefloat :
130 //                              case TerminalTokens.TokenNamedouble :
131 //                              case TerminalTokens.TokenNameboolean :  
132 //                                      char[] name = computeBaseTypeNames(typeName[0], excludedNames);
133 //                                      if(name != null) {
134 //                                              tempNames =  new char[][]{name};
135 //                                      }
136 //                                      break;
137 //                      }       
138 //              } catch(InvalidInputException e){
139 //                      // ignore
140 //              }
141
142                 // compute variable name for non base type
143                 if(tempNames == null) {
144                         tempNames = computeNames(typeName);
145                 }
146         
147                 boolean acceptDefaultName = true;
148                 
149                 for (int i = 0; i < tempNames.length; i++) {
150                         char[] tempName = tempNames[i];
151                         if(dim > 0) {
152                                 int length = tempName.length;
153                                 if (tempName[length-1] == 's'){
154                                         if(tempName.length > 1 && tempName[length-2] == 's') {
155                                                 System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
156                                                 tempName[length] = 'e';
157                                                 tempName[length+1] = 's';
158                                         }
159                                 } else if(tempName[length-1] == 'y') {
160                                         System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
161                                         tempName[length-1] = 'i';
162                                         tempName[length] = 'e';
163                                         tempName[length+1] = 's';
164                                 } else {
165                                         System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length);
166                                         tempName[length] = 's';
167                                 }
168                         }
169                 
170                         for (int j = 0; j < prefixes.length; j++) {
171                                 if(prefixes[j].length > 0
172                                         && Character.isLetterOrDigit(prefixes[j][prefixes[j].length - 1])) {
173                                         tempName[0] = Character.toUpperCase(tempName[0]);
174                                 } else {
175                                         tempName[0] = Character.toLowerCase(tempName[0]);
176                                 }
177                                 char[] prefixName = CharOperation.concat(prefixes[j], tempName);
178                                 for (int k = 0; k < suffixes.length; k++) {
179                                         char[] suffixName = CharOperation.concat(prefixName, suffixes[k]);
180                                         suffixName =
181                                                 excludeNames(
182                                                         suffixName,
183                                                         prefixName,
184                                                         suffixes[k],
185                                                         excludedNames);
186                                         if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
187                                                 acceptName(suffixName, prefixes[j], suffixes[k],  j == 0, k == 0, requestor);
188                                                 acceptDefaultName = false;
189                                         } else {
190                                                 suffixName = CharOperation.concat(
191                                                         prefixName,
192                                                         String.valueOf(1).toCharArray(),
193                                                         suffixes[k]
194                                                 );
195                                                 suffixName =
196                                                         excludeNames(
197                                                                 suffixName,
198                                                                 prefixName,
199                                                                 suffixes[k],
200                                                                 excludedNames);
201                                                 if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
202                                                         acceptName(suffixName, prefixes[j], suffixes[k], j == 0, k == 0, requestor);
203                                                         acceptDefaultName = false;
204                                                 }
205                                         }
206                                 }
207                         
208                         }
209                 }
210                 // if no names were found
211                 if(acceptDefaultName) {
212                         char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excludedNames);
213                         requestor.acceptNameWithoutPrefixAndSuffix(name);
214                 }
215         }
216         
217         private static void acceptName(
218                 char[] name,
219                 char[] prefix,
220                 char[] suffix,
221                 boolean isFirstPrefix,
222                 boolean isFirstSuffix,
223                 INamingRequestor requestor) {
224                 if(prefix.length > 0 && suffix.length > 0) {
225                         requestor.acceptNameWithPrefixAndSuffix(name, isFirstPrefix, isFirstSuffix);
226                 } else if(prefix.length > 0){
227                         requestor.acceptNameWithPrefix(name, isFirstPrefix);
228                 } else if(suffix.length > 0){
229                         requestor.acceptNameWithSuffix(name, isFirstSuffix);
230                 } else {
231                         requestor.acceptNameWithoutPrefixAndSuffix(name);
232                 }
233         }
234         
235         private static char[] computeBaseTypeNames(char firstName, char[][] excludedNames){
236                 char[] name = new char[]{firstName};
237                 
238                 for(int i = 0 ; i < excludedNames.length ; i++){
239                         if(CharOperation.equals(name, excludedNames[i], false)) {
240                                 name[0]++;
241                                 if(name[0] > 'z')
242                                         name[0] = 'a';
243                                 if(name[0] == firstName)
244                                         return null;
245                                 i = 0;
246                         }       
247                 }
248                 
249                 return name;
250         }
251         
252         private static char[][] computeNames(char[] sourceName){
253                 char[][] names = new char[5][];
254                 int nameCount = 0;
255                 boolean previousIsUpperCase = false;
256                 boolean previousIsLetter = true;
257                 for(int i = sourceName.length - 1 ; i >= 0 ; i--){
258                         boolean isUpperCase = Character.isUpperCase(sourceName[i]);
259                         boolean isLetter = Character.isLetter(sourceName[i]);
260                         if(isUpperCase && !previousIsUpperCase && previousIsLetter){
261                                 char[] name = CharOperation.subarray(sourceName,i,sourceName.length);
262                                 if(name.length > 1){
263                                         if(nameCount == names.length) {
264                                                 System.arraycopy(names, 0, names = new char[nameCount * 2][], 0, nameCount);
265                                         }
266                                         name[0] = Character.toLowerCase(name[0]);
267                                         names[nameCount++] = name;
268                                 }
269                         }
270                         previousIsUpperCase = isUpperCase;
271                         previousIsLetter = isLetter;
272                 }
273                 if(nameCount == 0){
274                         names[nameCount++] = CharOperation.toLowerCase(sourceName);                             
275                 }
276                 System.arraycopy(names, 0, names = new char[nameCount][], 0, nameCount);
277                 return names;
278         }
279
280         private static char[] excludeNames(
281                 char[] suffixName,
282                 char[] prefixName,
283                 char[] suffix,
284                 char[][] excludedNames) {
285                 int count = 2;
286                 int m = 0;
287                 while (m < excludedNames.length) {
288                         if(CharOperation.equals(suffixName, excludedNames[m], false)) {
289                                 suffixName = CharOperation.concat(
290                                         prefixName,
291                                         String.valueOf(count++).toCharArray(),
292                                         suffix
293                                 );
294                                 m = 0;
295                         } else {
296                                 m++;
297                         }
298                 }
299                 return suffixName;
300         }
301 }