|
3 | 3 | import com.intellij.codeInspection.LocalInspectionTool;
|
4 | 4 | import com.intellij.codeInspection.ProblemHighlightType;
|
5 | 5 | import com.intellij.codeInspection.ProblemsHolder;
|
6 |
| -import com.intellij.lang.Language; |
7 |
| -import com.intellij.lang.xml.XMLLanguage; |
8 | 6 | import com.intellij.openapi.util.NotNullLazyValue;
|
9 | 7 | import com.intellij.psi.PsiElement;
|
10 | 8 | import com.intellij.psi.PsiElementVisitor;
|
|
23 | 21 | import org.apache.commons.lang3.StringUtils;
|
24 | 22 | import org.jetbrains.annotations.NotNull;
|
25 | 23 | import org.jetbrains.annotations.Nullable;
|
26 |
| -import org.jetbrains.yaml.YAMLLanguage; |
27 | 24 | import org.jetbrains.yaml.YAMLTokenTypes;
|
28 | 25 | import org.jetbrains.yaml.psi.YAMLCompoundValue;
|
29 | 26 | import org.jetbrains.yaml.psi.YAMLKeyValue;
|
|
34 | 31 | /**
|
35 | 32 | * @author Daniel Espendiller <daniel@espendiller.net>
|
36 | 33 | */
|
37 |
| -public class TaggedExtendsInterfaceClassInspection extends LocalInspectionTool { |
| 34 | +public class TaggedExtendsInterfaceClassInspection { |
| 35 | + public static class TaggedExtendsInterfaceClassInspectionYaml extends LocalInspectionTool { |
| 36 | + public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 37 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 38 | + return super.buildVisitor(holder, isOnTheFly); |
| 39 | + } |
38 | 40 |
|
39 |
| - @NotNull |
40 |
| - @Override |
41 |
| - public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
42 |
| - if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
43 |
| - return super.buildVisitor(holder, isOnTheFly); |
44 |
| - } |
| 41 | + return new PsiElementVisitor() { |
| 42 | + private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector; |
45 | 43 |
|
46 |
| - return new PsiElementVisitor() { |
47 |
| - private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector; |
| 44 | + @Override |
| 45 | + public void visitElement(@NotNull PsiElement element) { |
| 46 | + visitYamlElement(element, holder); |
| 47 | + super.visitElement(element); |
| 48 | + } |
48 | 49 |
|
49 |
| - @Override |
50 |
| - public void visitElement(@NotNull PsiElement element) { |
51 |
| - Language language = element.getLanguage(); |
| 50 | + private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) { |
| 51 | + if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) { |
52 | 52 |
|
53 |
| - if (language == YAMLLanguage.INSTANCE) { |
54 |
| - visitYamlElement(element, holder, this.createLazyServiceCollector()); |
55 |
| - } else if (language == XMLLanguage.INSTANCE) { |
56 |
| - visitXmlElement(element, holder, this.createLazyServiceCollector()); |
57 |
| - } |
| 53 | + // class: '\Foo' |
| 54 | + String text = PsiElementUtils.trimQuote(psiElement.getText()); |
| 55 | + if (StringUtils.isBlank(text)) { |
| 56 | + return; |
| 57 | + } |
58 | 58 |
|
59 |
| - super.visitElement(element); |
60 |
| - } |
| 59 | + PsiElement yamlScalar = psiElement.getParent(); |
| 60 | + if (!(yamlScalar instanceof YAMLScalar)) { |
| 61 | + return; |
| 62 | + } |
61 | 63 |
|
62 |
| - private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() { |
63 |
| - if (this.serviceCollector == null) { |
64 |
| - this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject())); |
| 64 | + PsiElement classKey = yamlScalar.getParent(); |
| 65 | + if (classKey instanceof YAMLKeyValue) { |
| 66 | + PsiElement yamlCompoundValue = classKey.getParent(); |
| 67 | + if (yamlCompoundValue instanceof YAMLCompoundValue) { |
| 68 | + PsiElement serviceKeyValue = yamlCompoundValue.getParent(); |
| 69 | + if (serviceKeyValue instanceof YAMLKeyValue) { |
| 70 | + Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue); |
| 71 | + if (!tags.isEmpty()) { |
| 72 | + registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector()); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) { |
| 78 | + // Foobar\Foo: ~ |
| 79 | + String text = PsiElementUtils.getText(psiElement); |
| 80 | + if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) { |
| 81 | + PsiElement yamlKeyValue = psiElement.getParent(); |
| 82 | + if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) { |
| 83 | + Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue); |
| 84 | + if (!tags.isEmpty()) { |
| 85 | + registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
65 | 90 | }
|
66 | 91 |
|
67 |
| - return this.serviceCollector; |
68 |
| - } |
69 |
| - }; |
70 |
| - } |
| 92 | + private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() { |
| 93 | + if (this.serviceCollector == null) { |
| 94 | + this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject())); |
| 95 | + } |
71 | 96 |
|
72 |
| - private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) { |
73 |
| - String className = getClassNameFromServiceDefinition(element); |
74 |
| - if (className != null) { |
75 |
| - XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class); |
76 |
| - if (parentOfType != null) { |
77 |
| - // attach problems to string value only |
78 |
| - PsiElement[] psiElements = element.getChildren(); |
79 |
| - if (psiElements.length > 2) { |
80 |
| - registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, lazyServiceCollector); |
| 97 | + return this.serviceCollector; |
81 | 98 | }
|
82 |
| - } |
| 99 | + }; |
83 | 100 | }
|
84 | 101 | }
|
85 | 102 |
|
86 |
| - private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) { |
87 |
| - if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) { |
| 103 | + public static class TaggedExtendsInterfaceClassInspectionXml extends LocalInspectionTool { |
| 104 | + private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector; |
88 | 105 |
|
89 |
| - // class: '\Foo' |
90 |
| - String text = PsiElementUtils.trimQuote(psiElement.getText()); |
91 |
| - if (StringUtils.isBlank(text)) { |
92 |
| - return; |
| 106 | + public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 107 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 108 | + return super.buildVisitor(holder, isOnTheFly); |
93 | 109 | }
|
94 | 110 |
|
95 |
| - PsiElement yamlScalar = psiElement.getParent(); |
96 |
| - if (!(yamlScalar instanceof YAMLScalar)) { |
97 |
| - return; |
98 |
| - } |
| 111 | + return new PsiElementVisitor() { |
| 112 | + private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector; |
| 113 | + |
| 114 | + @Override |
| 115 | + public void visitElement(@NotNull PsiElement element) { |
| 116 | + visitXmlElement(element, holder); |
| 117 | + super.visitElement(element); |
| 118 | + } |
99 | 119 |
|
100 |
| - PsiElement classKey = yamlScalar.getParent(); |
101 |
| - if (classKey instanceof YAMLKeyValue) { |
102 |
| - PsiElement yamlCompoundValue = classKey.getParent(); |
103 |
| - if (yamlCompoundValue instanceof YAMLCompoundValue) { |
104 |
| - PsiElement serviceKeyValue = yamlCompoundValue.getParent(); |
105 |
| - if (serviceKeyValue instanceof YAMLKeyValue) { |
106 |
| - Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue); |
107 |
| - if (!tags.isEmpty()) { |
108 |
| - registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector); |
| 120 | + private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) { |
| 121 | + String className = getClassNameFromServiceDefinition(element); |
| 122 | + if (className != null) { |
| 123 | + XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class); |
| 124 | + if (parentOfType != null) { |
| 125 | + // attach problems to string value only |
| 126 | + PsiElement[] psiElements = element.getChildren(); |
| 127 | + if (psiElements.length > 2) { |
| 128 | + registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, createLazyServiceCollector()); |
| 129 | + } |
109 | 130 | }
|
110 | 131 | }
|
111 | 132 | }
|
112 |
| - } |
113 |
| - } else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) { |
114 |
| - // Foobar\Foo: ~ |
115 |
| - String text = PsiElementUtils.getText(psiElement); |
116 |
| - if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) { |
117 |
| - PsiElement yamlKeyValue = psiElement.getParent(); |
118 |
| - if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) { |
119 |
| - Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue); |
120 |
| - if (!tags.isEmpty()) { |
121 |
| - registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector); |
| 133 | + |
| 134 | + private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() { |
| 135 | + if (this.serviceCollector == null) { |
| 136 | + this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject())); |
122 | 137 | }
|
| 138 | + |
| 139 | + return this.serviceCollector; |
123 | 140 | }
|
124 |
| - } |
| 141 | + }; |
125 | 142 | }
|
126 | 143 | }
|
127 | 144 |
|
128 |
| - private void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) { |
| 145 | + private static void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) { |
129 | 146 | if (tags.isEmpty()) {
|
130 | 147 | return;
|
131 | 148 | }
|
|
0 commit comments