Search in sources :

Example 16 with AnyXmlElementDescriptor

use of com.intellij.xml.impl.schema.AnyXmlElementDescriptor in project intellij-community by JetBrains.

the class XmlUnboundNsPrefixInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        private Boolean isXml;

        private boolean isXmlFile(XmlElement element) {
            if (isXml == null) {
                final PsiFile file = element.getContainingFile();
                isXml = file instanceof XmlFile && !InjectedLanguageManager.getInstance(element.getProject()).isInjectedFragment(file);
            }
            return isXml.booleanValue();
        }

        @Override
        public void visitXmlToken(final XmlToken token) {
            if (isXmlFile(token) && token.getTokenType() == XmlTokenType.XML_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkUnboundNamespacePrefix(tag, tag, tag.getNamespacePrefix(), token, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            if (!isXmlFile(attribute)) {
                return;
            }
            final String namespace = attribute.getNamespace();
            if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(namespace)) {
                return;
            }
            XmlTag tag = attribute.getParent();
            if (tag == null)
                return;
            XmlElementDescriptor elementDescriptor = tag.getDescriptor();
            if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
                return;
            }
            final String name = attribute.getName();
            checkUnboundNamespacePrefix(attribute, tag, XmlUtil.findPrefixByQualifiedName(name), null, holder, isOnTheFly);
        }

        @Override
        public void visitXmlAttributeValue(XmlAttributeValue value) {
            PsiReference[] references = value.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof SchemaPrefixReference) {
                    if (!XML.equals(((SchemaPrefixReference) reference).getNamespacePrefix()) && reference.resolve() == null) {
                        holder.registerProblem(reference, XmlErrorMessages.message("unbound.namespace", ((SchemaPrefixReference) reference).getNamespacePrefix()), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                    }
                }
            }
        }
    };
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with AnyXmlElementDescriptor

use of com.intellij.xml.impl.schema.AnyXmlElementDescriptor in project intellij-community by JetBrains.

the class HtmlUnknownAttributeInspectionBase method checkAttribute.

