[devkit] InspectionMappingConsistencyInspection -> DOM

GitOrigin-RevId: e8b906536524dd2053fe2d6c77a77b9c88df2c4e
This commit is contained in:
Yann Cébron
2019-04-28 19:17:10 +03:00
committed by intellij-monorepo-bot
parent d7dd54eaba
commit 86f0e0c4ae
2 changed files with 70 additions and 78 deletions
@@ -1,35 +1,19 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.devkit.inspections;
import com.intellij.codeInsight.daemon.impl.analysis.InsertRequiredAttributeFix;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.InspectionEP;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomUtil;
import com.intellij.util.xml.GenericAttributeValue;
import com.intellij.util.xml.highlighting.AddDomElementQuickFix;
import com.intellij.util.xml.highlighting.BasicDomElementsInspection;
import com.intellij.util.xml.highlighting.DomElementAnnotationHolder;
import com.intellij.util.xml.highlighting.DomHighlightingHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.devkit.dom.Extension;
import org.jetbrains.idea.devkit.dom.ExtensionPoint;
@@ -40,65 +24,73 @@ import java.text.MessageFormat;
/**
* @author Dmitry Avdeev
*/
public class InspectionMappingConsistencyInspection extends DevKitInspectionBase {
public class InspectionMappingConsistencyInspection extends BasicDomElementsInspection<IdeaPlugin> {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder,
boolean isOnTheFly,
@NotNull LocalInspectionToolSession session) {
return new XmlElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
DomElement element = DomUtil.getDomElement(tag);
if (element instanceof Extension) {
ExtensionPoint extensionPoint = ((Extension)element).getExtensionPoint();
if (extensionPoint != null &&
InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) {
boolean key = tag.getAttribute("key") != null;
boolean groupKey = tag.getAttribute("groupKey") != null;
if (key) {
if (tag.getAttribute("bundle") == null) {
checkDefaultBundle(element, holder);
}
}
else if (tag.getAttribute("displayName") == null) {
registerProblem(element, holder, "displayName or key should be specified", "displayName", "key");
}
if (groupKey) {
if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) {
checkDefaultBundle(element, holder);
}
}
else if (tag.getAttribute("groupName") == null) {
registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey");
}
}
}
}
};
public InspectionMappingConsistencyInspection() {
super(IdeaPlugin.class);
}
private static void checkDefaultBundle(DomElement element, ProblemsHolder holder) {
IdeaPlugin plugin = DomUtil.getParentOfType(element, IdeaPlugin.class, true);
if (plugin != null && !DomUtil.hasXml(plugin.getResourceBundle())) {
registerProblem(element, holder, "Bundle should be specified");
@Override
protected void checkDomElement(DomElement element, DomElementAnnotationHolder holder, DomHighlightingHelper helper) {
if (!(element instanceof Extension)) {
return;
}
ExtensionPoint extensionPoint = ((Extension)element).getExtensionPoint();
if (extensionPoint == null ||
!InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), InspectionEP.class.getName())) {
return;
}
if (hasDefinedAttribute(element, "key")) {
if (!hasDefinedAttribute(element, "bundle")) {
checkDefaultBundle(element, holder);
}
}
else if (!hasDefinedAttribute(element, "displayName")) {
registerProblem(element, holder, "displayName or key should be specified", "displayName", "key");
}
if (hasDefinedAttribute(element, "groupKey")) {
if (!hasDefinedAttribute(element, "bundle") &&
!hasDefinedAttribute(element, "groupBundle")) {
checkDefaultBundle(element, holder);
}
}
else if (!hasDefinedAttribute(element, "groupName")) {
registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey");
}
}
private static void registerProblem(DomElement element, ProblemsHolder holder, String message, String... createAttrs) {
final Pair<TextRange, PsiElement> range = DomUtil.getProblemRange(element.getXmlTag());
holder.registerProblem(range.second, range.first, message,
holder.isOnTheFly()
? ContainerUtil.map(createAttrs,
s -> new InsertRequiredAttributeFix(
PsiTreeUtil.getParentOfType(range.second, XmlTag.class, false), s) {
@NotNull
@Override
public String getText() {
return MessageFormat.format("Insert ''{0}'' attribute", s);
}
}, new LocalQuickFix[createAttrs.length])
: LocalQuickFix.EMPTY_ARRAY);
private static boolean hasDefinedAttribute(DomElement element, String attributeName) {
final GenericAttributeValue attribute = PluginXmlDomInspection.getAttribute(element, attributeName);
return attribute != null && DomUtil.hasXml(attribute);
}
private static void checkDefaultBundle(DomElement element, DomElementAnnotationHolder holder) {
IdeaPlugin plugin = DomUtil.getParentOfType(element, IdeaPlugin.class, true);
if (plugin != null && !DomUtil.hasXml(plugin.getResourceBundle())) {
holder.createProblem(element, "Bundle should be specified",
new AddDomElementQuickFix<>(plugin.getResourceBundle()));
}
}
private static void registerProblem(DomElement element, DomElementAnnotationHolder holder, String message, String... createAttrs) {
if (holder.isOnTheFly()) {
holder.createProblem(element, message, ContainerUtil.map(createAttrs, attributeName -> {
final XmlTag tag = element.getXmlTag();
assert tag != null;
return new InsertRequiredAttributeFix(tag, attributeName) {
@NotNull
@Override
public String getText() {
return MessageFormat.format("Insert ''{0}'' attribute", attributeName);
}
};
}, new LocalQuickFix[createAttrs.length]));
}
else {
holder.createProblem(element, message);
}
}
}
@@ -621,7 +621,7 @@ public class PluginXmlDomInspection extends BasicDomElementsInspection<IdeaPlugi
}
@Nullable
private static GenericAttributeValue getAttribute(DomElement domElement, String attributeName) {
static GenericAttributeValue getAttribute(DomElement domElement, String attributeName) {
final DomAttributeChildDescription attributeDescription = domElement.getGenericInfo().getAttributeChildDescription(attributeName);
if (attributeDescription == null) {
return null;