55import com .intellij .codeInspection .ProblemsHolder ;
66import com .intellij .psi .PsiElement ;
77import com .intellij .psi .PsiElementVisitor ;
8- import com .intellij .psi .PsiFile ;
9- import com .intellij .psi .util .PsiTreeUtil ;
108import com .intellij .psi .xml .XmlAttribute ;
119import com .intellij .psi .xml .XmlAttributeValue ;
12- import com .intellij .psi .xml .XmlDocument ;
1310import com .intellij .psi .xml .XmlTag ;
1411import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
15- import org .apache .commons .lang .StringUtils ;
1612import org .jetbrains .annotations .NotNull ;
1713
18- import java .util .HashMap ;
19- import java .util .HashSet ;
20- import java .util .Map ;
21- import java .util .Set ;
22-
2314/**
2415 * @author Daniel Espendiller <daniel@espendiller.net>
2516 */
@@ -34,59 +25,41 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
3425
3526 return new PsiElementVisitor () {
3627 @ Override
37- public void visitFile (PsiFile file ) {
38- visitRoot (file , holder , "services" , "service" , "id" );
39- super .visitFile (file );
28+ public void visitElement (@ NotNull PsiElement element ) {
29+ if (element instanceof XmlAttributeValue xmlAttributeValue ) {
30+ visitRoot (xmlAttributeValue , holder , "services" , "service" , "id" );
31+ }
32+
33+ super .visitElement (element );
4034 }
4135 };
4236 }
4337
44- protected void visitRoot (PsiFile psiFile , @ NotNull ProblemsHolder holder , String root , String child , String tagName ) {
38+ protected void visitRoot (@ NotNull XmlAttributeValue xmlAttributeValue , @ NotNull ProblemsHolder holder , String root , String child , String tagName ) {
39+ String value = null ;
4540
46- XmlDocument xmlDocument = PsiTreeUtil .getChildOfType (psiFile , XmlDocument .class );
47- if (xmlDocument == null ) {
48- return ;
49- }
41+ if (xmlAttributeValue .getParent () instanceof XmlAttribute xmlAttribute && tagName .equals (xmlAttribute .getName ())) {
42+ XmlTag xmlTag = xmlAttribute .getParent ();
43+ if (xmlTag != null && child .equals (xmlTag .getName ()) && xmlTag .getParent () instanceof XmlTag rootContextXmlTag && root .equals (rootContextXmlTag .getName ())) {
44+ int found = 0 ;
45+ for (XmlTag parameters : rootContextXmlTag .findSubTags (child )) {
46+ String key = parameters .getAttributeValue (tagName );
5047
51- Map <String , XmlAttribute > psiElementMap = new HashMap <>();
52- Set <XmlAttribute > yamlKeyValues = new HashSet <>();
53-
54- for (XmlTag xmlTag : PsiTreeUtil .getChildrenOfTypeAsList (psiFile .getFirstChild (), XmlTag .class )) {
55- if (xmlTag .getName ().equals ("container" )) {
56- for (XmlTag servicesTag : xmlTag .getSubTags ()) {
57- if (servicesTag .getName ().equals (root )) {
58- for (XmlTag parameterTag : servicesTag .getSubTags ()) {
59- if (parameterTag .getName ().equals (child )) {
60- XmlAttribute keyAttr = parameterTag .getAttribute (tagName );
61- if (keyAttr != null ) {
62- String parameterName = keyAttr .getValue ();
63- if (parameterName != null && StringUtils .isNotBlank (parameterName )) {
64- if (psiElementMap .containsKey (parameterName )) {
65- yamlKeyValues .add (psiElementMap .get (parameterName ));
66- yamlKeyValues .add (keyAttr );
67- } else {
68- psiElementMap .put (parameterName , keyAttr );
69- }
70- }
48+ // lazy value resolve
49+ if (value == null ) {
50+ value = xmlAttributeValue .getValue ();
51+ }
7152
72- }
73- }
74- }
53+ if (value .equals (key )) {
54+ found ++;
7555 }
76- }
77- }
78- }
7956
80- if (yamlKeyValues .size () > 0 ) {
81- for (PsiElement psiElement : yamlKeyValues ) {
82- XmlAttributeValue valueElement = ((XmlAttribute ) psiElement ).getValueElement ();
83- if (valueElement != null ) {
84- holder .registerProblem (valueElement , "Duplicate Key" , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
57+ if (found == 2 ) {
58+ holder .registerProblem (xmlAttributeValue , "Symfony: Duplicate Key" , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
59+ break ;
60+ }
8561 }
8662 }
8763 }
88-
8964 }
90-
91-
9265}
0 commit comments