Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3eb172c

Browse files
Merge pull request #2106 from Haehnchen/feature/routing-duplicate-visitor
duplicate routing key inspections for yaml should only visit the working context
2 parents 2b95e5b + 37f26e0 commit 3eb172c

File tree

3 files changed

+41
-80
lines changed

3 files changed

+41
-80
lines changed
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package fr.adrienbrault.idea.symfony2plugin.routing.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;
6-
import com.intellij.psi.PsiFile;
7-
import com.intellij.psi.util.PsiTreeUtil;
88
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
99
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1010
import org.jetbrains.annotations.NotNull;
1111
import org.jetbrains.yaml.psi.YAMLDocument;
12-
import org.jetbrains.yaml.psi.YAMLValue;
12+
import org.jetbrains.yaml.psi.YAMLKeyValue;
13+
import org.jetbrains.yaml.psi.YAMLMapping;
1314

1415
/**
1516
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -34,24 +35,34 @@ public MyPsiElementVisitor(ProblemsHolder holder) {
3435
}
3536

3637
@Override
37-
public void visitFile(PsiFile file) {
38-
// @TODO: detection of routing files in right way
39-
// routing.yml
40-
// comment.routing.yml
41-
// routing/foo.yml
42-
if(!YamlHelper.isRoutingFile(file)) {
43-
return;
44-
}
38+
public void visitElement(@NotNull PsiElement element) {
39+
if (element instanceof YAMLKeyValue yamlKeyValue && YamlHelper.isRoutingFile(yamlKeyValue.getContainingFile()) && yamlKeyValue.getParent() instanceof YAMLMapping yamlMapping && yamlMapping.getParent() instanceof YAMLDocument) {
40+
String keyText1 = null;
4541

46-
YAMLDocument document = PsiTreeUtil.findChildOfType(file, YAMLDocument.class);
47-
if(document == null) {
48-
return;
49-
}
42+
int found = 0;
43+
for (YAMLKeyValue keyValue : yamlMapping.getKeyValues()) {
44+
String keyText = keyValue.getKeyText();
45+
46+
// lazy
47+
if (keyText1 == null) {
48+
keyText1 = yamlKeyValue.getKeyText();
49+
}
5050

51-
YAMLValue topLevelValue = document.getTopLevelValue();
52-
if(topLevelValue != null) {
53-
YamlHelper.attachDuplicateKeyInspection(topLevelValue, holder);
51+
if (keyText1.equals(keyText)) {
52+
found++;
53+
}
54+
55+
if (found == 2) {
56+
final PsiElement keyElement = yamlKeyValue.getKey();
57+
assert keyElement != null;
58+
holder.registerProblem(keyElement, "Symfony: Duplicate key", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
59+
60+
break;
61+
}
62+
}
5463
}
64+
65+
super.visitElement(element);
5566
}
5667
}
5768
}

‎src/main/java/fr/adrienbrault/idea/symfony2plugin/util/yaml/YamlHelper.java‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package fr.adrienbrault.idea.symfony2plugin.util.yaml;
22

3-
import com.intellij.codeInspection.ProblemHighlightType;
4-
import com.intellij.codeInspection.ProblemsHolder;
53
import com.intellij.openapi.application.Result;
64
import com.intellij.openapi.command.WriteCommandAction;
75
import com.intellij.openapi.util.Pair;
@@ -17,7 +15,6 @@
1715
import com.intellij.util.Consumer;
1816
import com.intellij.util.ObjectUtils;
1917
import com.intellij.util.ProcessingContext;
20-
import com.intellij.util.Processor;
2118
import com.intellij.util.containers.ContainerUtil;
2219
import com.jetbrains.php.lang.psi.elements.Parameter;
2320
import com.jetbrains.php.lang.psi.elements.PhpClass;
@@ -462,62 +459,6 @@ public static void getParentArrayKeys(YAMLKeyValue yamlKeyValue, List<String> ke
462459

463460
}
464461

465-
/**
466-
* Migrate to processKeysAfterRoot @TODO
467-
*
468-
* @param keyContext Should be Document or YAMLCompoundValueImpl which holds the key value children
469-
*/
470-
public static void attachDuplicateKeyInspection(PsiElement keyContext, @NotNull ProblemsHolder holder) {
471-
472-
Map<String, PsiElement> psiElementMap = new HashMap<>();
473-
Set<PsiElement> yamlKeyValues = new HashSet<>();
474-
475-
Collection<YAMLKeyValue> collection = PsiTreeUtil.getChildrenOfTypeAsList(keyContext, YAMLKeyValue.class);
476-
for(YAMLKeyValue yamlKeyValue: collection) {
477-
String keyText = PsiElementUtils.trimQuote(yamlKeyValue.getKeyText());
478-
if(StringUtils.isNotBlank(keyText)) {
479-
if(psiElementMap.containsKey(keyText)) {
480-
yamlKeyValues.add(psiElementMap.get(keyText));
481-
yamlKeyValues.add(yamlKeyValue);
482-
} else {
483-
psiElementMap.put(keyText, yamlKeyValue);
484-
}
485-
486-
}
487-
488-
}
489-
490-
if(yamlKeyValues.size() > 0) {
491-
for(PsiElement psiElement: yamlKeyValues) {
492-
if(psiElement instanceof YAMLKeyValue) {
493-
final PsiElement keyElement = ((YAMLKeyValue) psiElement).getKey();
494-
assert keyElement != null;
495-
holder.registerProblem(keyElement, "Duplicate key", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
496-
}
497-
}
498-
}
499-
500-
}
501-
502-
/**
503-
* Process yaml key in second level filtered by a root:
504-
* File > roots -> "Item"
505-
* TODO: visitQualifiedKeyValuesInFile
506-
*/
507-
public static void processKeysAfterRoot(@NotNull PsiFile psiFile, @NotNull Processor<YAMLKeyValue> yamlKeyValueProcessor, @NotNull String... roots) {
508-
for (String root : roots) {
509-
YAMLKeyValue yamlKeyValue = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, root);
510-
if(yamlKeyValue != null) {
511-
YAMLCompoundValue yaml = PsiTreeUtil.findChildOfType(yamlKeyValue, YAMLCompoundValue.class);
512-
if(yaml != null) {
513-
for(YAMLKeyValue yamlKeyValueVisit: PsiTreeUtil.getChildrenOfTypeAsList(yaml, YAMLKeyValue.class)) {
514-
yamlKeyValueProcessor.process(yamlKeyValueVisit);
515-
}
516-
}
517-
}
518-
}
519-
}
520-
521462
public static boolean isRoutingFile(PsiFile psiFile) {
522463
return psiFile.getName().contains("routing") || psiFile.getVirtualFile().getPath().contains("/routing");
523464
}

‎src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/inspection/DuplicateLocalRouteInspectionTest.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@ public void testDuplicateRouteKeyProvidesWarning() {
1515
" car: foo\n" +
1616
"f<caret>oo:\n" +
1717
" car: foo\n",
18-
"Duplicate key"
18+
"Symfony: Duplicate key"
1919
);
2020

2121
assertLocalInspectionContains("routing.yml", "" +
2222
"fo<caret>o:\n" +
2323
" car: foo\n" +
2424
"foo:\n" +
2525
" car: foo\n",
26-
"Duplicate key"
26+
"Symfony: Duplicate key"
2727
);
28-
}
2928

29+
assertLocalInspectionNotContains("routing.yml", "" +
30+
"foo:\n" +
31+
" car: foo\n" +
32+
"foo<caret>bar:\n" +
33+
" car: foo\n" +
34+
"foo:\n" +
35+
" car: foo\n",
36+
"Symfony: Duplicate key"
37+
);
38+
}
3039
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /