Skip to content

Commit 72eeb4e

Browse files
authored
Merge pull request #617 from bohdan-harniuk/fix-reindex-error-during-casting-to-xml-file
Fixed casting exception, refactored code
2 parents 95fffc7 + 5676d73 commit 72eeb4e

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

src/com/magento/idea/magento2plugin/indexes/XmlIndex.java

+46-41
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
1-
/**
1+
/*
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.indexes;
67

7-
import com.intellij.openapi.project.Project;
88
import com.intellij.openapi.vfs.VirtualFile;
9+
import com.intellij.psi.PsiFile;
910
import com.intellij.psi.PsiManager;
1011
import com.intellij.psi.search.GlobalSearchScope;
1112
import com.intellij.psi.util.PsiTreeUtil;
12-
import com.intellij.psi.xml.*;
13+
import com.intellij.psi.xml.XmlAttribute;
14+
import com.intellij.psi.xml.XmlFile;
15+
import com.intellij.psi.xml.XmlTag;
16+
import com.intellij.psi.xml.XmlTagValue;
1317
import com.intellij.util.indexing.FileBasedIndex;
1418
import com.jetbrains.php.lang.psi.elements.PhpClass;
1519
import com.magento.idea.magento2plugin.stubs.indexes.xml.PhpClassNameIndex;
16-
1720
import java.util.ArrayList;
1821
import java.util.Collection;
1922
import java.util.List;
2023

21-
public class XmlIndex {
22-
23-
private static XmlIndex INSTANCE;
24-
25-
private Project project;
24+
public final class XmlIndex {
2625

2726
private XmlIndex() {
2827
}
2928

30-
public static XmlIndex getInstance(final Project project) {
31-
if (null == INSTANCE) {
32-
INSTANCE = new XmlIndex();
33-
}
34-
INSTANCE.project = project;
35-
return INSTANCE;
36-
}
37-
38-
public static List<XmlTag> getPhpClassDeclarations(PhpClass phpClass) {
39-
40-
List<XmlTag> result = new ArrayList<>();
41-
42-
String fqn = phpClass.getPresentableFQN();
43-
44-
PsiManager psiManager = PsiManager.getInstance(phpClass.getProject());
45-
46-
Collection<VirtualFile> vfs = FileBasedIndex.getInstance()
47-
.getContainingFiles(PhpClassNameIndex.KEY, fqn, GlobalSearchScope.allScope(phpClass.getProject()));
48-
49-
for (VirtualFile vf : vfs) {
50-
XmlFile xmlFile = (XmlFile)psiManager.findFile(vf);
51-
if (xmlFile == null) {
29+
/**
30+
* Get PHP class declarations in the *.xml files.
31+
*
32+
* @param phpClass PhpClass
33+
*
34+
* @return List[XmlTag]
35+
*/
36+
public static List<XmlTag> getPhpClassDeclarations(final PhpClass phpClass) {
37+
38+
final List<XmlTag> result = new ArrayList<>();
39+
final String fqn = phpClass.getPresentableFQN();
40+
final PsiManager psiManager = PsiManager.getInstance(phpClass.getProject());
41+
final Collection<VirtualFile> vfs = FileBasedIndex.getInstance()
42+
.getContainingFiles(
43+
PhpClassNameIndex.KEY,
44+
fqn,
45+
GlobalSearchScope.allScope(phpClass.getProject())
46+
);
47+
48+
for (final VirtualFile vf : vfs) {
49+
final PsiFile psiFile = psiManager.findFile(vf);
50+
51+
if (!(psiFile instanceof XmlFile)) {
5252
continue;
5353
}
54+
final XmlFile xmlFile = (XmlFile) psiFile;
55+
final XmlTag[] xmlTags = PsiTreeUtil.getChildrenOfType(
56+
xmlFile.getFirstChild(),
57+
XmlTag.class
58+
);
5459

55-
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(xmlFile.getFirstChild(), XmlTag.class);
5660
if (xmlTags == null) {
5761
continue;
5862
}
59-
60-
for (XmlTag xmlTag: xmlTags) {
63+
for (final XmlTag xmlTag: xmlTags) {
6164
fillList(xmlTag, fqn, result);
6265
}
6366
}
6467

6568
return result;
66-
6769
}
6870

69-
private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
70-
for (XmlTag childTag: parentTag.getSubTags()) {
71-
for (XmlAttribute xmlAttribute: childTag.getAttributes()) {
71+
private static void fillList(
72+
final XmlTag parentTag,
73+
final String fqn,
74+
final List<XmlTag> list
75+
) {
76+
for (final XmlTag childTag: parentTag.getSubTags()) {
77+
for (final XmlAttribute xmlAttribute: childTag.getAttributes()) {
7278
String xmlAttributeValue = xmlAttribute.getValue();
7379
if (xmlAttributeValue != null) {
7480
xmlAttributeValue = xmlAttributeValue.startsWith("\\")
@@ -78,14 +84,13 @@ private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
7884
}
7985
}
8086
}
81-
XmlTagValue childTagValue = childTag.getValue();
87+
final XmlTagValue childTagValue = childTag.getValue();
8288
String tagValue = childTagValue.getTrimmedText();
8389
tagValue = tagValue.startsWith("\\") ? tagValue.substring(1) : tagValue;
90+
8491
if (!tagValue.isEmpty() && tagValue.startsWith(fqn)) {
8592
list.add(childTag);
8693
}
87-
88-
8994
fillList(childTag, fqn, list);
9095
}
9196
}

0 commit comments

Comments
 (0)