Skip to content

Commit 816cae3

Browse files
authored
Merge pull request #2094 from Haehnchen/feature/const-inspection-recursive
replace recursive visiting for yaml / xml const inspection
2 parents 09ab178 + 08fe9f4 commit 816cae3

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/inspection/ContainerConstantInspection.java

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
import com.intellij.codeInspection.LocalInspectionTool;
44
import com.intellij.codeInspection.ProblemHighlightType;
55
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.lang.Language;
7+
import com.intellij.lang.xml.XMLLanguage;
68
import com.intellij.psi.PsiElement;
79
import com.intellij.psi.PsiElementVisitor;
8-
import com.intellij.psi.PsiFile;
9-
import com.intellij.psi.PsiRecursiveElementVisitor;
10-
import com.intellij.psi.xml.XmlFile;
1110
import com.intellij.psi.xml.XmlText;
1211
import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
1312
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
1413
import org.apache.commons.lang.StringUtils;
1514
import org.jetbrains.annotations.NotNull;
16-
import org.jetbrains.yaml.psi.YAMLFile;
15+
import org.jetbrains.yaml.YAMLLanguage;
1716
import org.jetbrains.yaml.psi.YAMLScalar;
1817

1918
/**
@@ -27,61 +26,48 @@ public class ContainerConstantInspection extends LocalInspectionTool {
2726
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
2827
return new PsiElementVisitor() {
2928
@Override
30-
public void visitFile(@NotNull PsiFile psiFile) {
31-
if(psiFile instanceof XmlFile) {
32-
xmlVisitor(holder, psiFile);
33-
} else if(psiFile instanceof YAMLFile) {
34-
yamlVisitor(holder, psiFile);
29+
public void visitElement(@NotNull PsiElement element) {
30+
Language language = element.getLanguage();
31+
32+
if (language == YAMLLanguage.INSTANCE) {
33+
if (element instanceof YAMLScalar yamlScalar) {
34+
visitYamlElement(yamlScalar, holder);
35+
}
36+
} else if(language == XMLLanguage.INSTANCE) {
37+
visitXmlElement(element, holder);
3538
}
39+
40+
super.visitElement(element);
3641
}
3742
};
3843
}
39-
private void yamlVisitor(@NotNull ProblemsHolder holder, @NotNull PsiFile psiFile) {
40-
psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
41-
@Override
42-
public void visitElement(@NotNull PsiElement psiElement) {
43-
if(psiElement instanceof YAMLScalar) {
44-
String textValue = ((YAMLScalar) psiElement).getTextValue();
45-
if(textValue.startsWith("!php/const:")) {
46-
String constantName = textValue.substring(11);
47-
if(StringUtils.isNotBlank(constantName) && ServiceContainerUtil.getTargetsForConstant(psiElement.getProject(), constantName).size() == 0) {
48-
holder.registerProblem(psiElement, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
49-
}
50-
}
51-
}
52-
53-
super.visitElement(psiElement);
44+
private void visitYamlElement(@NotNull YAMLScalar psiElement, @NotNull ProblemsHolder holder) {
45+
String textValue = ((YAMLScalar) psiElement).getTextValue();
46+
if(textValue.startsWith("!php/const:")) {
47+
String constantName = textValue.substring(11);
48+
if(StringUtils.isNotBlank(constantName) && ServiceContainerUtil.getTargetsForConstant(psiElement.getProject(), constantName).size() == 0) {
49+
holder.registerProblem(psiElement, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
5450
}
55-
});
51+
}
5652
}
5753

58-
private void xmlVisitor(@NotNull ProblemsHolder holder, @NotNull PsiFile psiFile) {
59-
psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
60-
@Override
61-
public void visitElement(@NotNull PsiElement psiElement) {
62-
if(!XmlHelper.getArgumentValueWithTypePattern("constant").accepts(psiElement)) {
63-
super.visitElement(psiElement);
64-
return;
65-
}
54+
private void visitXmlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) {
55+
if(!XmlHelper.getArgumentValueWithTypePattern("constant").accepts(psiElement)) {
56+
return;
57+
}
6658

67-
PsiElement xmlText = psiElement.getParent();
68-
if(!(xmlText instanceof XmlText)) {
69-
super.visitElement(psiElement);
70-
return;
71-
}
59+
PsiElement xmlText = psiElement.getParent();
60+
if(!(xmlText instanceof XmlText)) {
61+
return;
62+
}
7263

73-
String value = ((XmlText) xmlText).getValue();
74-
if(StringUtils.isBlank(value)) {
75-
super.visitElement(psiElement);
76-
return;
77-
}
64+
String value = ((XmlText) xmlText).getValue();
65+
if(StringUtils.isBlank(value)) {
66+
return;
67+
}
7868

79-
if(ServiceContainerUtil.getTargetsForConstant(xmlText.getProject(), value).size() == 0) {
80-
holder.registerProblem(xmlText, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
81-
}
82-
83-
super.visitElement(psiElement);
84-
}
85-
});
69+
if(ServiceContainerUtil.getTargetsForConstant(xmlText.getProject(), value).size() == 0) {
70+
holder.registerProblem(xmlText, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
71+
}
8672
}
8773
}

0 commit comments

Comments
 (0)