5
5
import com .intellij .codeInspection .ProblemsHolder ;
6
6
import com .intellij .psi .PsiElement ;
7
7
import com .intellij .psi .PsiElementVisitor ;
8
- import com .intellij .psi .PsiFile ;
9
- import com .intellij .psi .util .PsiTreeUtil ;
10
8
import com .intellij .psi .xml .XmlAttribute ;
11
9
import com .intellij .psi .xml .XmlAttributeValue ;
12
- import com .intellij .psi .xml .XmlDocument ;
13
10
import com .intellij .psi .xml .XmlTag ;
14
11
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
15
- import org .apache .commons .lang .StringUtils ;
16
12
import org .jetbrains .annotations .NotNull ;
17
13
18
- import java .util .HashMap ;
19
- import java .util .HashSet ;
20
- import java .util .Map ;
21
- import java .util .Set ;
22
-
23
14
/**
24
15
* @author Daniel Espendiller <daniel@espendiller.net>
25
16
*/
@@ -34,59 +25,41 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
34
25
35
26
return new PsiElementVisitor () {
36
27
@ 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 );
40
34
}
41
35
};
42
36
}
43
37
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 ;
45
40
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 );
50
47
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
+ }
71
52
72
- }
73
- }
74
- }
53
+ if (value .equals (key )) {
54
+ found ++;
75
55
}
76
- }
77
- }
78
- }
79
56
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
+ }
85
61
}
86
62
}
87
63
}
88
-
89
64
}
90
-
91
-
92
65
}
0 commit comments