Search in sources :

Example 1 with RemoveAttributeIntentionFix

use of com.intellij.codeInsight.daemon.impl.analysis.RemoveAttributeIntentionFix in project intellij-community by JetBrains.

the class JavaFxRedundantPropertyValueInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            super.visitXmlAttribute(attribute);
            final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
            if (!(descriptor instanceof JavaFxPropertyAttributeDescriptor))
                return;
            final String attributeName = attribute.getName();
            final String attributeValue = attribute.getValue();
            if (attributeValue == null || attributeValue.startsWith("$") || attributeValue.startsWith("#") || attributeValue.startsWith("%") || FxmlConstants.FX_ID.equals(attributeName) || FxmlConstants.FX_VALUE.equals(attributeName) || FxmlConstants.FX_CONSTANT.equals(attributeName) || FxmlConstants.FX_CONTROLLER.equals(attributeName)) {
                return;
            }
            final PsiClass tagClass = JavaFxPsiUtil.getTagClass(attribute.getParent());
            final String defaultValue = getDefaultValue(attributeName, tagClass);
            if (defaultValue == null)
                return;
            if (isEqualValue(tagClass, attributeValue, defaultValue, descriptor.getDeclaration())) {
                holder.registerProblem(attribute, "Attribute is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveAttributeIntentionFix(attributeName, attribute));
            }
        }

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (!(descriptor instanceof JavaFxPropertyTagDescriptor)) {
                return;
            }
            if (tag.getSubTags().length != 0)
                return;
            final String tagText = tag.getValue().getTrimmedText();
            if (tagText.startsWith("$") || tagText.startsWith("#") || tagText.startsWith("%")) {
                return;
            }
            final XmlTag parentTag = tag.getParentTag();
            if (parentTag == null)
                return;
            final PsiClass tagClass = JavaFxPsiUtil.getTagClass(parentTag);
            final String defaultValue = getDefaultValue(tag.getName(), tagClass);
            if (defaultValue == null)
                return;
            if (isEqualValue(tagClass, tagText, defaultValue, descriptor.getDeclaration())) {
                holder.registerProblem(tag, "Tag is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveTagIntentionFix(tag.getName(), tag));
            }
        }
    };
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) RemoveAttributeIntentionFix(com.intellij.codeInsight.daemon.impl.analysis.RemoveAttributeIntentionFix) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JavaFxPropertyAttributeDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor) RemoveTagIntentionFix(com.intellij.codeInsight.daemon.impl.analysis.RemoveTagIntentionFix) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RemoveAttributeIntentionFix (com.intellij.codeInsight.daemon.impl.analysis.RemoveAttributeIntentionFix)1 RemoveTagIntentionFix (com.intellij.codeInsight.daemon.impl.analysis.RemoveTagIntentionFix)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1 NotNull (org.jetbrains.annotations.NotNull)1 JavaFxPropertyAttributeDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor)1 JavaFxPropertyTagDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor)1