Skip to content

Commit f462cc5

Browse files
committed
replace recursive php translation index visitor by controlflow
1 parent d20a7ee commit f462cc5

File tree

5 files changed

+61
-29
lines changed

5 files changed

+61
-29
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/TranslationStubIndex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.jetbrains.php.lang.psi.PhpFile;
1313
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
1414
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.externalizer.StringSetDataExternalizer;
15-
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.ArrayReturnPsiRecursiveVisitor;
15+
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.TranslationArrayReturnVisitor;
1616
import fr.adrienbrault.idea.symfony2plugin.translation.collector.YamlTranslationVisitor;
1717
import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil;
1818
import fr.adrienbrault.idea.symfony2plugin.util.ProjectUtil;
@@ -68,7 +68,7 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
6868

6969
if (psiFile instanceof PhpFile) {
7070
Set<String> translationKeySet = new HashSet<>();
71-
psiFile.acceptChildren(new ArrayReturnPsiRecursiveVisitor(pair -> translationKeySet.add(pair.getFirst())));
71+
TranslationArrayReturnVisitor.visitPhpReturn((PhpFile) psiFile, pair -> translationKeySet.add(pair.getFirst()));
7272

7373
if (translationKeySet.size() == 0) {
7474
return Collections.emptyMap();
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor;
22

33
import com.intellij.psi.PsiElement;
4-
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
54
import com.intellij.psi.util.PsiTreeUtil;
6-
import com.jetbrains.php.lang.psi.elements.ArrayCreationExpression;
7-
import com.jetbrains.php.lang.psi.elements.ArrayHashElement;
8-
import com.jetbrains.php.lang.psi.elements.PhpReturn;
9-
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
5+
import com.jetbrains.php.codeInsight.controlFlow.PhpControlFlowUtil;
6+
import com.jetbrains.php.codeInsight.controlFlow.PhpInstructionProcessor;
7+
import com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction;
8+
import com.jetbrains.php.lang.psi.PhpFile;
9+
import com.jetbrains.php.lang.psi.elements.*;
1010
import kotlin.Pair;
1111
import org.apache.commons.lang.StringUtils;
1212
import org.jetbrains.annotations.NotNull;
@@ -16,30 +16,34 @@
1616
import java.util.function.Consumer;
1717

1818
/**
19+
* "return ['key' => 'value1', 'key2' => 'value1']"
20+
*
1921
* @author Daniel Espendiller <daniel@espendiller.net>
2022
*/
21-
public class ArrayReturnPsiRecursiveVisitor extends PsiRecursiveElementWalkingVisitor {
23+
public class TranslationArrayReturnVisitor {
2224

23-
@NotNull
24-
private final Consumer<Pair<String, PsiElement>> arrayKeyVisitor;
25+
public static void visitPhpReturn(@NotNull PhpFile phpFile, @NotNull Consumer<Pair<String, PsiElement>> arrayKeyVisitor) {
2526

26-
public ArrayReturnPsiRecursiveVisitor(@NotNull Consumer<Pair<String, PsiElement>> arrayKeyVisitor) {
27-
this.arrayKeyVisitor = arrayKeyVisitor;
28-
}
27+
PhpInstructionProcessor processor = new PhpInstructionProcessor() {
28+
@Override
29+
public boolean processReturnInstruction(PhpReturnInstruction instruction) {
30+
PsiElement argument = instruction.getArgument();
31+
if (argument instanceof ArrayCreationExpression arrayCreationExpression) {
32+
collectConfigKeys(arrayCreationExpression, arrayKeyVisitor);
33+
}
2934

30-
@Override
31-
public void visitElement(@NotNull PsiElement element) {
32-
if(element instanceof PhpReturn) {
33-
visitPhpReturn((PhpReturn) element);
34-
}
35+
return super.processReturnInstruction(instruction);
36+
}
37+
};
3538

36-
super.visitElement(element);
37-
}
39+
// toplevel "returns"
40+
PhpControlFlowUtil.processFlow(phpFile.getControlFlow(), processor);
3841

39-
public void visitPhpReturn(PhpReturn phpReturn) {
40-
PsiElement arrayCreation = phpReturn.getFirstPsiChild();
41-
if(arrayCreation instanceof ArrayCreationExpression) {
42-
collectConfigKeys((ArrayCreationExpression) arrayCreation, this.arrayKeyVisitor);
42+
// toplevel "return" with namespace in file (for imports)
43+
for (PhpNamedElement value : phpFile.getTopLevelDefs().values()) {
44+
if (value instanceof PhpNamespace phpNamespace) {
45+
PhpControlFlowUtil.processFlow(phpNamespace.getControlFlow(), processor);
46+
}
4347
}
4448
}
4549

@@ -48,9 +52,7 @@ public static void collectConfigKeys(@NotNull ArrayCreationExpression creationEx
4852
}
4953

5054
public static void collectConfigKeys(@NotNull ArrayCreationExpression creationExpression, @NotNull Consumer<Pair<String, PsiElement>> arrayKeyVisitor, @NotNull List<String> context) {
51-
5255
for(ArrayHashElement hashElement: PsiTreeUtil.getChildrenOfTypeAsList(creationExpression, ArrayHashElement.class)) {
53-
5456
PsiElement arrayKey = hashElement.getKey();
5557
PsiElement arrayValue = hashElement.getValue();
5658

src/main/java/fr/adrienbrault/idea/symfony2plugin/translation/dict/TranslationUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import fr.adrienbrault.idea.symfony2plugin.extension.TranslatorProvider;
2020
import fr.adrienbrault.idea.symfony2plugin.extension.TranslatorProviderDict;
2121
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TranslationStubIndex;
22-
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.ArrayReturnPsiRecursiveVisitor;
22+
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.TranslationArrayReturnVisitor;
2323
import fr.adrienbrault.idea.symfony2plugin.translation.TranslatorLookupElement;
2424
import fr.adrienbrault.idea.symfony2plugin.translation.collector.YamlTranslationVisitor;
2525
import fr.adrienbrault.idea.symfony2plugin.translation.parser.DomainMappings;
@@ -152,11 +152,11 @@ public static Collection<PsiElement> getTranslationKeyTargetInsideFile(@NotNull
152152
return true;
153153
});
154154
} else if(psiFile instanceof PhpFile) {
155-
psiFile.acceptChildren(new ArrayReturnPsiRecursiveVisitor(pair -> {
155+
TranslationArrayReturnVisitor.visitPhpReturn((PhpFile) psiFile, pair -> {
156156
if (translationKey.equals(pair.getFirst())) {
157157
elements.add(pair.getSecond());
158158
}
159-
}));
159+
});
160160
} else if(TranslationUtil.isSupportedXlfFile(psiFile)) {
161161
// fine: xlf registered as XML file. try to find source value
162162
elements.addAll(TranslationUtil.getTargetForXlfAsXmlFile((XmlFile) psiFile, translationKey));

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/TranslationStubIndexTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public void setUp() throws Exception {
2323
super.setUp();
2424
myFixture.copyFileToProject("messages.fr.xlf");
2525
myFixture.copyFileToProject("messages.fr.php", "translations/messages.fr.php");
26+
myFixture.copyFileToProject("messages.de.php", "translations/messages.de.php");
2627
myFixture.copyFileToProject("messages+intl-icu.fr.xlf");
2728
myFixture.copyFileToProject("messages_two.fr.xlf");
2829
myFixture.copyFileToProject("foo.fr.xliff");
@@ -88,6 +89,11 @@ public void testThatDomainAndTranslationsKeyOfPhpIsInIndex() {
8889
assertContainsElements(getDomainKeys("messages"), "symfony.is.great [PHP-nested]");
8990
assertContainsElements(getDomainKeys("messages"), "symfony.is.amazing [PHP-nested]");
9091
assertContainsElements(getDomainKeys("messages"), "symfony.has.bundles [PHP-nested]");
92+
93+
assertContainsElements(getDomainKeys("messages"), "nested_1 [PHP]");
94+
assertContainsElements(getDomainKeys("messages"), "nested_2 [PHP]");
95+
assertContainsElements(getDomainKeys("messages"), "nested_3 [PHP]");
96+
assertContainsElements(getDomainKeys("messages"), "nested_4 [PHP]");
9197
}
9298

9399
public void testThatDomainAndTranslationsKeyOfXliffv2IsInIndex() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
if (true) {
4+
return [
5+
'nested_1 [PHP]' => "",
6+
];
7+
}
8+
9+
return [
10+
'nested_2 [PHP]' => "",
11+
];
12+
13+
14+
namespace Foo;
15+
16+
if (true) {
17+
return [
18+
'nested_3 [PHP]' => "",
19+
];
20+
}
21+
22+
return [
23+
'nested_4 [PHP]' => "",
24+
];

0 commit comments

Comments
 (0)