1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .indexes ;
6
7
7
- import com .intellij .openapi .project .Project ;
8
8
import com .intellij .openapi .vfs .VirtualFile ;
9
+ import com .intellij .psi .PsiFile ;
9
10
import com .intellij .psi .PsiManager ;
10
11
import com .intellij .psi .search .GlobalSearchScope ;
11
12
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 ;
13
17
import com .intellij .util .indexing .FileBasedIndex ;
14
18
import com .jetbrains .php .lang .psi .elements .PhpClass ;
15
19
import com .magento .idea .magento2plugin .stubs .indexes .xml .PhpClassNameIndex ;
16
-
17
20
import java .util .ArrayList ;
18
21
import java .util .Collection ;
19
22
import java .util .List ;
20
23
21
- public class XmlIndex {
22
-
23
- private static XmlIndex INSTANCE ;
24
-
25
- private Project project ;
24
+ public final class XmlIndex {
26
25
27
26
private XmlIndex () {
28
27
}
29
28
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 )) {
52
52
continue ;
53
53
}
54
+ final XmlFile xmlFile = (XmlFile ) psiFile ;
55
+ final XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (
56
+ xmlFile .getFirstChild (),
57
+ XmlTag .class
58
+ );
54
59
55
- XmlTag xmlTags [] = PsiTreeUtil .getChildrenOfType (xmlFile .getFirstChild (), XmlTag .class );
56
60
if (xmlTags == null ) {
57
61
continue ;
58
62
}
59
-
60
- for (XmlTag xmlTag : xmlTags ) {
63
+ for (final XmlTag xmlTag : xmlTags ) {
61
64
fillList (xmlTag , fqn , result );
62
65
}
63
66
}
64
67
65
68
return result ;
66
-
67
69
}
68
70
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 ()) {
72
78
String xmlAttributeValue = xmlAttribute .getValue ();
73
79
if (xmlAttributeValue != null ) {
74
80
xmlAttributeValue = xmlAttributeValue .startsWith ("\\ " )
@@ -78,14 +84,13 @@ private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
78
84
}
79
85
}
80
86
}
81
- XmlTagValue childTagValue = childTag .getValue ();
87
+ final XmlTagValue childTagValue = childTag .getValue ();
82
88
String tagValue = childTagValue .getTrimmedText ();
83
89
tagValue = tagValue .startsWith ("\\ " ) ? tagValue .substring (1 ) : tagValue ;
90
+
84
91
if (!tagValue .isEmpty () && tagValue .startsWith (fqn )) {
85
92
list .add (childTag );
86
93
}
87
-
88
-
89
94
fillList (childTag , fqn , list );
90
95
}
91
96
}
0 commit comments