Search in sources :

Example 91 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.

the class JavaFxColorRgbInspection 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 String attributeValue = attribute.getValue();
            if (attributeValue == null)
                return;
            final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
            if (descriptor instanceof JavaFxPropertyAttributeDescriptor) {
                final PsiClass psiClass = ((JavaFxPropertyAttributeDescriptor) descriptor).getPsiClass();
                if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
                    final XmlAttributeValue valueElement = attribute.getValueElement();
                    final PsiElement location = valueElement != null ? valueElement : attribute;
                    validateColorComponent(psiClass, attribute.getName(), attributeValue, location);
                }
            }
        }

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            if (tag.getSubTags().length != 0)
                return;
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (descriptor instanceof JavaFxPropertyTagDescriptor) {
                final PsiClass psiClass = ((JavaFxPropertyTagDescriptor) descriptor).getPsiClass();
                if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
                    final XmlTagValue valueElement = tag.getValue();
                    final XmlText[] textElements = valueElement.getTextElements();
                    final PsiElement location = textElements.length == 1 ? textElements[0] : tag;
                    validateColorComponent(psiClass, tag.getName(), valueElement.getTrimmedText(), location);
                }
            }
        }

        private void validateColorComponent(@NotNull PsiClass psiClass, @NotNull String propertyName, @NotNull String propertyValue, @NotNull PsiElement location) {
            final PsiMember declaration = JavaFxPsiUtil.getWritableProperties(psiClass).get(propertyName);
            final String boxedQName = JavaFxPsiUtil.getBoxedPropertyType(psiClass, declaration);
            if (CommonClassNames.JAVA_LANG_FLOAT.equals(boxedQName) || CommonClassNames.JAVA_LANG_DOUBLE.equals(boxedQName)) {
                try {
                    double value = Double.parseDouble(propertyValue);
                    if (value < 0.0 || value > 1.0) {
                        holder.registerProblem(location, "Color component has to be a number between 0.0 and 1.0, inclusively");
                    }
                } catch (NumberFormatException ignored) {
                }
            }
        }
    };
}
Also used : NotNull(org.jetbrains.annotations.NotNull) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JavaFxPropertyAttributeDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 92 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.

the class JavaFxDefaultTagInspection method buildVisitor.

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

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (descriptor instanceof JavaFxPropertyTagDescriptor) {
                final PsiClass parentTagClass = JavaFxPsiUtil.getTagClass(tag.getParentTag());
                if (parentTagClass != null) {
                    final String propertyName = JavaFxPsiUtil.getDefaultPropertyName(parentTagClass);
                    final String tagName = tag.getName();
                    if (Comparing.strEqual(tagName, propertyName) && !isCollectionAssignment(parentTagClass, propertyName, tag)) {
                        final TextRange startTagRange = XmlTagUtil.getStartTagRange(tag);
                        final TextRange rangeInElement = startTagRange != null ? startTagRange.shiftRight(-tag.getTextOffset()) : null;
                        holder.registerProblem(tag, rangeInElement, "Default property tag could be removed", new UnwrapTagFix(tagName));
                    }
                }
            }
        }
    };
}
Also used : JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) TextRange(com.intellij.openapi.util.TextRange) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 93 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor 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)

Example 94 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.

the class JavaFxBuiltInTagDescriptor method getAttributesDescriptors.

