Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
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.*;
import com.intellij.util.io.DataExternalizer;
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;
Expand All @@ -36,7 +36,7 @@ public class EventAnnotationStubIndex extends FileBasedIndexExtension<String, Di

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

@NotNull
@Override
Expand All @@ -55,7 +55,19 @@ public DataIndexer<String, DispatcherEvent, FileContent> 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;
};
Expand Down Expand Up @@ -91,73 +103,55 @@ public int getVersion() {
return 2;
}

private class MyPsiRecursiveElementWalkingVisitor extends PsiRecursiveElementVisitor {

private final Map<String, DispatcherEvent> map;

MyPsiRecursiveElementWalkingVisitor(Map<String, DispatcherEvent> map) {
this.map = map;
private void visitPhpDocTag(@NotNull PhpDocTag element, @NotNull Map<String, DispatcherEvent> 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());
}
}

Expand Down