|
2 | 2 |
|
3 | 3 | import com.intellij.codeInspection.LocalInspectionTool;
|
4 | 4 | import com.intellij.codeInspection.ProblemsHolder;
|
5 |
| -import com.intellij.lang.Language; |
6 |
| -import com.intellij.lang.xml.XMLLanguage; |
7 | 5 | import com.intellij.psi.PsiElement;
|
8 | 6 | import com.intellij.psi.PsiElementVisitor;
|
9 | 7 | import com.intellij.psi.util.PsiTreeUtil;
|
|
18 | 16 | import org.apache.commons.lang3.StringUtils;
|
19 | 17 | import org.jetbrains.annotations.NotNull;
|
20 | 18 | import org.jetbrains.annotations.Nullable;
|
21 |
| -import org.jetbrains.yaml.YAMLLanguage; |
22 | 19 | import org.jetbrains.yaml.psi.YAMLKeyValue;
|
23 | 20 |
|
24 | 21 | /**
|
25 | 22 | * @author Daniel Espendiller <daniel@espendiller.net>
|
26 | 23 | */
|
27 |
| -public class ControllerMethodInspection extends LocalInspectionTool { |
28 |
| - |
29 |
| - @NotNull |
30 |
| - @Override |
31 |
| - public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
32 |
| - if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
33 |
| - return super.buildVisitor(holder, isOnTheFly); |
34 |
| - } |
| 24 | +public class ControllerMethodInspection { |
| 25 | + public static class ControllerMethodInspectionYaml extends LocalInspectionTool { |
| 26 | + public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 27 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 28 | + return super.buildVisitor(holder, isOnTheFly); |
| 29 | + } |
35 | 30 |
|
36 |
| - return new PsiElementVisitor() { |
37 |
| - @Override |
38 |
| - public void visitElement(@NotNull PsiElement element) { |
39 |
| - Language language = element.getLanguage(); |
| 31 | + return new PsiElementVisitor() { |
| 32 | + @Override |
| 33 | + public void visitElement(@NotNull PsiElement element) { |
| 34 | + if (YamlElementPatternHelper.getSingleLineScalarKey("_controller", "controller").accepts(element)) { |
| 35 | + String text = PsiElementUtils.trimQuote(element.getText()); |
| 36 | + if (StringUtils.isNotBlank(text)) { |
| 37 | + InspectionUtil.inspectController(element, text, holder, new YamlLazyRouteName(element)); |
| 38 | + } |
| 39 | + } |
40 | 40 |
|
41 |
| - if (language == YAMLLanguage.INSTANCE) { |
42 |
| - visitYamlElement(element, holder); |
43 |
| - } else if(language == XMLLanguage.INSTANCE) { |
44 |
| - visitXmlElement(element, holder); |
| 41 | + super.visitElement(element); |
45 | 42 | }
|
46 |
| - |
47 |
| - super.visitElement(element); |
48 |
| - } |
49 |
| - }; |
50 |
| - } |
51 |
| - |
52 |
| - private void visitYamlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) { |
53 |
| - if (YamlElementPatternHelper.getSingleLineScalarKey("_controller", "controller").accepts(element)) { |
54 |
| - String text = PsiElementUtils.trimQuote(element.getText()); |
55 |
| - if (StringUtils.isNotBlank(text)) { |
56 |
| - InspectionUtil.inspectController(element, text, holder, new YamlLazyRouteName(element)); |
57 |
| - } |
| 43 | + }; |
58 | 44 | }
|
59 | 45 | }
|
60 | 46 |
|
61 |
| - public void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) { |
62 |
| - if(XmlHelper.getRouteControllerPattern().accepts(element)) { |
63 |
| - String text = PsiElementUtils.trimQuote(element.getText()); |
64 |
| - if(StringUtils.isNotBlank(text)) { |
65 |
| - InspectionUtil.inspectController(element, text, holder, new XmlLazyRouteName(element)); |
| 47 | + public static class ControllerMethodInspectionXml extends LocalInspectionTool { |
| 48 | + public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 49 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 50 | + return super.buildVisitor(holder, isOnTheFly); |
66 | 51 | }
|
| 52 | + |
| 53 | + return new PsiElementVisitor() { |
| 54 | + @Override |
| 55 | + public void visitElement(@NotNull PsiElement element) { |
| 56 | + if(XmlHelper.getRouteControllerPattern().accepts(element)) { |
| 57 | + String text = PsiElementUtils.trimQuote(element.getText()); |
| 58 | + if(StringUtils.isNotBlank(text)) { |
| 59 | + InspectionUtil.inspectController(element, text, holder, new XmlLazyRouteName(element)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + super.visitElement(element); |
| 64 | + } |
| 65 | + }; |
67 | 66 | }
|
68 | 67 | }
|
69 | 68 |
|
70 | 69 | private record YamlLazyRouteName(@NotNull PsiElement psiElement) implements InspectionUtil.LazyControllerNameResolve {
|
71 | 70 | @Nullable
|
72 |
| - @Override |
73 |
| - public String getRouteName() { |
74 |
| - YAMLKeyValue defaultKeyValue = PsiTreeUtil.getParentOfType(this.psiElement.getParent(), YAMLKeyValue.class); |
75 |
| - if (defaultKeyValue == null) { |
76 |
| - return null; |
77 |
| - } |
78 |
| - |
79 |
| - YAMLKeyValue def = PsiTreeUtil.getParentOfType(defaultKeyValue, YAMLKeyValue.class); |
80 |
| - if (def == null) { |
81 |
| - return null; |
82 |
| - } |
| 71 | + @Override |
| 72 | + public String getRouteName() { |
| 73 | + YAMLKeyValue defaultKeyValue = PsiTreeUtil.getParentOfType(this.psiElement.getParent(), YAMLKeyValue.class); |
| 74 | + if (defaultKeyValue == null) { |
| 75 | + return null; |
| 76 | + } |
83 | 77 |
|
84 |
| - return YamlHelper.getYamlKeyName(def); |
| 78 | + YAMLKeyValue def = PsiTreeUtil.getParentOfType(defaultKeyValue, YAMLKeyValue.class); |
| 79 | + if (def == null) { |
| 80 | + return null; |
85 | 81 | }
|
| 82 | + |
| 83 | + return YamlHelper.getYamlKeyName(def); |
86 | 84 | }
|
| 85 | + } |
87 | 86 |
|
88 | 87 | private record XmlLazyRouteName(@NotNull PsiElement psiElement) implements InspectionUtil.LazyControllerNameResolve {
|
89 |
| - |
90 | 88 | @Nullable
|
91 |
| - @Override |
92 |
| - public String getRouteName() { |
93 |
| - XmlTag defaultTag = PsiTreeUtil.getParentOfType(this.psiElement, XmlTag.class); |
94 |
| - if (defaultTag != null) { |
95 |
| - XmlTag routeTag = PsiTreeUtil.getParentOfType(defaultTag, XmlTag.class); |
96 |
| - if (routeTag != null) { |
97 |
| - XmlAttribute id = routeTag.getAttribute("id"); |
98 |
| - if (id != null) { |
99 |
| - return id.getValue(); |
100 |
| - } |
| 89 | + @Override |
| 90 | + public String getRouteName() { |
| 91 | + XmlTag defaultTag = PsiTreeUtil.getParentOfType(this.psiElement, XmlTag.class); |
| 92 | + if (defaultTag != null) { |
| 93 | + XmlTag routeTag = PsiTreeUtil.getParentOfType(defaultTag, XmlTag.class); |
| 94 | + if (routeTag != null) { |
| 95 | + XmlAttribute id = routeTag.getAttribute("id"); |
| 96 | + if (id != null) { |
| 97 | + return id.getValue(); |
101 | 98 | }
|
102 | 99 | }
|
103 |
| - |
104 |
| - return null; |
105 | 100 | }
|
| 101 | + |
| 102 | + return null; |
106 | 103 | }
|
| 104 | + } |
107 | 105 | }
|
0 commit comments