|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.doctrine.metadata; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 4 | +import com.intellij.psi.PsiElement; |
| 5 | +import com.jetbrains.php.lang.psi.elements.MethodReference; |
| 6 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 7 | +import com.jetbrains.php.lang.psi.elements.PhpExpression; |
| 8 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 9 | +import com.jetbrains.php.lang.psi.resolve.types.PhpType; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.utils.GotoCompletionUtil; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelField; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelFieldLookupElement; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelInterface; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.util.DoctrineMetadataUtil; |
| 19 | +import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 29 | + */ |
| 30 | +public class ObjectRepositoryFindGotoCompletionRegistrar implements GotoCompletionRegistrar { |
| 31 | + public void register(GotoCompletionRegistrarParameter registrar) { |
| 32 | + |
| 33 | + // "@var $om \Doctrine\Common\Persistence\ObjectManager" |
| 34 | + // "$om->getRepository('Foo\Bar')->" + s + "(['foo' => 'foo', '<caret>' => 'foo'])" |
| 35 | + registrar.register(PhpElementsUtil.getParameterListArrayValuePattern(), psiElement -> { |
| 36 | + PsiElement context = psiElement.getContext(); |
| 37 | + if (!(context instanceof StringLiteralExpression)) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.ArrayParameterMatcher(context, 0) |
| 42 | + .withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findOneBy") |
| 43 | + .withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findBy") |
| 44 | + .match(); |
| 45 | + |
| 46 | + if(methodMatchParameter != null) { |
| 47 | + MethodReference methodReference = methodMatchParameter.getMethodReference(); |
| 48 | + |
| 49 | + // extract from type provide on completion: |
| 50 | + // $foo->getRepository('MODEL')->findBy() |
| 51 | + Collection<PhpClass> phpClasses = PhpElementsUtil.getClassFromPhpTypeSetArrayClean(psiElement.getProject(), methodReference.getType().getTypes()); |
| 52 | + |
| 53 | + // resolve every direct repository instance $this->findBy() |
| 54 | + // or direct repository instance $repository->findBy() |
| 55 | + if(phpClasses.size() == 0) { |
| 56 | + PhpExpression classReference = methodReference.getClassReference(); |
| 57 | + if(classReference != null) { |
| 58 | + PhpType type = classReference.getType(); |
| 59 | + for (String s : type.getTypes()) { |
| 60 | + // dont visit type providers |
| 61 | + if(PhpType.isUnresolved(s)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + for (DoctrineModelInterface doctrineModel : DoctrineMetadataUtil.findMetadataModelForRepositoryClass(psiElement.getProject(), s)) { |
| 66 | + phpClasses.addAll(PhpElementsUtil.getClassesInterface(psiElement.getProject(), doctrineModel.getClassName())); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if(phpClasses.size() == 0) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return new MyArrayFieldMetadataGotoCompletionRegistrar(psiElement, phpClasses); |
| 77 | + } |
| 78 | + |
| 79 | + return null; |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private static class MyArrayFieldMetadataGotoCompletionRegistrar extends GotoCompletionProvider { |
| 84 | + @NotNull |
| 85 | + private final Collection<PhpClass> phpClasses; |
| 86 | + |
| 87 | + MyArrayFieldMetadataGotoCompletionRegistrar(@NotNull PsiElement element, @NotNull Collection<PhpClass> phpClasses) { |
| 88 | + super(element); |
| 89 | + this.phpClasses = phpClasses; |
| 90 | + } |
| 91 | + |
| 92 | + @NotNull |
| 93 | + @Override |
| 94 | + public Collection<LookupElement> getLookupElements() { |
| 95 | + List<LookupElement> results = new ArrayList<>(); |
| 96 | + |
| 97 | + phpClasses.forEach(phpClass -> |
| 98 | + results.addAll(EntityHelper.getModelFields(phpClass).stream() |
| 99 | + .map((Function<DoctrineModelField, LookupElement>) DoctrineModelFieldLookupElement::new) |
| 100 | + .collect(Collectors.toList()) |
| 101 | + ) |
| 102 | + ); |
| 103 | + |
| 104 | + return results; |
| 105 | + } |
| 106 | + |
| 107 | + @NotNull |
| 108 | + @Override |
| 109 | + public Collection<PsiElement> getPsiTargets(PsiElement element) { |
| 110 | + String content = GotoCompletionUtil.getTextValueForElement(element); |
| 111 | + if(content == null) { |
| 112 | + return Collections.emptyList(); |
| 113 | + } |
| 114 | + |
| 115 | + Collection<PsiElement> results = new ArrayList<>(); |
| 116 | + |
| 117 | + phpClasses.forEach(phpClass -> |
| 118 | + results.addAll(Arrays.asList(EntityHelper.getModelFieldTargets(phpClass, content))) |
| 119 | + ); |
| 120 | + |
| 121 | + return results; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments