From 80c6c8da42e28e47f50afadeee462b7d087c418c Mon Sep 17 00:00:00 2001 From: Daniel Espendiller Date: Sat, 5 Aug 2023 13:21:07 +0200 Subject: [PATCH] provide cache for form extension resolving --- .../form/dict/FormTypeClass.java | 10 +--- .../symfony2plugin/form/util/FormUtil.java | 55 +++++++++++++------ .../tests/form/util/FormUtilTest.java | 4 +- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/dict/FormTypeClass.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/dict/FormTypeClass.java index 6559d5ac7..1c3135733 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/dict/FormTypeClass.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/dict/FormTypeClass.java @@ -34,13 +34,7 @@ public String getName() { } @Nullable - public PhpClass getPhpClass() { - return phpClass; - } - - @Nullable - public PhpClass getPhpClass(Project project) { - + public PhpClass getPhpClass(@NotNull Project project) { if(phpClass != null) { return phpClass; } @@ -49,7 +43,7 @@ public PhpClass getPhpClass(Project project) { return null; } - return PhpElementsUtil.getClass(project, this.phpClassName); + return this.phpClass = PhpElementsUtil.getClass(project, this.phpClassName); } @NotNull diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java index a5af4dfec..3a3d2bd06 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java @@ -4,10 +4,10 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; import com.intellij.psi.impl.source.xml.XmlDocumentImpl; -import com.intellij.psi.util.PsiElementFilter; -import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.*; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlFile; import com.intellij.psi.xml.XmlTag; @@ -48,6 +48,7 @@ * @author Daniel Espendiller */ public class FormUtil { + private static final Key>> FORM_EXTENSION_TYPES = new Key<>("SYMFONY_FORM_EXTENSION_TYPES"); final public static String ABSTRACT_FORM_INTERFACE = "\\Symfony\\Component\\Form\\FormTypeInterface"; final public static String FORM_EXTENSION_INTERFACE = "\\Symfony\\Component\\Form\\FormTypeExtensionInterface"; @@ -79,7 +80,7 @@ public static PhpClass getFormTypeToClass(Project project, @Nullable String form return new FormTypeCollector(project).collect().getFormTypeToClass(formType); } - public static Collection getFormTypeLookupElements(Project project) { + public static Collection getFormTypeLookupElements(@NotNull Project project) { Collection lookupElements = new ArrayList<>(); @@ -89,7 +90,7 @@ public static Collection getFormTypeLookupElements(Project projec String name = entry.getValue().getName(); String typeText = entry.getValue().getPhpClassName(); - PhpClass phpClass = entry.getValue().getPhpClass(); + PhpClass phpClass = entry.getValue().getPhpClass(project); if(phpClass != null) { typeText = phpClass.getName(); } @@ -359,7 +360,7 @@ public static Set getTags(XmlTag serviceTag) { XmlAttribute attribute = serviceSubTag.getAttribute("name"); if(attribute != null) { String tagName = attribute.getValue(); - if(tagName != null && StringUtils.isNotBlank(tagName)) { + if(StringUtils.isNotBlank(tagName)) { tags.add(tagName); } } @@ -372,20 +373,42 @@ public static Set getTags(XmlTag serviceTag) { @NotNull public static Map getFormTypeClasses(@NotNull Project project) { + Collection nameAndClass = CachedValuesManager.getManager(project).getCachedValue( + project, + FORM_EXTENSION_TYPES, + new CachedValueProvider<>() { + @Override + public @NotNull Result> compute() { + Collection items = new ArrayList<>(); - Map map = new HashMap<>(); + for (PhpClass phpClass : PhpIndex.getInstance(project).getAllSubclasses(ABSTRACT_FORM_INTERFACE)) { + if (!isValidFormPhpClass(phpClass)) { + continue; + } - for(PhpClass phpClass: PhpIndex.getInstance(project).getAllSubclasses(ABSTRACT_FORM_INTERFACE)) { - if(!isValidFormPhpClass(phpClass)) { - continue; - } + String name = FormUtil.getFormNameOfPhpClass(phpClass); + if (name == null) { + continue; + } - String name = FormUtil.getFormNameOfPhpClass(phpClass); - if (name == null) { - continue; - } + items.add(new String[]{name, phpClass.getFQN()}); - map.put(name, new FormTypeClass(name, phpClass, EnumFormTypeSource.INDEX)); + } + + return Result.create(items, PsiModificationTracker.MODIFICATION_COUNT); + } + }, + false + ); + + Map map = new HashMap<>(); + + for (String[] item : nameAndClass) { + PhpClass phpClass = PhpElementsUtil.getClassInterface(project, item[1]); + if (phpClass != null) { + String name = item[0]; + map.put(name, new FormTypeClass(name, phpClass, EnumFormTypeSource.INDEX)); + } } return map; @@ -460,7 +483,7 @@ public PhpClass getFormTypeClass(String formTypeName) { return null; } - return forms.get(formTypeName).getPhpClass(); + return forms.get(formTypeName).getPhpClass(project); } public Map getFormTypesMap() { diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormUtilTest.java b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormUtilTest.java index 4436867e0..52f86bdf4 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormUtilTest.java +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormUtilTest.java @@ -53,10 +53,10 @@ public void testGetFormTypeClassOnParameter() { public void testGetFormTypeClasses() { Map formTypeClasses = FormUtil.getFormTypeClasses(getProject()); assertNotNull(formTypeClasses.get("foo_type")); - assertEquals(formTypeClasses.get("foo_type").getPhpClass().getFQN(), "\\Form\\FormType\\Foo"); + assertEquals(formTypeClasses.get("foo_type").getPhpClass(getProject()).getFQN(), "\\Form\\FormType\\Foo"); assertNotNull(formTypeClasses.get("foo_bar")); - assertEquals(formTypeClasses.get("foo_bar").getPhpClass().getFQN(), "\\Form\\FormType\\FooBar"); + assertEquals(formTypeClasses.get("foo_bar").getPhpClass(getProject()).getFQN(), "\\Form\\FormType\\FooBar"); } public void testGetFormAliases() {