Skip to content

Commit 2b95e5b

Browse files
authored
Merge pull request #2105 from Haehnchen/feature/duplicate-yaml-visit
duplicate key inspections for yaml should only visit the working context
2 parents 660fd57 + 6f612f4 commit 2b95e5b

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/inspection/YamlDuplicateParameterKeyInspection.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection;
22

33
import com.intellij.codeInspection.ProblemsHolder;
4+
import com.intellij.psi.PsiElement;
45
import com.intellij.psi.PsiElementVisitor;
56
import com.intellij.psi.PsiFile;
67
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
78
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.yaml.psi.YAMLKeyValue;
810

911
/**
1012
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -14,16 +16,19 @@ public class YamlDuplicateParameterKeyInspection extends YamlDuplicateServiceKey
1416
@NotNull
1517
@Override
1618
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
17-
1819
PsiFile psiFile = holder.getFile();
1920
if(!Symfony2ProjectComponent.isEnabled(psiFile.getProject())) {
2021
return super.buildVisitor(holder, isOnTheFly);
2122
}
2223

2324
return new PsiElementVisitor() {
2425
@Override
25-
public void visitFile(PsiFile file) {
26-
visitRoot(file, "parameters", holder);
26+
public void visitElement(@NotNull PsiElement element) {
27+
if (element instanceof YAMLKeyValue yamlKeyValue) {
28+
visitRoot(yamlKeyValue, "parameters", holder);
29+
}
30+
31+
super.visitElement(element);
2732
}
2833
};
2934
}
Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection;
22

33
import com.intellij.codeInspection.LocalInspectionTool;
4+
import com.intellij.codeInspection.ProblemHighlightType;
45
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.psi.PsiElement;
57
import com.intellij.psi.PsiElementVisitor;
68
import com.intellij.psi.PsiFile;
7-
import com.intellij.psi.util.PsiTreeUtil;
89
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
9-
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1010
import org.jetbrains.annotations.NotNull;
11-
import org.jetbrains.yaml.YAMLUtil;
12-
import org.jetbrains.yaml.psi.YAMLCompoundValue;
13-
import org.jetbrains.yaml.psi.YAMLFile;
1411
import org.jetbrains.yaml.psi.YAMLKeyValue;
1512
import org.jetbrains.yaml.psi.YAMLMapping;
1613

@@ -30,25 +27,41 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
3027

3128
return new PsiElementVisitor() {
3229
@Override
33-
public void visitFile(PsiFile file) {
34-
visitRoot(file, "services", holder);
30+
public void visitElement(@NotNull PsiElement element) {
31+
if (element instanceof YAMLKeyValue yamlKeyValue) {
32+
visitRoot(yamlKeyValue, "services", holder);
33+
}
34+
35+
super.visitElement(element);
3536
}
3637
};
3738
}
3839

39-
protected void visitRoot(PsiFile psiFile, String rootName, @NotNull ProblemsHolder holder) {
40-
if(!(psiFile instanceof YAMLFile)) {
41-
return;
42-
}
40+
protected void visitRoot(@NotNull YAMLKeyValue yamlKeyValue, String rootName, @NotNull ProblemsHolder holder) {
41+
if (yamlKeyValue.getParent() instanceof YAMLMapping yamlMapping) {
42+
String keyText1 = yamlKeyValue.getKeyText();
43+
if (!keyText1.startsWith("_")) {
44+
if (yamlMapping.getParent() instanceof YAMLKeyValue yamlKeyValue1) {
45+
if (rootName.equals(yamlKeyValue1.getKeyText())) {
46+
int found = 0;
47+
for (YAMLKeyValue keyValue : yamlMapping.getKeyValues()) {
48+
String keyText = keyValue.getKeyText();
4349

44-
YAMLKeyValue yamlKeyValue = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, rootName);
45-
if(yamlKeyValue == null) {
46-
return;
47-
}
50+
if (keyText1.equals(keyText)) {
51+
found++;
52+
}
4853

49-
YAMLCompoundValue yaml = PsiTreeUtil.findChildOfType(yamlKeyValue, YAMLMapping.class);
50-
if(yaml != null) {
51-
YamlHelper.attachDuplicateKeyInspection(yaml, holder);
54+
if (found == 2) {
55+
final PsiElement keyElement = yamlKeyValue.getKey();
56+
assert keyElement != null;
57+
holder.registerProblem(keyElement, "Symfony: Duplicate key", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
58+
59+
break;
60+
}
61+
}
62+
}
63+
}
64+
}
5265
}
5366
}
5467
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.config.yaml.inspection;
2+
3+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
4+
5+
/**
6+
* @author Daniel Espendiller <daniel@espendiller.net>
7+
*
8+
* @see fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection.YamlDuplicateServiceKeyInspection
9+
*/
10+
public class YamlDuplicateParameterKeyInspectionTest extends SymfonyLightCodeInsightFixtureTestCase {
11+
12+
public void testDuplicateServiceKeyProvidesWarning() {
13+
assertLocalInspectionContains("services.yml", "" +
14+
"parameters:\n" +
15+
" fo<caret>o: \n" +
16+
" foo: ~\n",
17+
"Symfony: Duplicate key"
18+
);
19+
20+
assertLocalInspectionContains("services.yml", "" +
21+
"parameters:\n" +
22+
" foo: \n" +
23+
" f<caret>oo: ~\n",
24+
"Symfony: Duplicate key"
25+
);
26+
27+
assertLocalInspectionNotContains("services.yml", "" +
28+
"parameters:\n" +
29+
" foo1: ~" +
30+
" f<caret>oo: ~\n",
31+
"Symfony: Duplicate key"
32+
);
33+
}
34+
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/yaml/inspection/YamlDuplicateServiceKeyInspectionTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void testDuplicateServiceKeyProvidesWarning() {
1616
" car: car\n" +
1717
" foo: \n" +
1818
" car: car \n",
19-
"Duplicate key"
19+
"Symfony: Duplicate key"
2020
);
2121

2222
assertLocalInspectionContains("routing.yml", "" +
@@ -25,7 +25,16 @@ public void testDuplicateServiceKeyProvidesWarning() {
2525
" car: car \n" +
2626
" f<caret>oo: \n" +
2727
" car: car",
28-
"Duplicate key"
28+
"Symfony: Duplicate key"
29+
);
30+
31+
assertLocalInspectionNotContains("routing.yml", "" +
32+
"services:\n" +
33+
" foo1: \n" +
34+
" car: car \n" +
35+
" f<caret>oo: \n" +
36+
" car: car",
37+
"Symfony: Duplicate key"
2938
);
3039
}
3140

0 commit comments

Comments
 (0)