10
10
import com .jetbrains .php .lang .psi .elements .*;
11
11
import com .jetbrains .php .phpunit .PhpUnitUtil ;
12
12
import fr .adrienbrault .idea .symfony2plugin .Symfony2Icons ;
13
+ import fr .adrienbrault .idea .symfony2plugin .form .FormOptionGotoCompletionRegistrar ;
14
+ import fr .adrienbrault .idea .symfony2plugin .form .visitor .FormOptionLookupVisitor ;
13
15
import fr .adrienbrault .idea .symfony2plugin .form .dict .*;
16
+ import fr .adrienbrault .idea .symfony2plugin .form .visitor .FormOptionVisitor ;
14
17
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
15
18
import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
16
19
import fr .adrienbrault .idea .symfony2plugin .util .dict .ServiceUtil ;
21
24
22
25
import java .util .*;
23
26
27
+ /**
28
+ * @author Daniel Espendiller <daniel@espendiller.net>
29
+ */
24
30
public class FormOptionsUtil {
25
31
26
32
private static final String EXTENDED_TYPE_METHOD = "getExtendedType" ;
@@ -92,7 +98,8 @@ private static void visitExtendedTypeMethod(List<String> formTypeNamesList, List
92
98
93
99
}
94
100
95
- public static Map <String , FormOption > getFormExtensionKeys (Project project , String ... formTypeNames ) {
101
+ @ NotNull
102
+ public static Map <String , FormOption > getFormExtensionKeys (@ NotNull Project project , @ NotNull String ... formTypeNames ) {
96
103
97
104
List <FormClass > typeClasses = FormOptionsUtil .getExtendedTypeClasses (project , formTypeNames );
98
105
Map <String , FormOption > extensionClassMap = new HashMap <String , FormOption >();
@@ -208,18 +215,36 @@ private static void getFormViewVarsAttachKeys(Set<String> stringSet, FieldRefere
208
215
}
209
216
}
210
217
211
- public static Map <String , String > getFormDefaultKeys (Project project , String formTypeName ) {
212
- return getFormDefaultKeys (project , formTypeName , new HashMap <String , String >(), new FormUtil .FormTypeCollector (project ).collect (), 0 );
218
+
219
+ @ Deprecated
220
+ public static Map <String , String > getFormDefaultKeys (@ NotNull Project project , @ NotNull String formTypeName ) {
221
+ final Map <String , String > items = new HashMap <String , String >();
222
+
223
+ getFormDefaultKeys (project , formTypeName , new HashMap <String , String >(), new FormUtil .FormTypeCollector (project ).collect (), 0 , new FormOptionVisitor () {
224
+ @ Override
225
+ public void visit (@ NotNull PsiElement psiElement , @ NotNull String option , @ NotNull FormClass formClass , @ NotNull FormOptionEnum optionEnum ) {
226
+ String presentableFQN = formClass .getPhpClass ().getPresentableFQN ();
227
+ if (presentableFQN != null ) {
228
+ items .put (option , presentableFQN );
229
+ }
230
+ }
231
+ });
232
+
233
+ return items ;
213
234
}
214
235
215
- private static Map <String , String > getFormDefaultKeys (Project project , String formTypeName , HashMap <String , String > defaultValues , FormUtil .FormTypeCollector collector , int depth ) {
236
+ public static void getFormDefaultKeys (@ NotNull Project project , @ NotNull String formTypeName , @ NotNull FormOptionVisitor visitor ) {
237
+ getFormDefaultKeys (project , formTypeName , new HashMap <String , String >(), new FormUtil .FormTypeCollector (project ).collect (), 0 , visitor );
238
+ }
239
+
240
+ private static Map <String , String > getFormDefaultKeys (Project project , String formTypeName , HashMap <String , String > defaultValues , FormUtil .FormTypeCollector collector , int depth , @ NotNull FormOptionVisitor visitor ) {
216
241
217
242
PhpClass phpClass = collector .getFormTypeToClass (formTypeName );
218
243
if (phpClass == null ) {
219
244
return defaultValues ;
220
245
}
221
246
222
- attachOnDefaultOptions (project , defaultValues , phpClass );
247
+ getDefaultOptions (project , phpClass , new FormClass ( FormClassEnum . FORM_TYPE , phpClass , false ), visitor );
223
248
224
249
// recursive search for parent form types
225
250
PsiElement parentMethod = PhpElementsUtil .getClassMethod (phpClass , "getParent" );
@@ -228,7 +253,7 @@ private static Map<String, String> getFormDefaultKeys(Project project, String fo
228
253
if (phpReturn != null ) {
229
254
PhpPsiElement returnValue = phpReturn .getFirstPsiChild ();
230
255
if (returnValue instanceof StringLiteralExpression ) {
231
- getFormDefaultKeys (project , ((StringLiteralExpression ) returnValue ).getContents (), defaultValues , collector , ++depth );
256
+ getFormDefaultKeys (project , ((StringLiteralExpression ) returnValue ).getContents (), defaultValues , collector , ++depth , visitor );
232
257
}
233
258
234
259
}
@@ -237,24 +262,26 @@ private static Map<String, String> getFormDefaultKeys(Project project, String fo
237
262
return defaultValues ;
238
263
}
239
264
240
- /**
241
- * use getDefaultOptions
242
- */
243
- @ Deprecated
244
- private static void attachOnDefaultOptions (@ NotNull Project project , @ NotNull Map <String , String > defaultValues , @ NotNull PhpClass phpClass ) {
245
- // @TODO!!! really remove this :)
246
- for (Map .Entry <String , FormOption > entry : getDefaultOptions (project , phpClass , new FormClass (FormClassEnum .FORM_TYPE , phpClass , false )).entrySet ()) {
247
- String presentableFQN = entry .getValue ().getFormClass ().getPhpClass ().getPresentableFQN ();
248
- if (presentableFQN != null ) {
249
- defaultValues .put (entry .getKey (), presentableFQN );
250
- }
251
- }
252
- }
253
-
254
265
@ NotNull
255
266
private static Map <String , FormOption > getDefaultOptions (@ NotNull Project project , @ NotNull PhpClass phpClass , @ NotNull FormClass formClass ) {
267
+ final Map <String , FormOption > options = new HashMap <String , FormOption >();
268
+
269
+ getDefaultOptions (project , phpClass , formClass , new FormOptionVisitor () {
270
+ @ Override
271
+ public void visit (@ NotNull PsiElement psiElement , @ NotNull String option , @ NotNull FormClass formClass , @ NotNull FormOptionEnum optionEnum ) {
272
+ // append REQUIRED, if we already know this value
273
+ if (options .containsKey (option )) {
274
+ options .get (option ).addOptionEnum (optionEnum );
275
+ } else {
276
+ options .put (option , new FormOption (option , formClass , optionEnum ));
277
+ }
278
+ }
279
+ });
280
+
281
+ return options ;
282
+ }
256
283
257
- Map < String , FormOption > options = new HashMap < String , FormOption >();
284
+ private static void getDefaultOptions ( @ NotNull Project project , @ NotNull PhpClass phpClass , @ NotNull FormClass formClass , @ NotNull FormOptionVisitor visitor ) {
258
285
259
286
for (String methodName : new String [] {"setDefaultOptions" , "configureOptions" }) {
260
287
@@ -269,8 +296,8 @@ private static Map<String, FormOption> getDefaultOptions(@NotNull Project projec
269
296
if (PhpElementsUtil .isEqualMethodReferenceName (methodReference , "setDefaults" )) {
270
297
PsiElement [] parameters = methodReference .getParameters ();
271
298
if (parameters .length > 0 && parameters [0 ] instanceof ArrayCreationExpression ) {
272
- for (String key : PhpElementsUtil .getArrayCreationKeys ((ArrayCreationExpression ) parameters [0 ])) {
273
- options . put ( key , new FormOption ( key , formClass ) );
299
+ for (Map . Entry < String , PsiElement > entry : PhpElementsUtil .getArrayCreationKeyMap ((ArrayCreationExpression ) parameters [0 ]). entrySet ( )) {
300
+ visitor . visit ( entry . getValue (), entry . getKey () , formClass , FormOptionEnum . DEFAULT );
274
301
}
275
302
}
276
303
@@ -280,12 +307,8 @@ private static Map<String, FormOption> getDefaultOptions(@NotNull Project projec
280
307
if (PhpElementsUtil .isEqualMethodReferenceName (methodReference , currentMethod )) {
281
308
PsiElement [] parameters = methodReference .getParameters ();
282
309
if (parameters .length > 0 && parameters [0 ] instanceof ArrayCreationExpression ) {
283
- for (String stringValue : PhpElementsUtil .getArrayValuesAsString ((ArrayCreationExpression ) parameters [0 ])) {
284
- if (options .containsKey (stringValue )) {
285
- options .get (stringValue ).addOptionEnum (FormOptionEnum .getEnum (currentMethod ));
286
- } else {
287
- options .put (stringValue , new FormOption (stringValue , formClass , FormOptionEnum .REQUIRED ));
288
- }
310
+ for (Map .Entry <String , PsiElement > entry : PhpElementsUtil .getArrayValuesAsMap ((ArrayCreationExpression ) parameters [0 ]).entrySet ()) {
311
+ visitor .visit (entry .getValue (), entry .getKey (), formClass , FormOptionEnum .getEnum (currentMethod ));
289
312
}
290
313
}
291
314
break ;
@@ -301,7 +324,7 @@ private static Map<String, FormOption> getDefaultOptions(@NotNull Project projec
301
324
PhpClass phpClassInner = ((Method ) parentMethod ).getContainingClass ();
302
325
if (phpClassInner != null ) {
303
326
// @TODO only use setDefaultOptions, recursive call get setDefaults again
304
- options . putAll ( getDefaultOptions (project , phpClassInner , formClass ) );
327
+ getDefaultOptions (project , phpClassInner , formClass , visitor );
305
328
}
306
329
}
307
330
}
@@ -310,7 +333,6 @@ private static Map<String, FormOption> getDefaultOptions(@NotNull Project projec
310
333
311
334
}
312
335
313
- return options ;
314
336
}
315
337
316
338
/**
@@ -370,52 +392,33 @@ public static Collection<LookupElement> getFormExtensionKeysLookupElements(Proje
370
392
return lookupElements ;
371
393
}
372
394
395
+ @ Deprecated
373
396
@ NotNull
374
- public static Collection <PsiElement > getDefaultOptionTargets (StringLiteralExpression element , String formType ) {
375
-
376
- Map <String , String > defaultOptions = FormOptionsUtil .getFormDefaultKeys (element .getProject (), formType );
377
- String value = element .getContents ();
397
+ public static Collection <PsiElement > getDefaultOptionTargets (@ NotNull StringLiteralExpression element , @ NotNull String formType ) {
378
398
379
- if (!defaultOptions .containsKey (value )) {
399
+ final String value = element .getContents ();
400
+ if (StringUtils .isBlank (value )) {
380
401
return Collections .emptySet ();
381
402
}
382
403
383
- String className = defaultOptions . get ( value );
404
+ final Collection < PsiElement > psiElements = new ArrayList < PsiElement >( );
384
405
385
- // @TODO: use class core
386
- PsiElement [] psiElements = PhpElementsUtil .getPsiElementsBySignature (element .getProject (), "#M#C\\ " + className + ".setDefaultOptions" );
387
- if (psiElements .length == 0 ) {
388
- return Collections .emptySet ();
389
- }
390
-
391
- PsiElement keyValue = PhpElementsUtil .findArrayKeyValueInsideReference (psiElements [0 ], "setDefaults" , value );
392
- if (keyValue != null ) {
393
- return Arrays .asList (psiElements );
394
- }
406
+ FormOptionsUtil .getFormDefaultKeys (element .getProject (), formType , new FormOptionVisitor () {
407
+ @ Override
408
+ public void visit (@ NotNull PsiElement psiElement , @ NotNull String option , @ NotNull FormClass formClass , @ NotNull FormOptionEnum optionEnum ) {
409
+ if (option .equals (value )) {
410
+ psiElements .add (psiElement );
411
+ }
412
+ }
413
+ });
395
414
396
- return Collections . emptySet () ;
415
+ return psiElements ;
397
416
}
398
417
399
- public static Collection < LookupElement > getDefaultOptionLookupElements ( Project project , String formType ) {
400
-
418
+ @ NotNull
419
+ public static Collection < LookupElement > getDefaultOptionLookupElements ( @ NotNull Project project , @ NotNull String formType ) {
401
420
Collection <LookupElement > lookupElements = new ArrayList <LookupElement >();
402
-
403
- for (Map .Entry <String , String > extension : FormOptionsUtil .getFormDefaultKeys (project , formType ).entrySet ()) {
404
- String typeText = extension .getValue ();
405
- if (typeText .lastIndexOf ("\\ " ) != -1 ) {
406
- typeText = typeText .substring (typeText .lastIndexOf ("\\ " ) + 1 );
407
- }
408
-
409
- if (typeText .endsWith ("Type" )) {
410
- typeText = typeText .substring (0 , typeText .length () - 4 );
411
- }
412
-
413
- lookupElements .add (LookupElementBuilder .create (extension .getKey ())
414
- .withTypeText (typeText )
415
- .withIcon (Symfony2Icons .FORM_OPTION )
416
- );
417
- }
418
-
421
+ FormOptionsUtil .getFormDefaultKeys (project , formType , new FormOptionLookupVisitor (lookupElements ));
419
422
return lookupElements ;
420
423
}
421
424
0 commit comments