Skip to content

Commit 78385ba

Browse files
committed
[T3CMS] Contribute IconReferences without PhpIndex exceptions
1 parent 0928c5a commit 78385ba

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

typo3-cms/src/main/java/com/cedricziel/idea/typo3/icons/IconReferenceContributor.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cedricziel.idea.typo3.util.PhpLangUtil;
44
import com.intellij.patterns.PlatformPatterns;
55
import com.intellij.psi.*;
6+
import com.intellij.psi.util.PsiTreeUtil;
67
import com.intellij.util.ProcessingContext;
78
import com.jetbrains.php.PhpIndex;
89
import 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
}

typo3-cms/src/test/java/com/cedricziel/idea/typo3/icons/IconReferenceProviderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.cedricziel.idea.typo3.icons;
22

3+
import com.cedricziel.idea.typo3.AbstractTestCase;
34
import com.intellij.psi.PsiElement;
45
import com.intellij.psi.PsiReference;
5-
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
66

77

8-
public class IconReferenceProviderTest extends LightCodeInsightFixtureTestCase {
8+
public class IconReferenceProviderTest extends AbstractTestCase {
99
@Override
1010
protected String getTestDataPath() {
1111
return "testData/com/cedricziel/idea/typo3/icons";

typo3-cms/testData/com/cedricziel/idea/typo3/icons/general_utility_icon_provider_test.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ class IconFactory
77
}
88
}
99

10+
namespace TYPO3\CMS\Core\Utility {
11+
class GeneralUtility
12+
{
13+
public static function makeInstance()
14+
{
15+
}
16+
}
17+
}
18+
1019
namespace {
11-
use TYPO3\CMS\Core\Utility\GeneralUtility;
20+
1221
use TYPO3\CMS\Core\Imaging\IconFactory;
22+
use TYPO3\CMS\Core\Utility\GeneralUtility;
1323

1424
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
1525

0 commit comments

Comments
 (0)