|
12 | 12 | import com.jetbrains.php.PhpIndex;
|
13 | 13 | import com.jetbrains.php.lang.psi.elements.Field;
|
14 | 14 | import com.jetbrains.php.lang.psi.elements.Method;
|
| 15 | +import com.jetbrains.php.lang.psi.elements.Parameter; |
15 | 16 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
16 | 17 | import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
|
17 | 18 | import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.AttributeValueInterface;
|
@@ -289,51 +290,76 @@ public static ServiceTypeHint getYamlConstructorTypeHint(@NotNull PsiElement psi
|
289 | 290 | */
|
290 | 291 | @Nullable
|
291 | 292 | public static ServiceTypeHint getYamlConstructorTypeHint(@NotNull YAMLScalar yamlScalar, @NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) {
|
292 |
| - PsiElement context = yamlScalar.getContext(); |
293 |
| - if(!(context instanceof YAMLSequenceItem)) { |
294 |
| - return null; |
295 |
| - } |
296 |
| - |
297 |
| - final YAMLSequenceItem sequenceItem = (YAMLSequenceItem) context; |
298 |
| - if (!(sequenceItem.getContext() instanceof YAMLSequence)) { |
299 |
| - return null; |
300 |
| - } |
| 293 | + // arguments: ['$foobar': '@foo'] |
301 | 294 |
|
302 |
| - final YAMLSequence yamlArray = (YAMLSequence) sequenceItem.getContext(); |
303 |
| - if(!(yamlArray.getContext() instanceof YAMLKeyValue)) { |
304 |
| - return null; |
305 |
| - } |
306 |
| - |
307 |
| - final YAMLKeyValue yamlKeyValue = (YAMLKeyValue) yamlArray.getContext(); |
308 |
| - if(!yamlKeyValue.getKeyText().equals("arguments")) { |
309 |
| - return null; |
310 |
| - } |
311 |
| - |
312 |
| - YAMLMapping parentMapping = yamlKeyValue.getParentMapping(); |
313 |
| - if(parentMapping == null) { |
314 |
| - return null; |
315 |
| - } |
316 |
| - |
317 |
| - String serviceId = getServiceClassFromServiceMapping(parentMapping); |
318 |
| - if(StringUtils.isBlank(serviceId)) { |
319 |
| - return null; |
320 |
| - } |
321 |
| - |
322 |
| - PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(yamlScalar.getProject(), serviceId, lazyServiceCollector); |
323 |
| - if(serviceClass == null) { |
324 |
| - return null; |
| 295 | + PsiElement context = yamlScalar.getContext(); |
| 296 | + if(context instanceof YAMLKeyValue) { |
| 297 | + String key = ((YAMLKeyValue) context).getKeyText(); |
| 298 | + if(key.startsWith("$")) { |
| 299 | + PsiElement yamlMapping = context.getParent(); |
| 300 | + if(yamlMapping instanceof YAMLMapping) { |
| 301 | + PsiElement yamlKeyValue = yamlMapping.getParent(); |
| 302 | + if(yamlKeyValue instanceof YAMLKeyValue) { |
| 303 | + String keyText = ((YAMLKeyValue) yamlKeyValue).getKeyText(); |
| 304 | + if(keyText.equals("arguments")) { |
| 305 | + YAMLMapping parentMapping = ((YAMLKeyValue) yamlKeyValue).getParentMapping(); |
| 306 | + if(parentMapping != null) { |
| 307 | + String serviceId = getServiceClassFromServiceMapping(parentMapping); |
| 308 | + if(StringUtils.isNotBlank(serviceId)) { |
| 309 | + PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(yamlScalar.getProject(), serviceId, lazyServiceCollector); |
| 310 | + if(serviceClass != null) { |
| 311 | + Method constructor = serviceClass.getConstructor(); |
| 312 | + if(constructor != null) { |
| 313 | + Parameter[] parameters = constructor.getParameters(); |
| 314 | + for (int i = 0; i < parameters.length; i++) { |
| 315 | + Parameter parameter = parameters[i]; |
| 316 | + if (key.equals("$" + parameter.getName())) { |
| 317 | + return new ServiceTypeHint(constructor, i, yamlScalar); |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | + } |
325 | 328 | }
|
326 | 329 |
|
327 |
| - Method constructor = serviceClass.getConstructor(); |
328 |
| - if(constructor == null) { |
329 |
| - return null; |
| 330 | + // arguments: ['@foobar'] |
| 331 | + if(context instanceof YAMLSequenceItem) { |
| 332 | + YAMLSequenceItem sequenceItem = (YAMLSequenceItem) context; |
| 333 | + PsiElement yamlSequenceItem = sequenceItem.getContext(); |
| 334 | + if (yamlSequenceItem instanceof YAMLSequence) { |
| 335 | + YAMLSequence yamlArray = (YAMLSequence) sequenceItem.getContext(); |
| 336 | + PsiElement yamlKeyValue = yamlArray.getContext(); |
| 337 | + if(yamlKeyValue instanceof YAMLKeyValue) { |
| 338 | + YAMLKeyValue yamlKeyValueArguments = (YAMLKeyValue) yamlKeyValue; |
| 339 | + if(yamlKeyValueArguments.getKeyText().equals("arguments")) { |
| 340 | + YAMLMapping parentMapping = yamlKeyValueArguments.getParentMapping(); |
| 341 | + if(parentMapping != null) { |
| 342 | + String serviceId = getServiceClassFromServiceMapping(parentMapping); |
| 343 | + if(StringUtils.isNotBlank(serviceId)) { |
| 344 | + PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(yamlScalar.getProject(), serviceId, lazyServiceCollector); |
| 345 | + if(serviceClass != null) { |
| 346 | + Method constructor = serviceClass.getConstructor(); |
| 347 | + if(constructor != null) { |
| 348 | + return new ServiceTypeHint( |
| 349 | + constructor, |
| 350 | + PsiElementUtils.getPrevSiblingsOfType(sequenceItem, PlatformPatterns.psiElement(YAMLSequenceItem.class)).size(), |
| 351 | + yamlScalar |
| 352 | + ); |
| 353 | + } |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + } |
| 358 | + } |
| 359 | + } |
330 | 360 | }
|
331 | 361 |
|
332 |
| - return new ServiceTypeHint( |
333 |
| - constructor, |
334 |
| - PsiElementUtils.getPrevSiblingsOfType(sequenceItem, PlatformPatterns.psiElement(YAMLSequenceItem.class)).size(), |
335 |
| - yamlScalar |
336 |
| - ); |
| 362 | + return null; |
337 | 363 | }
|
338 | 364 |
|
339 | 365 | /**
|
@@ -395,13 +421,13 @@ public static ServiceTypeHint getXmlConstructorTypeHint(@NotNull PsiElement psiE
|
395 | 421 | // service/argument[id]
|
396 | 422 | String serviceDefName = XmlHelper.getClassFromServiceDefinition(serviceTag);
|
397 | 423 | if(serviceDefName != null) {
|
398 |
| - PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceDefName); |
| 424 | + PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceDefName, lazyServiceCollector); |
399 | 425 |
|
400 | 426 | // check type hint on constructor
|
401 | 427 | if(phpClass != null) {
|
402 | 428 | Method constructor = phpClass.getConstructor();
|
403 | 429 | if(constructor != null) {
|
404 |
| - return new ServiceTypeHint(constructor, getArgumentIndex(argumentTag), psiElement); |
| 430 | + return new ServiceTypeHint(constructor, XmlHelper.getArgumentIndex(argumentTag, constructor), psiElement); |
405 | 431 | }
|
406 | 432 | }
|
407 | 433 | }
|
@@ -440,19 +466,15 @@ public static ServiceTypeHint getXmlCallTypeHint(@NotNull PsiElement psiElement,
|
440 | 466 |
|
441 | 467 | // get service class
|
442 | 468 | if(serviceTag != null && "service".equals(serviceTag.getName())) {
|
443 |
| - XmlAttribute classAttribute = serviceTag.getAttribute("class"); |
444 |
| - if(classAttribute != null) { |
445 |
| - |
446 |
| - String serviceDefName = classAttribute.getValue(); |
447 |
| - if(serviceDefName != null) { |
448 |
| - PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceDefName); |
449 |
| - |
450 |
| - // finally check method type hint |
451 |
| - if(phpClass != null) { |
452 |
| - Method method = phpClass.findMethodByName(methodName); |
453 |
| - if(method != null) { |
454 |
| - return new ServiceTypeHint(method, getArgumentIndex(currentXmlTag), psiElement); |
455 |
| - } |
| 469 | + String serviceDefName = XmlHelper.getClassFromServiceDefinition(serviceTag); |
| 470 | + if(serviceDefName != null) { |
| 471 | + PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceDefName, lazyServiceCollector); |
| 472 | + |
| 473 | + // finally check method type hint |
| 474 | + if(phpClass != null) { |
| 475 | + Method method = phpClass.findMethodByName(methodName); |
| 476 | + if(method != null) { |
| 477 | + return new ServiceTypeHint(method, XmlHelper.getArgumentIndex(currentXmlTag, method), psiElement); |
456 | 478 | }
|
457 | 479 | }
|
458 | 480 | }
|
@@ -506,21 +528,6 @@ public static int getServiceUsage(@NotNull Project project, @NotNull String id)
|
506 | 528 | return usage;
|
507 | 529 | }
|
508 | 530 |
|
509 |
| - private static int getArgumentIndex(@NotNull XmlTag xmlTag) { |
510 |
| - |
511 |
| - PsiElement psiElement = xmlTag; |
512 |
| - int index = 0; |
513 |
| - |
514 |
| - while (psiElement != null) { |
515 |
| - psiElement = psiElement.getPrevSibling(); |
516 |
| - if(psiElement instanceof XmlTag && "argument".equalsIgnoreCase(((XmlTag) psiElement).getName())) { |
517 |
| - index++; |
518 |
| - } |
519 |
| - } |
520 |
| - |
521 |
| - return index; |
522 |
| - } |
523 |
| - |
524 | 531 | public static boolean isLowerPriority(String name) {
|
525 | 532 | for(String lowerName: LOWER_PRIORITY) {
|
526 | 533 | if(name.contains(lowerName)) {
|
|
0 commit comments