|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.stubs.indexes; |
| 2 | + |
| 3 | +import com.intellij.util.indexing.*; |
| 4 | +import com.intellij.util.io.DataExternalizer; |
| 5 | +import com.intellij.util.io.EnumeratorStringDescriptor; |
| 6 | +import com.intellij.util.io.KeyDescriptor; |
| 7 | +import com.jetbrains.php.lang.PhpFileType; |
| 8 | +import com.jetbrains.php.lang.psi.PhpFile; |
| 9 | +import com.jetbrains.php.lang.psi.PhpPsiUtil; |
| 10 | +import com.jetbrains.php.lang.psi.elements.*; |
| 11 | +import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionArgument; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.externalizer.StringListDataExternalizer; |
| 13 | +import org.apache.commons.lang3.StringUtils; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * Generalized index for PHP attributes on classes and methods |
| 20 | + * |
| 21 | + * Maps attribute FQNs to their targets with additional data: |
| 22 | + * - Key: Attribute FQN (e.g., "\Twig\Attribute\AsTwigFilter", "\Symfony\Component\Console\Attribute\AsCommand") |
| 23 | + * - Value: List<String> where: |
| 24 | + * [0] = Class FQN (e.g., "App\Twig\AppExtension") |
| 25 | + * [1] = Method name (for method-level attributes) or attribute parameter (e.g., filter name) |
| 26 | + * [2+] = Additional data (extensible for future use) |
| 27 | + * |
| 28 | + * Examples: |
| 29 | + * - AsTwigFilter: ["App\Twig\AppExtension", "formatProductNumber", "product_number_filter"] |
| 30 | + * - AsCommand: ["App\Command\CreateUserCommand"] |
| 31 | + * |
| 32 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 33 | + */ |
| 34 | +public class PhpAttributeIndex extends FileBasedIndexExtension<String, List<String>> { |
| 35 | + public static final ID<String, List<String>> KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.php_attribute.index"); |
| 36 | + |
| 37 | + @Override |
| 38 | + public @NotNull ID<String, List<String>> getName() { |
| 39 | + return KEY; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public @NotNull DataIndexer<String, List<String>, FileContent> getIndexer() { |
| 44 | + return new PhpAttributeIndexer(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public @NotNull KeyDescriptor<String> getKeyDescriptor() { |
| 49 | + return EnumeratorStringDescriptor.INSTANCE; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public @NotNull DataExternalizer<List<String>> getValueExternalizer() { |
| 54 | + return StringListDataExternalizer.INSTANCE; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public @NotNull FileBasedIndex.InputFilter getInputFilter() { |
| 59 | + return virtualFile -> virtualFile.getFileType() == PhpFileType.INSTANCE; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean dependsOnFileContent() { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int getVersion() { |
| 69 | + return 6; |
| 70 | + } |
| 71 | + |
| 72 | + public static class PhpAttributeIndexer implements DataIndexer<String, List<String>, FileContent> { |
| 73 | + // Twig attributes on methods |
| 74 | + private static final Set<String> TWIG_METHOD_ATTRIBUTES = Set.of( |
| 75 | + "\\Twig\\Attribute\\AsTwigFilter", |
| 76 | + "\\Twig\\Attribute\\AsTwigFunction", |
| 77 | + "\\Twig\\Attribute\\AsTwigTest" |
| 78 | + ); |
| 79 | + |
| 80 | + // Symfony console command attributes on classes |
| 81 | + private static final String AS_COMMAND_ATTRIBUTE = "\\Symfony\\Component\\Console\\Attribute\\AsCommand"; |
| 82 | + |
| 83 | + @Override |
| 84 | + public @NotNull Map<String, List<String>> map(@NotNull FileContent inputData) { |
| 85 | + Map<String, List<String>> result = new HashMap<>(); |
| 86 | + if (!(inputData.getPsiFile() instanceof PhpFile phpFile)) { |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + for (PhpClass phpClass : PhpPsiUtil.findAllClasses(phpFile)) { |
| 91 | + // Process class-level attributes |
| 92 | + processClassAttributes(phpClass, result); |
| 93 | + |
| 94 | + // Process method-level attributes |
| 95 | + for (Method method : phpClass.getOwnMethods()) { |
| 96 | + processMethodAttributes(phpClass, method, result); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Process attributes on class level (e.g., AsCommand on Command classes) |
| 105 | + */ |
| 106 | + private void processClassAttributes(@NotNull PhpClass phpClass, @NotNull Map<String, List<String>> result) { |
| 107 | + for (PhpAttribute attribute : phpClass.getAttributes()) { |
| 108 | + String attributeFqn = attribute.getFQN(); |
| 109 | + if (attributeFqn == null) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + // Index AsCommand attribute on class |
| 114 | + // Key: Attribute FQN |
| 115 | + // Value: [class FQN] |
| 116 | + if (AS_COMMAND_ATTRIBUTE.equals(attributeFqn)) { |
| 117 | + String classFqn = StringUtils.stripStart(phpClass.getFQN(), "\\"); |
| 118 | + result.put(attributeFqn, List.of(classFqn)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Process attributes on method level (Twig attributes) |
| 125 | + */ |
| 126 | + private void processMethodAttributes(@NotNull PhpClass phpClass, @NotNull Method method, @NotNull Map<String, List<String>> result) { |
| 127 | + for (PhpAttribute attribute : method.getAttributes()) { |
| 128 | + String attributeFqn = attribute.getFQN(); |
| 129 | + if (attributeFqn == null) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + // Index Twig attributes on methods |
| 134 | + // Key: Attribute FQN |
| 135 | + // Value: [class FQN, method name, filter/function/test name] |
| 136 | + if (TWIG_METHOD_ATTRIBUTES.contains(attributeFqn)) { |
| 137 | + String nameAttribute = extractFirstAttributeParameter(attribute); |
| 138 | + if (nameAttribute != null) { |
| 139 | + String classFqn = StringUtils.stripStart(phpClass.getFQN(), "\\"); |
| 140 | + result.put(attributeFqn, List.of(classFqn, method.getName(), nameAttribute)); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Extract the first parameter from a PHP attribute during indexing |
| 148 | + * We can't use PhpPsiAttributesUtil because it doesn't work reliably during indexing |
| 149 | + */ |
| 150 | + private String extractFirstAttributeParameter(@NotNull PhpAttribute attribute) { |
| 151 | + for (PhpAttribute.PhpAttributeArgument argument : attribute.getArguments()) { |
| 152 | + PhpExpectedFunctionArgument funcArg = argument.getArgument(); |
| 153 | + if (funcArg != null && funcArg.getArgumentIndex() == 0) { |
| 154 | + // Try to get the value as a string |
| 155 | + String value = funcArg.getValue(); |
| 156 | + if (value != null) { |
| 157 | + // Remove quotes if present |
| 158 | + return value.replaceAll("^['\"]|['\"]$", ""); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + return null; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments