-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring of ObjectManager::findBy* and support more possible repos…
- Loading branch information
Showing
13 changed files
with
365 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,12 @@ | |
import com.intellij.psi.*; | ||
import com.intellij.util.ProcessingContext; | ||
import com.jetbrains.php.lang.PhpLanguage; | ||
import com.jetbrains.php.lang.parser.PhpElementTypes; | ||
import com.jetbrains.php.lang.psi.elements.*; | ||
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; | ||
import fr.adrienbrault.idea.symfony2plugin.dic.ConstraintPropertyReference; | ||
import fr.adrienbrault.idea.symfony2plugin.dic.ServiceReference; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.EntityReference; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.ModelFieldReference; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineTypes; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.TemplateReference; | ||
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; | ||
|
@@ -20,8 +18,6 @@ | |
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
|
@@ -151,38 +147,6 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No | |
} | ||
); | ||
|
||
psiReferenceRegistrar.registerReferenceProvider( | ||
// @TODO: implement global pattern for array parameters | ||
PlatformPatterns.psiElement(StringLiteralExpression.class).withParent( | ||
PlatformPatterns.or( | ||
PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE), | ||
PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY) | ||
) | ||
).inside(PlatformPatterns.psiElement(ParameterList.class)), | ||
new PsiReferenceProvider() { | ||
@NotNull | ||
@Override | ||
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) { | ||
|
||
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.ArrayParameterMatcher(psiElement, 0) | ||
.withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findOneBy") | ||
.withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findBy") | ||
.match(); | ||
|
||
if(methodMatchParameter == null) { | ||
return new PsiReference[0]; | ||
} | ||
|
||
Collection<PhpClass> phpClasses = PhpElementsUtil.getClassFromPhpTypeSetArrayClean(psiElement.getProject(), methodMatchParameter.getMethodReference().getType().getTypes()); | ||
if(phpClasses.size() == 0) { | ||
return new PsiReference[0]; | ||
} | ||
|
||
return new PsiReference[]{ new ModelFieldReference((StringLiteralExpression) psiElement, phpClasses)}; | ||
} | ||
} | ||
); | ||
|
||
psiReferenceRegistrar.registerReferenceProvider( | ||
PlatformPatterns.psiElement(StringLiteralExpression.class), | ||
new PsiReferenceProvider() { | ||
|
60 changes: 0 additions & 60 deletions
60
src/fr/adrienbrault/idea/symfony2plugin/doctrine/ModelFieldReference.java
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
...lt/idea/symfony2plugin/doctrine/metadata/ObjectRepositoryFindGotoCompletionRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.doctrine.metadata; | ||
|
||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.psi.PsiElement; | ||
import com.jetbrains.php.lang.psi.elements.MethodReference; | ||
import com.jetbrains.php.lang.psi.elements.PhpClass; | ||
import com.jetbrains.php.lang.psi.elements.PhpExpression; | ||
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; | ||
import com.jetbrains.php.lang.psi.resolve.types.PhpType; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.utils.GotoCompletionUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelField; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelFieldLookupElement; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelInterface; | ||
import fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.util.DoctrineMetadataUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; | ||
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class ObjectRepositoryFindGotoCompletionRegistrar implements GotoCompletionRegistrar { | ||
public void register(GotoCompletionRegistrarParameter registrar) { | ||
|
||
// "@var $om \Doctrine\Common\Persistence\ObjectManager" | ||
// "$om->getRepository('Foo\Bar')->" + s + "(['foo' => 'foo', '<caret>' => 'foo'])" | ||
registrar.register(PhpElementsUtil.getParameterListArrayValuePattern(), psiElement -> { | ||
PsiElement context = psiElement.getContext(); | ||
if (!(context instanceof StringLiteralExpression)) { | ||
return null; | ||
} | ||
|
||
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.ArrayParameterMatcher(context, 0) | ||
.withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findOneBy") | ||
.withSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findBy") | ||
.match(); | ||
|
||
if(methodMatchParameter != null) { | ||
MethodReference methodReference = methodMatchParameter.getMethodReference(); | ||
|
||
// extract from type provide on completion: | ||
// $foo->getRepository('MODEL')->findBy() | ||
Collection<PhpClass> phpClasses = PhpElementsUtil.getClassFromPhpTypeSetArrayClean(psiElement.getProject(), methodReference.getType().getTypes()); | ||
|
||
// resolve every direct repository instance $this->findBy() | ||
// or direct repository instance $repository->findBy() | ||
if(phpClasses.size() == 0) { | ||
PhpExpression classReference = methodReference.getClassReference(); | ||
if(classReference != null) { | ||
PhpType type = classReference.getType(); | ||
for (String s : type.getTypes()) { | ||
// dont visit type providers | ||
if(PhpType.isUnresolved(s)) { | ||
continue; | ||
} | ||
|
||
for (DoctrineModelInterface doctrineModel : DoctrineMetadataUtil.findMetadataModelForRepositoryClass(psiElement.getProject(), s)) { | ||
phpClasses.addAll(PhpElementsUtil.getClassesInterface(psiElement.getProject(), doctrineModel.getClassName())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if(phpClasses.size() == 0) { | ||
return null; | ||
} | ||
|
||
return new MyArrayFieldMetadataGotoCompletionRegistrar(psiElement, phpClasses); | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
|
||
private static class MyArrayFieldMetadataGotoCompletionRegistrar extends GotoCompletionProvider { | ||
@NotNull | ||
private final Collection<PhpClass> phpClasses; | ||
|
||
MyArrayFieldMetadataGotoCompletionRegistrar(@NotNull PsiElement element, @NotNull Collection<PhpClass> phpClasses) { | ||
super(element); | ||
this.phpClasses = phpClasses; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Collection<LookupElement> getLookupElements() { | ||
List<LookupElement> results = new ArrayList<>(); | ||
|
||
phpClasses.forEach(phpClass -> | ||
results.addAll(EntityHelper.getModelFields(phpClass).stream() | ||
.map((Function<DoctrineModelField, LookupElement>) DoctrineModelFieldLookupElement::new) | ||
.collect(Collectors.toList()) | ||
) | ||
); | ||
|
||
return results; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Collection<PsiElement> getPsiTargets(PsiElement element) { | ||
String content = GotoCompletionUtil.getTextValueForElement(element); | ||
if(content == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
Collection<PsiElement> results = new ArrayList<>(); | ||
|
||
phpClasses.forEach(phpClass -> | ||
results.addAll(Arrays.asList(EntityHelper.getModelFieldTargets(phpClass, content))) | ||
); | ||
|
||
return results; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.