From 8d78ec1a0fc0e097aec5d2ec0eec25be1788b42f Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Thu, 4 Oct 2018 14:43:33 +0300 Subject: [PATCH] IDEA-199839 Support deprecation in xsd schemas --- .../src/messages/XmlBundle.properties | 1 + .../src/META-INF/XmlPlugin.xml | 4 ++ .../XmlDocumentationProvider.java | 26 ++------- .../daemon/XmlInspectionsTest.java | 6 +++ xml/tests/testData/xml/deprecated.xml | 3 ++ xml/tests/testData/xml/deprecated.xsd | 12 +++++ .../XmlDeprecatedElement.html | 6 +++ .../XmlDeprecatedElementInspection.java | 54 +++++++++++++++++++ .../src/com/intellij/xml/util/XmlUtil.java | 20 +++++++ 9 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 xml/tests/testData/xml/deprecated.xml create mode 100644 xml/tests/testData/xml/deprecated.xsd create mode 100644 xml/xml-analysis-impl/resources/inspectionDescriptions/XmlDeprecatedElement.html create mode 100644 xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDeprecatedElementInspection.java diff --git a/platform/platform-resources-en/src/messages/XmlBundle.properties b/platform/platform-resources-en/src/messages/XmlBundle.properties index ba44654546c3..75657f39a4b8 100644 --- a/platform/platform-resources-en/src/messages/XmlBundle.properties +++ b/platform/platform-resources-en/src/messages/XmlBundle.properties @@ -172,6 +172,7 @@ xml.inspections.unused.schema.remove=Remove unused namespace declaration xml.inspections.path.resolve=File path resolving in XML xml.inspections.default.attribute.value=Redundant default attribute value assignment +xml.inspections.deprecated=Deprecated elements xml.inspections.global=XML highlighting diff --git a/platform/platform-resources/src/META-INF/XmlPlugin.xml b/platform/platform-resources/src/META-INF/XmlPlugin.xml index b71698891b03..01c79fab533d 100644 --- a/platform/platform-resources/src/META-INF/XmlPlugin.xml +++ b/platform/platform-resources/src/META-INF/XmlPlugin.xml @@ -438,6 +438,10 @@ bundle="messages.XmlBundle" key="xml.inspections.default.attribute.value" groupBundle="messages.XmlBundle" groupKey="xml.inspections.group.name" implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlDefaultAttributeValueInspection"/> + ".length()).trim(); diff --git a/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java b/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java index 3bfc6045b430..7423367b0dcf 100644 --- a/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java +++ b/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.daemon; import com.intellij.codeInsight.daemon.impl.analysis.XmlDefaultAttributeValueInspection; +import com.intellij.codeInsight.daemon.impl.analysis.XmlDeprecatedElementInspection; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.ide.highlighter.HtmlFileType; import com.intellij.ide.highlighter.XmlFileType; @@ -72,6 +73,11 @@ public class XmlInspectionsTest extends LightPlatformCodeInsightFixtureTestCase myFixture.testHighlighting("def.xml", "def.xsd"); } + public void testDeprecations() { + myFixture.enableInspections(new XmlDeprecatedElementInspection()); + myFixture.testHighlighting("deprecated.xml", "deprecated.xsd"); + } + @Override protected String getTestDataPath() { return PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') + "/xml/tests/testData/xml"; diff --git a/xml/tests/testData/xml/deprecated.xml b/xml/tests/testData/xml/deprecated.xml new file mode 100644 index 000000000000..fcab3b59b147 --- /dev/null +++ b/xml/tests/testData/xml/deprecated.xml @@ -0,0 +1,3 @@ +deprecated=""> + <deprecated/> + \ No newline at end of file diff --git a/xml/tests/testData/xml/deprecated.xsd b/xml/tests/testData/xml/deprecated.xsd new file mode 100644 index 000000000000..167ca3c71bb4 --- /dev/null +++ b/xml/tests/testData/xml/deprecated.xsd @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/xml-analysis-impl/resources/inspectionDescriptions/XmlDeprecatedElement.html b/xml/xml-analysis-impl/resources/inspectionDescriptions/XmlDeprecatedElement.html new file mode 100644 index 000000000000..62d7a2e1c2a2 --- /dev/null +++ b/xml/xml-analysis-impl/resources/inspectionDescriptions/XmlDeprecatedElement.html @@ -0,0 +1,6 @@ + + +This inspection checks for deprecated XML elements. +

The elements can be marked by XML comment or documentation tag with text "deprecated". + + \ No newline at end of file diff --git a/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDeprecatedElementInspection.java b/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDeprecatedElementInspection.java new file mode 100644 index 000000000000..0177561c9d18 --- /dev/null +++ b/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDeprecatedElementInspection.java @@ -0,0 +1,54 @@ +// Copyright 2000-2018 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 com.intellij.codeInsight.daemon.impl.analysis; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.XmlSuppressableInspectionTool; +import com.intellij.lang.ASTNode; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.XmlElementVisitor; +import com.intellij.psi.meta.PsiMetaData; +import com.intellij.psi.xml.XmlAttribute; +import com.intellij.psi.xml.XmlChildRole; +import com.intellij.psi.xml.XmlTag; +import com.intellij.xml.util.XmlUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Locale; + +public class XmlDeprecatedElementInspection extends XmlSuppressableInspectionTool { + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { + return new XmlElementVisitor() { + @Override + public void visitXmlTag(XmlTag tag) { + if (checkDeprecated(tag.getDescriptor())) { + ASTNode nameNode = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()); + if (nameNode != null) { + holder.registerProblem(nameNode.getPsi(), "The tag is marked as deprecated", ProblemHighlightType.LIKE_DEPRECATED); + } + } + } + + @Override + public void visitXmlAttribute(XmlAttribute attribute) { + if (checkDeprecated(attribute.getDescriptor())) { + holder.registerProblem(attribute.getNameElement(), "The attribute is marked as deprecated", ProblemHighlightType.LIKE_DEPRECATED); + } + } + }; + } + + private static boolean checkDeprecated(@Nullable PsiMetaData metaData) { + if (metaData == null) return false; + PsiElement declaration = metaData.getDeclaration(); + if (declaration == null) return false; + PsiElement comment = XmlUtil.findPreviousComment(declaration); + if (comment == null) return false; + return StringUtil.trimStart(comment.getText(), "