IDEA-199839 Support deprecation in xsd schemas

detecting with regexp
This commit is contained in:
Dmitry Avdeev
2018-10-09 17:12:18 +03:00
parent 5524d127cd
commit cf4be07b9b
3 changed files with 65 additions and 7 deletions

View File

@@ -2,10 +2,10 @@
<element name="root">
<complexType>
<sequence>
<!-- deprecated -->
<!-- deprecated element -->
<element name="deprecated"/>
</sequence>
<!-- deprecated -->
<!-- DEPRECATED attribute -->
<attribute name="deprecated"/>
</complexType>
</element>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.codeInsight.daemon.impl.analysis.XmlDeprecatedElementInspection.OptionsPanel">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="48"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f9b6b" class="javax.swing.JTextField" binding="myTextField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<vspacer id="6d333">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="45cd5" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Regexp:"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -14,19 +14,26 @@ 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.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
import javax.swing.*;
import java.util.regex.Pattern;
public class XmlDeprecatedElementInspection extends XmlSuppressableInspectionTool {
@Language("RegExp")
public String regexp = "(?i)deprecated.*";
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
Pattern pattern = Pattern.compile(regexp);
return new XmlElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
if (checkDeprecated(tag.getDescriptor())) {
if (checkDeprecated(tag.getDescriptor(), pattern)) {
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);
@@ -36,19 +43,36 @@ public class XmlDeprecatedElementInspection extends XmlSuppressableInspectionToo
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
if (checkDeprecated(attribute.getDescriptor())) {
if (checkDeprecated(attribute.getDescriptor(), pattern)) {
holder.registerProblem(attribute.getNameElement(), "The attribute is marked as deprecated", ProblemHighlightType.LIKE_DEPRECATED);
}
}
};
}
private static boolean checkDeprecated(@Nullable PsiMetaData metaData) {
@Nullable
@Override
public JComponent createOptionsPanel() {
return new OptionsPanel(this).myPanel;
}
private static boolean checkDeprecated(@Nullable PsiMetaData metaData, Pattern pattern) {
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(), "<!--").toLowerCase(Locale.ENGLISH).trim().startsWith("deprecated");
String s = StringUtil.trimStart(comment.getText(), "<!--").trim();
return pattern.matcher(s).matches();
}
public static class OptionsPanel {
private JTextField myTextField;
private JPanel myPanel;
public OptionsPanel(XmlDeprecatedElementInspection inspection) {
myTextField.setText(inspection.regexp);
myTextField.addActionListener(e -> inspection.regexp = myTextField.getText());
}
}
}