From 27ef64601008956b5e2df23f16550144fa8e2c32 Mon Sep 17 00:00:00 2001 From: Daniel Espendiller Date: Fri, 24 Mar 2023 17:40:22 +0100 Subject: [PATCH] replace recursive visting of elements inside "@Event" indexer with direct level visting --- .../indexes/EventAnnotationStubIndex.java | 112 +++++++++--------- 1 file changed, 53 insertions(+), 59 deletions(-) diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/EventAnnotationStubIndex.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/EventAnnotationStubIndex.java index bdafcc88a..545d7cb4b 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/EventAnnotationStubIndex.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/EventAnnotationStubIndex.java @@ -3,7 +3,6 @@ import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiRecursiveElementVisitor; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.ObjectUtils; import com.intellij.util.indexing.*; @@ -11,12 +10,13 @@ import com.intellij.util.io.EnumeratorStringDescriptor; import com.intellij.util.io.KeyDescriptor; import com.jetbrains.php.lang.PhpFileType; +import com.jetbrains.php.lang.documentation.phpdoc.PhpDocUtil; import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes; import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment; import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag; import com.jetbrains.php.lang.parser.PhpElementTypes; -import com.jetbrains.php.lang.psi.elements.PhpPsiElement; -import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; +import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.*; import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; import fr.adrienbrault.idea.symfony2plugin.stubs.dict.DispatcherEvent; @@ -36,7 +36,7 @@ public class EventAnnotationStubIndex extends FileBasedIndexExtension KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.events_annotation"); private final KeyDescriptor myKeyDescriptor = new EnumeratorStringDescriptor(); - private static ObjectStreamDataExternalizer EXTERNALIZER = new ObjectStreamDataExternalizer<>(); + private static final ObjectStreamDataExternalizer EXTERNALIZER = new ObjectStreamDataExternalizer<>(); @NotNull @Override @@ -55,7 +55,19 @@ public DataIndexer getIndexer() { return map; } - psiFile.accept(new MyPsiRecursiveElementWalkingVisitor(map)); + for (PhpNamedElement topLevelElement : ((PhpFile) psiFile).getTopLevelDefs().values()) { + if (topLevelElement instanceof PhpClass clazz) { + for (Field ownField : clazz.getOwnFields()) { + PhpDocComment docComment = ownField.getDocComment(); + if (docComment != null) { + PhpDocUtil.processTagElementsByName(docComment, null, docTag -> { + visitPhpDocTag(docTag, map); + return true; + }); + } + } + } + } return map; }; @@ -91,73 +103,55 @@ public int getVersion() { return 2; } - private class MyPsiRecursiveElementWalkingVisitor extends PsiRecursiveElementVisitor { - - private final Map map; - - MyPsiRecursiveElementWalkingVisitor(Map map) { - this.map = map; + private void visitPhpDocTag(@NotNull PhpDocTag element, @NotNull Map map) { + String name = StringUtils.stripStart(element.getName(), "@"); + if(!"Event".equalsIgnoreCase(name)) { + return; } - - @Override - public void visitElement(PsiElement element) { - if ((element instanceof PhpDocTag)) { - visitPhpDocTag((PhpDocTag) element); - } - super.visitElement(element); + PhpDocComment phpDocComment = ObjectUtils.tryCast(element.getParent(), PhpDocComment.class); + if(phpDocComment == null) { + return; } - private void visitPhpDocTag(PhpDocTag element) { - String name = StringUtils.stripStart(element.getName(), "@"); - if(!"Event".equalsIgnoreCase(name)) { - return; - } - - PhpDocComment phpDocComment = ObjectUtils.tryCast(element.getParent(), PhpDocComment.class); - if(phpDocComment == null) { - return; - } - - PhpPsiElement nextPsiSibling = phpDocComment.getNextPsiSibling(); - if(nextPsiSibling == null || nextPsiSibling.getNode().getElementType() != PhpElementTypes.CLASS_CONSTANTS) { - return; - } + PhpPsiElement nextPsiSibling = phpDocComment.getNextPsiSibling(); + if(nextPsiSibling == null || nextPsiSibling.getNode().getElementType() != PhpElementTypes.CLASS_CONSTANTS) { + return; + } - ClassConstImpl childOfAnyType = PsiTreeUtil.findChildOfAnyType(nextPsiSibling, ClassConstImpl.class); - if(childOfAnyType == null) { - return; - } + ClassConstImpl childOfAnyType = PsiTreeUtil.findChildOfAnyType(nextPsiSibling, ClassConstImpl.class); + if(childOfAnyType == null) { + return; + } - PsiElement defaultValue = childOfAnyType.getDefaultValue(); - if(!(defaultValue instanceof StringLiteralExpression)) { - return; - } + PsiElement defaultValue = childOfAnyType.getDefaultValue(); + if(!(defaultValue instanceof StringLiteralExpression)) { + return; + } - String contents = ((StringLiteralExpression) defaultValue).getContents(); + String contents = ((StringLiteralExpression) defaultValue).getContents(); - String fqn = StringUtils.stripStart(childOfAnyType.getFQN(), "\\"); + String fqn = StringUtils.stripStart(childOfAnyType.getFQN(), "\\"); - map.put(contents, new DispatcherEvent( - fqn, - findClassInstance(phpDocComment, element)) - ); - } + map.put(contents, new DispatcherEvent( + fqn, + findClassInstance(phpDocComment, element)) + ); + } - private String findClassInstance(@NotNull PhpDocComment phpDocComment, @NotNull PhpDocTag phpDocTag) { - PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList)); - if(phpDocAttributeList instanceof PhpPsiElement) { - PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(phpDocAttributeList, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocString)); - if(childrenOfType instanceof StringLiteralExpression) { - String contents = StringUtils.stripStart(((StringLiteralExpression) childrenOfType).getContents(), "\\"); - if(StringUtils.isNotBlank(contents) && contents.length() < 350) { - return contents; - } + private String findClassInstance(@NotNull PhpDocComment phpDocComment, @NotNull PhpDocTag phpDocTag) { + PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList)); + if(phpDocAttributeList instanceof PhpPsiElement) { + PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(phpDocAttributeList, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocString)); + if(childrenOfType instanceof StringLiteralExpression) { + String contents = StringUtils.stripStart(((StringLiteralExpression) childrenOfType).getContents(), "\\"); + if(StringUtils.isNotBlank(contents) && contents.length() < 350) { + return contents; } } - - return EventDispatcherUtil.extractEventClassInstance(phpDocComment.getText()); } + + return EventDispatcherUtil.extractEventClassInstance(phpDocComment.getText()); } }