Skip to content

Commit 46892db

Browse files
authored
Merge pull request #2087 from Haehnchen/feature/event-index-recursive-replace
replace recursive visting of elements inside "@event" indexer with direct level visting
2 parents 1101e4e + 27ef646 commit 46892db

File tree

1 file changed

+53
-59
lines changed

1 file changed

+53
-59
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/EventAnnotationStubIndex.java

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
import com.intellij.patterns.PlatformPatterns;
44
import com.intellij.psi.PsiElement;
55
import com.intellij.psi.PsiFile;
6-
import com.intellij.psi.PsiRecursiveElementVisitor;
76
import com.intellij.psi.util.PsiTreeUtil;
87
import com.intellij.util.ObjectUtils;
98
import com.intellij.util.indexing.*;
109
import com.intellij.util.io.DataExternalizer;
1110
import com.intellij.util.io.EnumeratorStringDescriptor;
1211
import com.intellij.util.io.KeyDescriptor;
1312
import com.jetbrains.php.lang.PhpFileType;
13+
import com.jetbrains.php.lang.documentation.phpdoc.PhpDocUtil;
1414
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
1515
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
1616
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
1717
import com.jetbrains.php.lang.parser.PhpElementTypes;
18-
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
19-
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
18+
import com.jetbrains.php.lang.psi.PhpFile;
19+
import com.jetbrains.php.lang.psi.elements.*;
2020
import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl;
2121
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
2222
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.DispatcherEvent;
@@ -36,7 +36,7 @@ public class EventAnnotationStubIndex extends FileBasedIndexExtension<String, Di
3636

3737
public static final ID<String, DispatcherEvent> KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.events_annotation");
3838
private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor();
39-
private static ObjectStreamDataExternalizer<DispatcherEvent> EXTERNALIZER = new ObjectStreamDataExternalizer<>();
39+
private static final ObjectStreamDataExternalizer<DispatcherEvent> EXTERNALIZER = new ObjectStreamDataExternalizer<>();
4040

4141
@NotNull
4242
@Override
@@ -55,7 +55,19 @@ public DataIndexer<String, DispatcherEvent, FileContent> getIndexer() {
5555
return map;
5656
}
5757

58-
psiFile.accept(new MyPsiRecursiveElementWalkingVisitor(map));
58+
for (PhpNamedElement topLevelElement : ((PhpFile) psiFile).getTopLevelDefs().values()) {
59+
if (topLevelElement instanceof PhpClass clazz) {
60+
for (Field ownField : clazz.getOwnFields()) {
61+
PhpDocComment docComment = ownField.getDocComment();
62+
if (docComment != null) {
63+
PhpDocUtil.processTagElementsByName(docComment, null, docTag -> {
64+
visitPhpDocTag(docTag, map);
65+
return true;
66+
});
67+
}
68+
}
69+
}
70+
}
5971

6072
return map;
6173
};
@@ -91,73 +103,55 @@ public int getVersion() {
91103
return 2;
92104
}
93105

