33import com .cedricziel .idea .typo3 .util .PhpLangUtil ;
44import com .intellij .patterns .PlatformPatterns ;
55import com .intellij .psi .*;
6+ import com .intellij .psi .util .PsiTreeUtil ;
67import com .intellij .util .ProcessingContext ;
78import com .jetbrains .php .PhpIndex ;
89import com .jetbrains .php .lang .psi .elements .MethodReference ;
@@ -21,51 +22,43 @@ public class IconReferenceContributor extends PsiReferenceContributor {
2122 public void registerReferenceProviders (@ NotNull PsiReferenceRegistrar registrar ) {
2223 // known method calls
2324 registrar .registerReferenceProvider (
24- PlatformPatterns .psiElement (StringLiteralExpression .class ),
25- new PsiReferenceProvider () {
26- @ NotNull
27- @ Override
28- public PsiReference [] getReferencesByElement (@ NotNull PsiElement element , @ NotNull ProcessingContext context ) {
29- StringLiteralExpression stringLiteralExpression = (StringLiteralExpression ) element ;
25+ PlatformPatterns .psiElement (StringLiteralExpression .class ).withSuperParent (2 , PlatformPatterns .psiElement (MethodReference .class )),
26+ new PsiReferenceProvider () {
27+ @ NotNull
28+ @ Override
29+ public PsiReference [] getReferencesByElement (@ NotNull PsiElement element , @ NotNull ProcessingContext context ) {
30+ StringLiteralExpression stringLiteralExpression = (StringLiteralExpression ) element ;
31+ MethodReference methodReference = (MethodReference ) PsiTreeUtil .findFirstParent (stringLiteralExpression , p -> p instanceof MethodReference );
3032
31- PsiElement parent = stringLiteralExpression .getParent ();
32- while (!(parent instanceof MethodReference )) {
33-
34- if (parent != null ) {
35- parent = parent .getParent ();
36-
37- continue ;
38- }
39-
40- return new PsiReference [0 ];
41- }
42-
43- MethodReference methodReference = (MethodReference ) parent ;
44- String methodName = methodReference .getName ();
33+ if (methodReference == null ) {
34+ return PsiReference .EMPTY_ARRAY ;
35+ }
4536
46- if (methodReference .getFirstPsiChild () instanceof Variable ) {
47- Variable variable = (Variable ) methodReference .getFirstPsiChild ();
48- String signature = variable .getSignature ();
37+ String methodName = methodReference .getName ();
38+ if (methodReference .getFirstPsiChild () instanceof Variable ) {
39+ Variable variable = (Variable ) methodReference .getFirstPsiChild ();
40+ String signature = variable .getSignature ();
41+ try {
4942 Collection <? extends PhpNamedElement > bySignature = PhpIndex .getInstance (element .getProject ()).getBySignature (signature );
50- try {
51- for (PhpNamedElement el : bySignature ) {
52- if (el .getFQN ().equals (ICON_FACTORY ) && methodName .equals ("getIcon" )) {
53- return new PsiReference []{new IconReference (stringLiteralExpression )};
54- }
43+ for (PhpNamedElement el : bySignature ) {
44+ String fqn = el .getFQN ();
45+ if (fqn .equals (ICON_FACTORY ) && methodName .equals ("getIcon" )) {
46+ return new PsiReference []{new IconReference (stringLiteralExpression )};
5547 }
56- } catch (RuntimeException e ) {
57- // invalid index signature, skip
5848 }
49+ } catch (RuntimeException e ) {
50+ // invalid index signature, skip
5951 }
52+ }
6053
61- String className = PhpLangUtil .getClassName (stringLiteralExpression );
62- if (methodName != null && className != null && methodName .equals ("getIcon" ) && className .equals (ICON_FACTORY )) {
63- return new PsiReference []{new IconReference (stringLiteralExpression )};
64- }
65-
66- return new PsiReference [0 ];
54+ String className = PhpLangUtil .getClassName (stringLiteralExpression );
55+ if (methodName != null && className != null && methodName .equals ("getIcon" ) && className .equals (ICON_FACTORY )) {
56+ return new PsiReference []{new IconReference (stringLiteralExpression )};
6757 }
58+
59+ return PsiReference .EMPTY_ARRAY ;
6860 }
61+ }
6962 );
7063 }
7164}
0 commit comments