|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.stubs.indexes; |
| 2 | + |
| 3 | +import com.intellij.psi.PsiElement; |
| 4 | +import com.intellij.psi.PsiFile; |
| 5 | +import com.intellij.psi.PsiRecursiveElementVisitor; |
| 6 | +import com.intellij.psi.util.PsiTreeUtil; |
| 7 | +import com.intellij.util.indexing.*; |
| 8 | +import com.intellij.util.io.DataExternalizer; |
| 9 | +import com.intellij.util.io.EnumeratorStringDescriptor; |
| 10 | +import com.intellij.util.io.KeyDescriptor; |
| 11 | +import com.jetbrains.php.lang.PhpFileType; |
| 12 | +import com.jetbrains.php.lang.psi.PhpFile; |
| 13 | +import com.jetbrains.php.lang.psi.elements.*; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.externalizer.StringSetDataExternalizer; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 16 | +import gnu.trove.THashMap; |
| 17 | +import org.apache.commons.lang.StringUtils; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +/** |
| 26 | + * public function configureOptions(OptionsResolver $resolver) |
| 27 | + * { |
| 28 | + * $resolver->setDefaults(['data_class' => XXX]); |
| 29 | + * $resolver->setDefault('data_class', XXX); |
| 30 | + * } |
| 31 | + * |
| 32 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 33 | + */ |
| 34 | +public class FormDataClassStubIndex extends FileBasedIndexExtension<String, Set<String>> { |
| 35 | + public static final ID<String, Set<String>> KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.form_data_class"); |
| 36 | + private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor(); |
| 37 | + |
| 38 | + @NotNull |
| 39 | + @Override |
| 40 | + public ID<String, Set<String>> getName() { |
| 41 | + return KEY; |
| 42 | + } |
| 43 | + |
| 44 | + @NotNull |
| 45 | + @Override |
| 46 | + public DataIndexer<String, Set<String>, FileContent> getIndexer() { |
| 47 | + return inputData -> { |
| 48 | + Map<String, Set<String>> map = new THashMap<>(); |
| 49 | + |
| 50 | + PsiFile psiFile = inputData.getPsiFile(); |
| 51 | + if(!(psiFile instanceof PhpFile)) { |
| 52 | + return map; |
| 53 | + } |
| 54 | + |
| 55 | + psiFile.accept(new PsiRecursiveElementVisitor() { |
| 56 | + @Override |
| 57 | + public void visitElement(@NotNull PsiElement element) { |
| 58 | + if (element instanceof MethodReference) { |
| 59 | + String phpClassFqn = null; |
| 60 | + |
| 61 | + String name = ((MethodReference) element).getName(); |
| 62 | + if ("setDefault".equals(name) && ((MethodReference) element).getType().getTypes().stream().anyMatch(s -> s.toLowerCase().contains("optionsresolver"))) { |
| 63 | + // $resolver->setDefault('data_class', XXX); |
| 64 | + |
| 65 | + ParameterList parameterList = ((MethodReference) element).getParameterList(); |
| 66 | + if (parameterList != null) { |
| 67 | + PsiElement parameter = parameterList.getParameter(0); |
| 68 | + if (parameter instanceof StringLiteralExpression) { |
| 69 | + String contents = ((StringLiteralExpression) parameter).getContents(); |
| 70 | + if ("data_class".equals(contents)) { |
| 71 | + PsiElement parameter1 = parameterList.getParameter(1); |
| 72 | + if (parameter1 != null) { |
| 73 | + phpClassFqn = getString(parameter1); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } else if ("setDefaults".equals(name) && ((MethodReference) element).getType().getTypes().stream().anyMatch(s -> s.toLowerCase().contains("optionsresolver"))) { |
| 79 | + // $resolver->setDefaults(['data_class' => XXX]); |
| 80 | + |
| 81 | + ParameterList parameterList = ((MethodReference) element).getParameterList(); |
| 82 | + if (parameterList != null) { |
| 83 | + PsiElement parameter = parameterList.getParameter(0); |
| 84 | + if (parameter instanceof ArrayCreationExpression) { |
| 85 | + PhpPsiElement dataClassPsiElement = PhpElementsUtil.getArrayValue((ArrayCreationExpression) parameter, "data_class"); |
| 86 | + if (dataClassPsiElement != null) { |
| 87 | + phpClassFqn = getString(dataClassPsiElement); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (phpClassFqn != null) { |
| 94 | + Method methodScope = PsiTreeUtil.getParentOfType(element, Method.class); |
| 95 | + if (methodScope != null) { |
| 96 | + PhpClass parentOfType = methodScope.getContainingClass(); |
| 97 | + if (parentOfType != null) { |
| 98 | + map.putIfAbsent(phpClassFqn, new HashSet<>()); |
| 99 | + map.get(phpClassFqn).add(parentOfType.getFQN()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + super.visitElement(element); |
| 106 | + } |
| 107 | + |
| 108 | + @Nullable |
| 109 | + private String getString(@NotNull PsiElement parameter) { |
| 110 | + if (parameter instanceof ClassConstantReference) { |
| 111 | + String classConstantPhpFqn = PhpElementsUtil.getClassConstantPhpFqn((ClassConstantReference) parameter); |
| 112 | + if (StringUtils.isNotBlank(classConstantPhpFqn)) { |
| 113 | + return "\\" + StringUtils.stripStart(classConstantPhpFqn, "\\"); |
| 114 | + } |
| 115 | + } else if (parameter instanceof StringLiteralExpression) { |
| 116 | + String contents1 = ((StringLiteralExpression) parameter).getContents(); |
| 117 | + if (StringUtils.isNotBlank(contents1)) { |
| 118 | + return "\\" + StringUtils.stripStart(contents1, "\\"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + return map; |
| 127 | + }; |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + @NotNull |
| 132 | + @Override |
| 133 | + public KeyDescriptor<String> getKeyDescriptor() { |
| 134 | + return this.myKeyDescriptor; |
| 135 | + } |
| 136 | + |
| 137 | + @NotNull |
| 138 | + @Override |
| 139 | + public DataExternalizer<Set<String>> getValueExternalizer() { |
| 140 | + return new StringSetDataExternalizer(); |
| 141 | + } |
| 142 | + |
| 143 | + @NotNull |
| 144 | + @Override |
| 145 | + public FileBasedIndex.InputFilter getInputFilter() { |
| 146 | + return file -> file.getFileType() == PhpFileType.INSTANCE; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean dependsOnFileContent() { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public int getVersion() { |
| 156 | + return 1; |
| 157 | + } |
| 158 | +} |
0 commit comments