Skip to content

Commit f767aa1

Browse files
committed
Make UserFuncReference being contributed and resolved from concatenation expressions
1 parent 0c592c8 commit f767aa1

File tree

3 files changed

+99
-36
lines changed

3 files changed

+99
-36
lines changed

src/main/java/com/cedricziel/idea/typo3/userFunc/UserFuncReference.java

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cedricziel.idea.typo3.userFunc;
22

3+
import com.intellij.openapi.util.TextRange;
34
import com.intellij.psi.PsiElementResolveResult;
45
import com.intellij.psi.PsiPolyVariantReferenceBase;
56
import com.intellij.psi.ResolveResult;
@@ -50,7 +51,11 @@ public UserFuncReference(XmlText xmlText) {
5051
}
5152

5253
public UserFuncReference(StringLiteralExpression stringLiteralExpression) {
53-
super(stringLiteralExpression);
54+
this(stringLiteralExpression, null);
55+
}
56+
57+
public UserFuncReference(StringLiteralExpression stringLiteralExpression, TextRange textRange) {
58+
super(stringLiteralExpression, textRange);
5459

5560
String text = stringLiteralExpression.getContents();
5661
if (text.contains("->")) {
@@ -79,11 +84,11 @@ public UserFuncReference(StringLiteralExpression stringLiteralExpression) {
7984
}
8085
}
8186

82-
public UserFuncReference(ConcatenationExpression element) {
83-
super(element);
87+
public UserFuncReference(StringLiteralExpression element1, TextRange textRange, ConcatenationExpression parent) {
88+
super(element1, textRange);
8489

85-
ClassConstantReference classRef = PsiTreeUtil.findChildOfType(element, ClassConstantReference.class);
86-
StringLiteralExpression stringEl = PsiTreeUtil.findChildOfType(element, StringLiteralExpression.class);
90+
ClassConstantReference classRef = PsiTreeUtil.findChildOfType(parent, ClassConstantReference.class);
91+
StringLiteralExpression stringEl = PsiTreeUtil.findChildOfType(parent, StringLiteralExpression.class);
8792

8893
if (classRef == null || stringEl == null) {
8994
methodName = null;
@@ -92,7 +97,7 @@ public UserFuncReference(ConcatenationExpression element) {
9297
return;
9398
}
9499

95-
className = classRef.getFQN();
100+
className = classRef.getText().replace("::class", "");
96101

97102
String[] split = StringUtils.split(stringEl.getContents(), "->");
98103
if (split.length == 0) {
@@ -130,20 +135,22 @@ public Object[] getVariants() {
130135

131136
List<Object> list = new ArrayList<>();
132137
PhpIndex phpIndex = PhpIndex.getInstance(myElement.getProject());
133-
Iterator<String> count = phpIndex.getAllFunctionNames(null).iterator();
134138

135-
while (true) {
136-
while (count.hasNext()) {
137-
String functionName = count.next();
139+
if (className == null) {
140+
Iterator<String> globalFunctions = phpIndex.getAllFunctionNames(null).iterator();
141+
while (true) {
142+
while (globalFunctions.hasNext()) {
143+
String functionName = globalFunctions.next();
138144

139-
phpIndex
140-
.getFunctionsByName(functionName)
141-
.stream()
142-
.map(UserFuncLookupElement::new)
143-
.forEach(list::add);
144-
}
145+
phpIndex
146+
.getFunctionsByName(functionName)
147+
.stream()
148+
.map(UserFuncLookupElement::new)
149+
.forEach(list::add);
150+
}
145151

146-
break;
152+
break;
153+
}
147154
}
148155

149156
if (className != null) {
@@ -156,21 +163,6 @@ public Object[] getVariants() {
156163
});
157164
}
158165

159-
Iterator<String> classes = phpIndex.getAllClassNames(null).iterator();
160-
while (true) {
161-
while (classes.hasNext()) {
162-
String className = classes.next();
163-
164-
phpIndex
165-
.getFunctionsByName(className)
166-
.stream()
167-
.map(UserFuncLookupElement::new)
168-
.forEach(list::add);
169-
}
170-
171-
break;
172-
}
173-
174166
return list.toArray();
175167
}
176168
}

src/main/java/com/cedricziel/idea/typo3/userFunc/UserFuncReferenceContributor.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cedricziel.idea.typo3.userFunc;
22

3+
import com.intellij.openapi.util.TextRange;
34
import com.intellij.psi.*;
45
import com.intellij.psi.xml.XmlText;
56
import com.intellij.util.ProcessingContext;
@@ -35,7 +36,16 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
3536
@Override
3637
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
3738

38-
return new PsiReference[]{new UserFuncReference((StringLiteralExpression) element)};
39+
StringLiteralExpression element1 = (StringLiteralExpression) element;
40+
if (element1.getContents().contains("->")) {
41+
int i = element1.getContents().indexOf("->");
42+
43+
return new PsiReference[]{
44+
new UserFuncReference(element1, new TextRange(i + 3, element1.getTextLength() - 1))
45+
};
46+
}
47+
48+
return new PsiReference[]{new UserFuncReference(element1)};
3949
}
4050
}
4151
);
@@ -50,7 +60,16 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
5060
@Override
5161
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
5262

53-
return new PsiReference[]{new UserFuncReference((StringLiteralExpression) element)};
63+
StringLiteralExpression element1 = (StringLiteralExpression) element;
64+
if (element1.getContents().contains("->")) {
65+
int i = element1.getContents().indexOf("->");
66+
67+
return new PsiReference[]{
68+
new UserFuncReference(element1, new TextRange(i + 3, element1.getTextLength() - 1))
69+
};
70+
}
71+
72+
return new PsiReference[]{new UserFuncReference(element1)};
5473
}
5574
}
5675
);
@@ -65,7 +84,17 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
6584
@Override
6685
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
6786

