Skip to content

Commit

Permalink
Merge pull request #2202 from Haehnchen/feature/cache-form-types
Browse files Browse the repository at this point in the history
provide cache for form extension resolving
  • Loading branch information
Haehnchen authored Aug 5, 2023
2 parents e4dbeb9 + 6f4fc4f commit 321bb3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@
*/
public class FormTypeClass {

final private String name;
private final String name;
private PhpClass phpClass;
private String phpClassName;
final private EnumFormTypeSource source;

public FormTypeClass(@NotNull String name, @NotNull PhpClass phpClass, @NotNull EnumFormTypeSource source) {
this.name = name;
this.phpClass = phpClass;
this.source = source;
}
private final String phpClassName;
private final EnumFormTypeSource source;

public FormTypeClass(@NotNull String name, @NotNull String phpClassName, @NotNull EnumFormTypeSource source) {
this.name = name;
Expand All @@ -34,22 +28,12 @@ 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;
}

if(this.phpClassName == null) {
return null;
}

return PhpElementsUtil.getClass(project, this.phpClassName);
return this.phpClass = PhpElementsUtil.getClass(project, this.phpClassName);
}

@NotNull
Expand All @@ -60,5 +44,4 @@ public EnumFormTypeSource getSource() {
public String getPhpClassName() {
return phpClassName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +48,7 @@
* @author Daniel Espendiller <[email protected]>
*/
public class FormUtil {
private static final Key<CachedValue<Collection<String[]>>> 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";
Expand Down Expand Up @@ -79,7 +80,7 @@ public static PhpClass getFormTypeToClass(Project project, @Nullable String form
return new FormTypeCollector(project).collect().getFormTypeToClass(formType);
}

public static Collection<LookupElement> getFormTypeLookupElements(Project project) {
public static Collection<LookupElement> getFormTypeLookupElements(@NotNull Project project) {

Collection<LookupElement> lookupElements = new ArrayList<>();

Expand All @@ -89,7 +90,7 @@ public static Collection<LookupElement> 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();
}
Expand Down Expand Up @@ -359,7 +360,7 @@ public static Set<String> 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);
}
}
Expand All @@ -372,20 +373,38 @@ public static Set<String> getTags(XmlTag serviceTag) {

@NotNull
public static Map<String, FormTypeClass> getFormTypeClasses(@NotNull Project project) {
Collection<String[]> nameAndClass = CachedValuesManager.getManager(project).getCachedValue(
project,
FORM_EXTENSION_TYPES,
new CachedValueProvider<>() {
@Override
public @NotNull Result<Collection<String[]>> compute() {
Collection<String[]> items = new ArrayList<>();

Map<String, FormTypeClass> 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()});

}

return Result.create(items, PsiModificationTracker.MODIFICATION_COUNT);
}
},
false
);

Map<String, FormTypeClass> map = new HashMap<>();

map.put(name, new FormTypeClass(name, phpClass, EnumFormTypeSource.INDEX));
for (String[] item : nameAndClass) {
map.put(item[0], new FormTypeClass(item[0], item[1], EnumFormTypeSource.INDEX));
}

return map;
Expand Down Expand Up @@ -460,7 +479,7 @@ public PhpClass getFormTypeClass(String formTypeName) {
return null;
}

return forms.get(formTypeName).getPhpClass();
return forms.get(formTypeName).getPhpClass(project);
}

public Map<String, FormTypeClass> getFormTypesMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void testGetFormTypeClassOnParameter() {
public void testGetFormTypeClasses() {
Map<String, FormTypeClass> 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() {
Expand Down

0 comments on commit 321bb3a

Please sign in to comment.