94-
private class MyPsiRecursiveElementWalkingVisitor extends PsiRecursiveElementVisitor {
95-
96-
private final Map<String, DispatcherEvent> map;
97-
98-
MyPsiRecursiveElementWalkingVisitor(Map<String, DispatcherEvent> map) {
99-
this.map = map;
106+
private void visitPhpDocTag(@NotNull PhpDocTag element, @NotNull Map<String, DispatcherEvent> map) {
107+
String name = StringUtils.stripStart(element.getName(), "@");
108+
if(!"Event".equalsIgnoreCase(name)) {
109+
return;
100110
}
101111

102-
103-
@Override
104-
public void visitElement(PsiElement element) {
105-
if ((element instanceof PhpDocTag)) {
106-
visitPhpDocTag((PhpDocTag) element);
107-
}
108-
super.visitElement(element);
112+
PhpDocComment phpDocComment = ObjectUtils.tryCast(element.getParent(), PhpDocComment.class);
113+
if(phpDocComment == null) {
114+
return;
109115
}
110116

111-
private void visitPhpDocTag(PhpDocTag element) {
112-
String name = StringUtils.stripStart(element.getName(), "@");
113-
if(!"Event".equalsIgnoreCase(name)) {
114-
return;
115-
}
116-
117-
PhpDocComment phpDocComment = ObjectUtils.tryCast(element.getParent(), PhpDocComment.class);
118-
if(phpDocComment == null) {
119-
return;
120-
}
121-
122-
PhpPsiElement nextPsiSibling = phpDocComment.getNextPsiSibling();
123-
if(nextPsiSibling == null || nextPsiSibling.getNode().getElementType() != PhpElementTypes.CLASS_CONSTANTS) {
124-
return;
125-
}
117+
PhpPsiElement nextPsiSibling = phpDocComment.getNextPsiSibling();
118+
if(nextPsiSibling == null || nextPsiSibling.getNode().getElementType() != PhpElementTypes.CLASS_CONSTANTS) {
119+
return;
120+
}
126121

127-
ClassConstImpl childOfAnyType = PsiTreeUtil.findChildOfAnyType(nextPsiSibling, ClassConstImpl.class);
128-
if(childOfAnyType == null) {
129-
return;
130-
}
122+
ClassConstImpl childOfAnyType = PsiTreeUtil.findChildOfAnyType(nextPsiSibling, ClassConstImpl.class);
123+
if(childOfAnyType == null) {
124+
return;
125+
}
131126

132-
PsiElement defaultValue = childOfAnyType.getDefaultValue();
133-
if(!(defaultValue instanceof StringLiteralExpression)) {
134-
return;
135-
}
127+
PsiElement defaultValue = childOfAnyType.getDefaultValue();
128+
if(!(defaultValue instanceof StringLiteralExpression)) {
129+
return;
130+
}
136131

137-
String contents = ((StringLiteralExpression) defaultValue).getContents();
132+
String contents = ((StringLiteralExpression) defaultValue).getContents();
138133

139-
String fqn = StringUtils.stripStart(childOfAnyType.getFQN(), "\\");
134+
String fqn = StringUtils.stripStart(childOfAnyType.getFQN(), "\\");
140135

141-
map.put(contents, new DispatcherEvent(
142-
fqn,
143-
findClassInstance(phpDocComment, element))
144-
);
145-
}
136+
map.put(contents, new DispatcherEvent(
137+
fqn,
138+
findClassInstance(phpDocComment, element))
139+
);
140+
}
146141

147-
private String findClassInstance(@NotNull PhpDocComment phpDocComment, @NotNull PhpDocTag phpDocTag) {
148-
PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
149-
if(phpDocAttributeList instanceof PhpPsiElement) {
150-
PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(phpDocAttributeList, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocString));
151-
if(childrenOfType instanceof StringLiteralExpression) {
152-
String contents = StringUtils.stripStart(((StringLiteralExpression) childrenOfType).getContents(), "\\");
153-
if(StringUtils.isNotBlank(contents) && contents.length() < 350) {
154-
return contents;
155-
}
142+
private String findClassInstance(@NotNull PhpDocComment phpDocComment, @NotNull PhpDocTag phpDocTag) {
143+
PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
144+
if(phpDocAttributeList instanceof PhpPsiElement) {
145+
PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(phpDocAttributeList, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocString));
146+
if(childrenOfType instanceof StringLiteralExpression) {
147+
String contents = StringUtils.stripStart(((StringLiteralExpression) childrenOfType).getContents(), "\\");
148+
if(StringUtils.isNotBlank(contents) && contents.length() < 350) {
149+
return contents;
156150
}
157151
}
158-
159-
return EventDispatcherUtil.extractEventClassInstance(phpDocComment.getText());
160152
}
153+
154+
return EventDispatcherUtil.extractEventClassInstance(phpDocComment.getText());
161155
}
162156
}
163157

0 commit comments

Comments
 (0)