@Override
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final XmlTag tag = attribute.getParent();
    if (tag instanceof HtmlTag) {
        XmlElementDescriptor elementDescriptor = tag.getDescriptor();
        if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
            return;
        }
        XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
        if (attributeDescriptor == null && !attribute.isNamespaceDeclaration()) {
            final String name = attribute.getName();
            if (!XmlUtil.attributeFromTemplateFramework(name, tag) && (!isCustomValuesEnabled() || !isCustomValue(name))) {
                boolean maySwitchToHtml5 = HtmlUtil.isCustomHtml5Attribute(name) && !HtmlUtil.hasNonHtml5Doctype(tag);
                LocalQuickFix[] quickfixes = new LocalQuickFix[maySwitchToHtml5 ? 3 : 2];
                quickfixes[0] = new AddCustomHtmlElementIntentionAction(ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.attribute", name));
                quickfixes[1] = new RemoveAttributeIntentionAction(name);
                if (maySwitchToHtml5) {
                    quickfixes[2] = new SwitchToHtml5WithHighPriorityAction();
                }
                registerProblemOnAttributeName(attribute, XmlErrorMessages.message("attribute.is.not.allowed.here", attribute.getName()), holder, quickfixes);
            }
        }
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 18 with AnyXmlElementDescriptor

use of com.intellij.xml.impl.schema.AnyXmlElementDescriptor in project intellij-community by JetBrains.

the class RequiredAttributesInspectionBase method checkTag.

@Override
protected void checkTag(@NotNull XmlTag tag, @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    String name = tag.getName();
    XmlElementDescriptor elementDescriptor = XmlUtil.getDescriptorFromContext(tag);
    if (elementDescriptor instanceof AnyXmlElementDescriptor || elementDescriptor == null) {
        elementDescriptor = tag.getDescriptor();
    }
    if (elementDescriptor == null)
        return;
    if ((elementDescriptor instanceof XmlHighlightingAwareElementDescriptor) && !((XmlHighlightingAwareElementDescriptor) elementDescriptor).shouldCheckRequiredAttributes()) {
        return;
    }
    XmlAttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributesDescriptors(tag);
    Set<String> requiredAttributes = null;
    for (XmlAttributeDescriptor attribute : attributeDescriptors) {
        if (attribute != null && attribute.isRequired()) {
            if (requiredAttributes == null) {
                requiredAttributes = new HashSet<>();
            }
            requiredAttributes.add(attribute.getName(tag));
        }
    }
    if (requiredAttributes != null) {
        for (final String attrName : requiredAttributes) {
            if (!hasAttribute(tag, attrName) && !XmlExtension.getExtension(tag.getContainingFile()).isRequiredAttributeImplicitlyPresent(tag, attrName)) {
                LocalQuickFix insertRequiredAttributeIntention = XmlQuickFixFactory.getInstance().insertRequiredAttributeFix(tag, attrName);
                final String localizedMessage = XmlErrorMessages.message("element.doesnt.have.required.attribute", name, attrName);
                reportOneTagProblem(tag, attrName, localizedMessage, insertRequiredAttributeIntention, holder, getIntentionAction(attrName));
            }
        }
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlHighlightingAwareElementDescriptor(com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightingAwareElementDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor)

Example 19 with AnyXmlElementDescriptor

use of com.intellij.xml.impl.schema.AnyXmlElementDescriptor in project intellij-plugins by JetBrains.

the class AngularJSTagDescriptorsProvider method getDescriptor.

@Nullable
@Override
public XmlElementDescriptor getDescriptor(XmlTag xmlTag) {
    final Project project = xmlTag.getProject();
    if (!(xmlTag instanceof HtmlTag && AngularIndexUtil.hasAngularJS(project)))
        return null;
    final String tagName = xmlTag.getName();
    final String directiveName = DirectiveUtil.normalizeAttributeName(tagName);
    final XmlNSDescriptor nsDescriptor = xmlTag.getNSDescriptor(xmlTag.getNamespace(), false);
    final XmlElementDescriptor descriptor = nsDescriptor != null ? nsDescriptor.getElementDescriptor(xmlTag) : null;
    if (descriptor != null && !(descriptor instanceof AnyXmlElementDescriptor)) {
        return null;
    }
    if ((NG_CONTAINER.equals(directiveName) || NG_CONTENT.equals(directiveName) || NG_TEMPLATE.equals(directiveName)) && AngularIndexUtil.hasAngularJS2(project)) {
        return new AngularJSTagDescriptor(directiveName, createDirective(xmlTag, directiveName));
    }
    JSImplicitElement directive = DirectiveUtil.getTagDirective(directiveName, project);
    if (DirectiveUtil.isAngular2Directive(directive) && !directive.getName().equals(tagName)) {
        // we've found directive via normalized name for Angular, it should not work
        directive = null;
    }
    if (directive == null && !tagName.equals(directiveName) && AngularIndexUtil.hasAngularJS2(project)) {
        directive = DirectiveUtil.getTagDirective(tagName, project);
        if (!DirectiveUtil.isAngular2Directive(directive))
            directive = null;
    }
    return directive != null ? new AngularJSTagDescriptor(directiveName, directive) : null;
}
Also used : Project(com.intellij.openapi.project.Project) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) HtmlTag(com.intellij.psi.html.HtmlTag) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)19 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)15 XmlTag (com.intellij.psi.xml.XmlTag)8 HtmlTag (com.intellij.psi.html.HtmlTag)6 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)3 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)3 Nullable (org.jetbrains.annotations.Nullable)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 PsiElement (com.intellij.psi.PsiElement)2 XmlExtension (com.intellij.xml.XmlExtension)2 ArrayList (java.util.ArrayList)2 Validator (com.intellij.codeInsight.daemon.Validator)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)1 XmlHighlightingAwareElementDescriptor (com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightingAwareElementDescriptor)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 RelaxedHtmlFromSchemaElementDescriptor (com.intellij.html.impl.RelaxedHtmlFromSchemaElementDescriptor)1 JSImplicitElement (com.intellij.lang.javascript.psi.stubs.JSImplicitElement)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1