Skip to content

Commit e7788b2

Browse files
committed
support new node tree structure of Symfony configuration and fix pattern for possible empty file of config file detection Haehnchen#1344
1 parent aea05e9 commit e7788b2

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/utils/ConfigUtil.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import com.intellij.psi.util.*;
99
import com.intellij.util.containers.ContainerUtil;
1010
import com.jetbrains.php.PhpIndex;
11-
import com.jetbrains.php.lang.psi.elements.Method;
12-
import com.jetbrains.php.lang.psi.elements.MethodReference;
13-
import com.jetbrains.php.lang.psi.elements.PhpClass;
11+
import com.jetbrains.php.lang.psi.elements.*;
1412
import fr.adrienbrault.idea.symfony2plugin.util.FilesystemUtil;
1513
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1614
import org.apache.commons.lang.StringUtils;
@@ -107,12 +105,28 @@ private static void visitTreeSignatures(@NotNull Collection<PhpClass> classes, @
107105
continue;
108106
}
109107

108+
Collection<ParameterListOwner> parameterOwners = new ArrayList<>();
109+
110+
// Symfony < 4.1: (new TreeBuilder())->root('foobar')
110111
for(MethodReference methodReference: PsiTreeUtil.findChildrenOfType(method, MethodReference.class)) {
111112
if(!PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder", "root")) {
112113
continue;
113114
}
114115

115-
PsiElement[] parameters = methodReference.getParameters();
116+
parameterOwners.add(methodReference);
117+
}
118+
119+
// Symfony >= 4.1: new TreeBuilder('foobar')
120+
for(NewExpression methodReference: PsiTreeUtil.findChildrenOfType(method, NewExpression.class)) {
121+
if(!PhpElementsUtil.isNewExpressionPhpClassWithInstance(methodReference, "Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder")) {
122+
continue;
123+
}
124+
125+
parameterOwners.add(methodReference);
126+
}
127+
128+
for (ParameterListOwner ownerParameters : parameterOwners) {
129+
PsiElement[] parameters = ownerParameters.getParameters();
116130
if (parameters.length == 0) {
117131
continue;
118132
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlElementPatternHelper.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.intellij.openapi.vfs.VirtualFile;
44
import com.intellij.patterns.*;
5+
import com.intellij.psi.PsiDirectory;
56
import com.intellij.psi.PsiElement;
67
import com.intellij.psi.PsiFile;
78
import com.intellij.psi.PsiWhiteSpace;
@@ -43,6 +44,8 @@ public class YamlElementPatternHelper {
4344
*
4445
* /../config/packages/doctrine.yml
4546
* /../config/packages/test/doctrine.yml
47+
*
48+
* /../config/packages/doctrine.yaml
4649
*/
4750
private static final PatternCondition<PsiFile> CONFIG_YAML_PATTERN = new PatternCondition<PsiFile>("Yaml Configuration") {
4851
@Override
@@ -55,12 +58,19 @@ public boolean accepts(@NotNull PsiFile psiFile, ProcessingContext processingCon
5558
return true;
5659
}
5760

58-
VirtualFile virtualFile = psiFile.getVirtualFile();
59-
if(virtualFile == null) {
61+
// psiFile.virtualFile is empty; check via folder structure
62+
PsiDirectory containingDirectory = psiFile.getContainingDirectory();
63+
if (containingDirectory == null) {
6064
return false;
6165
}
6266

63-
String relativePath = VfsExUtil.getRelativeProjectPath(psiFile.getProject(), virtualFile);
67+
if ("packages".equals(containingDirectory.getName())) {
68+
return true;
69+
}
70+
71+
VirtualFile virtualDirectoryFile = containingDirectory.getVirtualFile();
72+
73+
String relativePath = VfsExUtil.getRelativeProjectPath(psiFile.getProject(), virtualDirectoryFile);
6474
return relativePath != null && relativePath.contains("config/packages/");
6575
}
6676
};

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/utils/ConfigUtilTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void testGetTreeSignatures() {
3131

3232
assertContainsElements(signatures.get("foobar_root"), "\\Foo\\Bar\\MyConfiguration");
3333
assertContainsElements(signatures.get("foobar_root"), "\\Foo\\Bar\\MyNextConfiguration");
34+
assertContainsElements(signatures.get("foobar_root_11"), "\\Foo\\Bar\\MyNextConfiguration");
3435
}
3536

3637
/**

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/utils/fixtures/ConfigUtilTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getConfigTreeBuilder()
4343
if(true) {
4444
foreach([] as $_) {
4545
(new TreeBuilder())->root('foobar_root');
46+
(new TreeBuilder('foobar_root_11'));
4647
}
4748
}
4849
}

0 commit comments

Comments
 (0)