22
33import com .intellij .codeInspection .LocalInspectionTool ;
44import com .intellij .codeInspection .ProblemsHolder ;
5+ import com .intellij .lang .Language ;
6+ import com .intellij .lang .xml .XMLLanguage ;
57import com .intellij .psi .PsiElement ;
68import com .intellij .psi .PsiElementVisitor ;
7- import com .intellij .psi .PsiFile ;
8- import com .intellij .psi .PsiRecursiveElementWalkingVisitor ;
99import com .intellij .psi .util .PsiTreeUtil ;
1010import com .intellij .psi .xml .XmlAttribute ;
11- import com .intellij .psi .xml .XmlFile ;
1211import com .intellij .psi .xml .XmlTag ;
1312import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
1413import fr .adrienbrault .idea .symfony2plugin .codeInspection .InspectionUtil ;
1918import org .apache .commons .lang .StringUtils ;
2019import org .jetbrains .annotations .NotNull ;
2120import org .jetbrains .annotations .Nullable ;
22- import org .jetbrains .yaml .psi . YAMLFile ;
21+ import org .jetbrains .yaml .YAMLLanguage ;
2322import org .jetbrains .yaml .psi .YAMLKeyValue ;
2423
2524/**
@@ -36,104 +35,73 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
3635
3736 return new PsiElementVisitor () {
3837 @ Override
39- public void visitFile (PsiFile psiFile ) {
40- if (psiFile instanceof YAMLFile ) {
41- visitYaml (holder , psiFile );
42- }
38+ public void visitElement (@ NotNull PsiElement element ) {
39+ Language language = element .getLanguage ();
4340
44- if (psiFile instanceof XmlFile ) {
45- visitXml (holder , psiFile );
41+ if (language == YAMLLanguage .INSTANCE ) {
42+ visitYamlElement (element , holder );
43+ } else if (language == XMLLanguage .INSTANCE ) {
44+ visitXmlElement (element , holder );
4645 }
4746
48- super .visitFile ( psiFile );
47+ super .visitElement ( element );
4948 }
5049 };
5150 }
5251
53- private void visitYaml (final ProblemsHolder holder , PsiFile psiFile ) {
54- psiFile .acceptChildren (new PsiRecursiveElementWalkingVisitor () {
55- @ Override
56- public void visitElement (PsiElement element ) {
57- if (YamlElementPatternHelper .getSingleLineScalarKey ("_controller" , "controller" ).accepts (element )) {
58- String text = PsiElementUtils .trimQuote (element .getText ());
59- if (StringUtils .isNotBlank (text )) {
60- InspectionUtil .inspectController (element , text , holder , new YamlLazyRouteName (element ));
61- }
62- }
63-
64- super .visitElement (element );
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 ));
6557 }
66- });
58+ }
6759 }
6860
69- private void visitXml (final ProblemsHolder holder , PsiFile psiFile ) {
70- psiFile .acceptChildren (new PsiRecursiveElementWalkingVisitor () {
71- @ Override
72- public void visitElement (PsiElement element ) {
73- if (XmlHelper .getRouteControllerPattern ().accepts (element )) {
74- String text = PsiElementUtils .trimQuote (element .getText ());
75- if (StringUtils .isNotBlank (text )) {
76- InspectionUtil .inspectController (element , text , holder , new XmlLazyRouteName (element ));
77- }
78- }
79-
80- super .visitElement (element );
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 ));
8166 }
82- });
83- }
84-
85- public class YamlLazyRouteName implements InspectionUtil .LazyControllerNameResolve {
86-
87- private final PsiElement psiElement ;
88-
89- public YamlLazyRouteName (PsiElement psiElement ) {
90- this .psiElement = psiElement ;
9167 }
68+ }
9269
70+ private record YamlLazyRouteName (@ NotNull PsiElement psiElement ) implements InspectionUtil .LazyControllerNameResolve {
9371 @ Nullable
94- @ Override
95- public String getRouteName () {
72+ @ Override
73+ public String getRouteName () {
74+ YAMLKeyValue defaultKeyValue = PsiTreeUtil .getParentOfType (this .psiElement .getParent (), YAMLKeyValue .class );
75+ if (defaultKeyValue == null ) {
76+ return null ;
77+ }
9678
97- YAMLKeyValue defaultKeyValue = PsiTreeUtil .getParentOfType (this . psiElement . getParent () , YAMLKeyValue .class );
98- if ( defaultKeyValue == null ) {
99- return null ;
100- }
79+ YAMLKeyValue def = PsiTreeUtil .getParentOfType (defaultKeyValue , YAMLKeyValue .class );
80+ if ( def == null ) {
81+ return null ;
82+ }
10183
102- YAMLKeyValue def = PsiTreeUtil .getParentOfType (defaultKeyValue , YAMLKeyValue .class );
103- if (def == null ) {
104- return null ;
84+ return YamlHelper .getYamlKeyName (def );
10585 }
106-
107- return YamlHelper .getYamlKeyName (def );
10886 }
109- }
110-
111- public class XmlLazyRouteName implements InspectionUtil .LazyControllerNameResolve {
112-
113- private final PsiElement psiElement ;
11487
115- public XmlLazyRouteName (PsiElement psiElement ) {
116- this .psiElement = psiElement ;
117- }
88+ private record XmlLazyRouteName (@ NotNull PsiElement psiElement ) implements InspectionUtil .LazyControllerNameResolve {
11889
11990 @ Nullable
120- @ Override
121- public String getRouteName () {
122-
123- XmlTag defaultTag = PsiTreeUtil . getParentOfType ( this . psiElement , XmlTag . class );
124- if ( defaultTag != null ) {
125- XmlTag routeTag = PsiTreeUtil . getParentOfType ( defaultTag , XmlTag . class );
126- if ( routeTag != null ) {
127- XmlAttribute id = routeTag . getAttribute ( "id" );
128- if ( id != null ) {
129- return id . getValue ();
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+ }
130101 }
131102 }
132- }
133103
134- return null ;
104+ return null ;
105+ }
135106 }
136-
137- }
138-
139107}
0 commit comments