@Override
public XmlAttributeDescriptor[] getAttributesDescriptors(@Nullable XmlTag context) {
    final List<XmlAttributeDescriptor> descriptors = new ArrayList<>();
    final List<String> builtInAttributeNames = FxmlConstants.FX_BUILT_IN_TAG_SUPPORTED_ATTRIBUTES.get(getName());
    if (builtInAttributeNames != null) {
        for (String attrName : builtInAttributeNames) {
            descriptors.add(JavaFxBuiltInAttributeDescriptor.create(attrName, getName()));
        }
    }
    JavaFxClassTagDescriptorBase.collectStaticAttributesDescriptors(context, descriptors);
    final XmlTag referencedTag = getReferencedTag(myXmlTag);
    if (referencedTag != null) {
        final XmlElementDescriptor referencedDescriptor = referencedTag.getDescriptor();
        if (referencedDescriptor != null) {
            final XmlAttributeDescriptor[] attributesDescriptors = referencedDescriptor.getAttributesDescriptors(referencedTag);
            if (attributesDescriptors != null) {
                Collections.addAll(descriptors, attributesDescriptors);
            }
        }
    }
    final XmlTag includedRoot = getIncludedRoot(context);
    if (includedRoot != null) {
        final XmlElementDescriptor includedRootDescriptor = includedRoot.getDescriptor();
        if (includedRootDescriptor instanceof JavaFxClassTagDescriptorBase) {
            ((JavaFxClassTagDescriptorBase) includedRootDescriptor).collectInstanceProperties(descriptors);
        }
    }
    return descriptors.isEmpty() ? XmlAttributeDescriptor.EMPTY : descriptors.toArray(XmlAttributeDescriptor.EMPTY);
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ArrayList(java.util.ArrayList) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 95 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.

the class JavaFxClassTagDescriptorBase method getElementDescriptor.

@Nullable
@Override
public XmlElementDescriptor getElementDescriptor(XmlTag childTag, XmlTag contextTag) {
    final String name = childTag.getName();
    if (FxmlConstants.FX_BUILT_IN_TAGS.contains(name)) {
        return new JavaFxBuiltInTagDescriptor(name, childTag);
    }
    if (FxmlConstants.FX_ROOT.equals(name)) {
        return new JavaFxRootTagDescriptor(childTag);
    }
    final String shortName = StringUtil.getShortName(name);
    if (!name.equals(shortName)) {
        //static property
        final PsiMethod propertySetter = JavaFxPsiUtil.findStaticPropertySetter(name, childTag);
        if (propertySetter != null) {
            return new JavaFxPropertyTagDescriptor(propertySetter.getContainingClass(), shortName, true);
        }
        final Project project = childTag.getProject();
        if (JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project)) == null) {
            return null;
        }
    }
    final PsiClass psiClass = getPsiClass();
    if (psiClass != null) {
        final String parentTagName = contextTag.getName();
        if (!FxmlConstants.FX_DEFINE.equals(parentTagName)) {
            if (FxmlConstants.FX_ROOT.equals(parentTagName)) {
                final Map<String, PsiMember> properties = JavaFxPsiUtil.getWritableProperties(psiClass);
                if (properties.get(name) != null) {
                    return new JavaFxPropertyTagDescriptor(psiClass, name, false);
                }
            } else {
                final JavaFxPropertyTagDescriptor defaultPropertyDescriptor = getDefaultPropertyDescriptor();
                if (defaultPropertyDescriptor != null) {
                    final String defaultPropertyName = defaultPropertyDescriptor.getName();
                    if (StringUtil.equalsIgnoreCase(defaultPropertyName, name) && !StringUtil.equals(defaultPropertyName, name)) {
                        final XmlElementDescriptor childDescriptor = defaultPropertyDescriptor.getElementDescriptor(childTag, contextTag);
                        if (childDescriptor != null) {
                            return childDescriptor;
                        }
                    }
                }
                final Map<String, PsiMember> properties = JavaFxPsiUtil.getWritableProperties(psiClass);
                if (properties.get(name) != null) {
                    return new JavaFxPropertyTagDescriptor(psiClass, name, false);
                }
            }
        }
    }
    if (name.length() != 0 && Character.isLowerCase(name.charAt(0))) {
        return new JavaFxPropertyTagDescriptor(psiClass, name, false);
    }
    return new JavaFxClassTagDescriptor(name, childTag);
}
Also used : Project(com.intellij.openapi.project.Project) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)159 XmlTag (com.intellij.psi.xml.XmlTag)88 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)60 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)54 PsiElement (com.intellij.psi.PsiElement)23 XmlFile (com.intellij.psi.xml.XmlFile)23 Nullable (org.jetbrains.annotations.Nullable)23 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)22 PsiFile (com.intellij.psi.PsiFile)11 ArrayList (java.util.ArrayList)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 NotNull (org.jetbrains.annotations.NotNull)10 ClassBackedElementDescriptor (com.intellij.javascript.flex.mxml.schema.ClassBackedElementDescriptor)7 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)7 Project (com.intellij.openapi.project.Project)7 HtmlTag (com.intellij.psi.html.HtmlTag)7 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)5 XmlDocument (com.intellij.psi.xml.XmlDocument)5 DElementPattern (org.kohsuke.rngom.digested.DElementPattern)5 InvalidPropertyException (com.intellij.flex.uiDesigner.InvalidPropertyException)4