5
5
6
6
package com .magento .idea .magento2plugin .reference .provider ;
7
7
8
+ import com .intellij .openapi .project .Project ;
8
9
import com .intellij .openapi .util .TextRange ;
9
10
import com .intellij .psi .PsiElement ;
10
11
import com .intellij .psi .PsiReference ;
11
12
import com .intellij .psi .PsiReferenceProvider ;
13
+ import com .intellij .psi .stubs .StubIndex ;
12
14
import com .intellij .util .ProcessingContext ;
13
15
import com .jetbrains .php .PhpIndex ;
14
16
import com .jetbrains .php .lang .psi .elements .PhpClass ;
15
17
import com .jetbrains .php .lang .psi .elements .PhpNamespace ;
18
+ import com .jetbrains .php .lang .psi .stubs .indexes .PhpNamespaceIndex ;
16
19
import com .magento .idea .magento2plugin .reference .xml .PolyVariantReferenceBase ;
17
20
import com .magento .idea .magento2plugin .util .RegExUtil ;
18
21
import java .util .ArrayList ;
26
29
@ SuppressWarnings ({"PMD.AvoidInstantiatingObjectsInLoops" })
27
30
public class PhpClassReferenceProvider extends PsiReferenceProvider {
28
31
29
- @ NotNull
30
32
@ Override
31
- public PsiReference [] getReferencesByElement (
33
+ public @ NotNull PsiReference [] getReferencesByElement (
32
34
final @ NotNull PsiElement element ,
33
35
final @ NotNull ProcessingContext context
34
36
) {
35
-
36
37
final String origValue = element .getText ();
37
38
38
39
final Pattern pattern = Pattern .compile (RegExUtil .PhpRegex .FQN );
@@ -49,15 +50,21 @@ public PsiReference[] getReferencesByElement(
49
50
final StringBuilder namespace = new StringBuilder ();
50
51
String namespacePart ;
51
52
final List <PsiReference > psiReferences = new ArrayList <>();
53
+
52
54
for (int i = 0 ; i < fqnParts .length - 1 ; i ++) {
53
55
namespacePart = fqnParts [i ];
56
+ namespace .append ('\\' );
57
+ namespace .append (namespacePart );
58
+
59
+ final String namespaceId = namespace
60
+ .toString ()
61
+ .toLowerCase (new Locale ("en" ,"EN" ));
62
+
63
+ final Collection <PhpNamespace > references = hasNamespaceInIndex (
64
+ namespaceId ,
65
+ element .getProject ()
66
+ ) ? phpIndex .getNamespacesByName (namespaceId ) : new ArrayList <>();
54
67
55
- namespace .append ("\\ " );//NOPMD
56
- namespace .append (namespacePart );//NOPMD
57
- final Collection <PhpNamespace > references =
58
- phpIndex .getNamespacesByName (namespace .toString ().toLowerCase (
59
- new Locale ("en" ,"EN" ))
60
- );
61
68
if (!references .isEmpty ()) {
62
69
final TextRange range = new TextRange (
63
70
origValue .indexOf (classFQN ) + namespace .toString ().lastIndexOf (92 ),
@@ -70,6 +77,7 @@ public PsiReference[] getReferencesByElement(
70
77
71
78
final String className = classFQN .substring (classFQN .lastIndexOf (92 ) + 1 );
72
79
final Collection <PhpClass > classes = phpIndex .getAnyByFQN (classFQN );
80
+
73
81
if (!classes .isEmpty ()) {
74
82
final TextRange range = new TextRange (
75
83
origValue .lastIndexOf (92 ) + 1 ,
@@ -80,4 +88,24 @@ public PsiReference[] getReferencesByElement(
80
88
81
89
return psiReferences .toArray (new PsiReference [0 ]);
82
90
}
91
+
92
+ /**
93
+ * Check if php namespace index has specified identifier.
94
+ *
95
+ * @param namespaceIdentifier String
96
+ * @param project Project
97
+ *
98
+ * @return boolean
99
+ */
100
+ private boolean hasNamespaceInIndex (
101
+ final @ NotNull String namespaceIdentifier ,
102
+ final @ NotNull Project project
103
+ ) {
104
+ final Collection <String > keys = StubIndex .getInstance ().getAllKeys (
105
+ PhpNamespaceIndex .KEY ,
106
+ project
107
+ );
108
+
109
+ return keys .contains (namespaceIdentifier );
110
+ }
83
111
}
0 commit comments