68-
return new PsiReference[]{new UserFuncReference((ConcatenationExpression) element.getParent())};
87+
ConcatenationExpression parent = (ConcatenationExpression) element.getParent();
88+
StringLiteralExpression element1 = (StringLiteralExpression) element;
89+
if (element1.getContents().contains("->")) {
90+
int i = element1.getContents().indexOf("->");
91+
92+
return new PsiReference[]{
93+
new UserFuncReference(element1, new TextRange(i + 3, element1.getTextLength() - 1), parent)
94+
};
95+
}
96+
97+
return PsiReference.EMPTY_ARRAY;
6998
}
7099
}
71100
);
@@ -80,7 +109,17 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
80109
@Override
81110
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
82111

83-
return new PsiReference[]{new UserFuncReference((ConcatenationExpression) element.getParent())};
112+
ConcatenationExpression parent = (ConcatenationExpression) element.getParent();
113+
StringLiteralExpression element1 = (StringLiteralExpression) element;
114+
if (element1.getContents().contains("->")) {
115+
int i = element1.getContents().indexOf("->");
116+
117+
return new PsiReference[]{
118+
new UserFuncReference(element1, new TextRange(i + 3, element1.getTextLength() - 1), parent)
119+
};
120+
}
121+
122+
return PsiReference.EMPTY_ARRAY;
84123
}
85124
}
86125
);

src/test/java/com/cedricziel/idea/typo3/userFunc/UserFuncReferenceTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public void testReferenceCanResolveDefinition() {
3737
Function.class,
3838
"A global function can be resolved"
3939
);
40+
41+
assertResolvesTo(
42+
"bar.php",
43+
"<?php \n['userFunc' => Foo\\Bar::class . '->q<caret>ux'];",
44+
UserFuncReference.class,
45+
Method.class,
46+
"A class method can be resolved through concatenation"
47+
);
4048
}
4149

4250
public void testReferenceCanResolveVariants() {
@@ -58,12 +66,36 @@ public void testReferenceCanResolveVariants() {
5866
"Reference can correctly resolve static methods as variants"
5967
);
6068

69+
assertHasVariant(
70+
"foo.php",
71+
"<?php \n['userFunc' => Foo\\Bar::class . '->q<caret>'];",
72+
"quo",
73+
UserFuncReference.class,
74+
"Reference can correctly resolve public methods as variants on concatenation expression"
75+
);
76+
77+
assertHasVariant(
78+
"foo.php",
79+
"<?php \n['userFunc' => Foo\\Bar::class . '->q<caret>'];",
80+
"qux",
81+
UserFuncReference.class,
82+
"Reference can correctly resolve static methods as variants on concatenation expression"
83+
);
84+
6185
assertNotHasVariant(
6286
"foo.php",
6387
"<?php \n['userFunc' => 'Foo\\Bar->q<caret>'];",
6488
"invisible",
6589
UserFuncReference.class,
6690
"Reference does not present inaccessible variants (non public methods)"
6791
);
92+
93+
assertNotHasVariant(
94+
"foo.php",
95+
"<?php \n['userFunc' => Foo\\Bar::class . '->q<caret>'];",
96+
"invisible",
97+
UserFuncReference.class,
98+
"Reference does not present inaccessible variants (non public methods) on concatenation expression"
99+
);
68100
}
69101
}

0 commit comments

Comments
 (0)