Search in sources :

Example 6 with XmlTag

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

the class HtmlUnknownTagInspectionBase method checkTag.

@Override
protected void checkTag(@NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    XmlElementDescriptor descriptorFromContext = XmlUtil.getDescriptorFromContext(tag);
    PsiElement parent = tag.getParent();
    XmlElementDescriptor parentDescriptor = parent instanceof XmlTag ? ((XmlTag) parent).getDescriptor() : null;
    XmlElementDescriptor ownDescriptor = isAbstractDescriptor(descriptorFromContext) ? tag.getDescriptor() : descriptorFromContext;
    if (isAbstractDescriptor(ownDescriptor) || (parentDescriptor instanceof HtmlElementDescriptorImpl && ownDescriptor instanceof HtmlElementDescriptorImpl && isAbstractDescriptor(descriptorFromContext))) {
        final String name = tag.getName();
        if (!isCustomValuesEnabled() || !isCustomValue(name)) {
            final AddCustomHtmlElementIntentionAction action = new AddCustomHtmlElementIntentionAction(TAG_KEY, name, XmlBundle.message("add.custom.html.tag", name));
            // todo: support "element is not allowed" message for html5
            // some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag)
            final String message = isAbstractDescriptor(ownDescriptor) ? XmlErrorMessages.message("unknown.html.tag", name) : XmlErrorMessages.message("element.is.not.allowed.here", name);
            final PsiElement startTagName = XmlTagUtil.getStartTagNameElement(tag);
            assert startTagName != null;
            final PsiElement endTagName = XmlTagUtil.getEndTagNameElement(tag);
            List<LocalQuickFix> quickfixes = new ArrayList<>();
            quickfixes.add(action);
            if (isOnTheFly) {
                PsiFile file = startTagName.getContainingFile();
                if (file instanceof XmlFile) {
                    quickfixes.add(XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(startTagName, "", null));
                }
                // People using non-HTML as their template data language (but having not changed this in the IDE)
                // will most likely see 'unknown html tag' error, because HTML is usually the default.
                // So if they check quick fixes for this error they'll discover Change Template Data Language feature.
                ContainerUtil.addIfNotNull(quickfixes, createChangeTemplateDataFix(file));
            }
            if (HtmlUtil.isHtml5Tag(name) && !HtmlUtil.hasNonHtml5Doctype(tag)) {
                quickfixes.add(new SwitchToHtml5WithHighPriorityAction());
            }
            ProblemHighlightType highlightType = tag.getContainingFile().getContext() == null ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.INFORMATION;
            if (startTagName.getTextLength() > 0) {
                holder.registerProblem(startTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
            if (endTagName != null) {
                holder.registerProblem(endTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) PsiFile(com.intellij.psi.PsiFile) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 7 with XmlTag

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

the class RemoveExtraClosingTagIntentionAction method doFix.

private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
    final XmlToken endNameToken = (XmlToken) element;
    final PsiElement tagElement = endNameToken.getParent();
    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement))
        return;
    if (tagElement instanceof PsiErrorElement) {
        tagElement.delete();
    } else {
        final ASTNode astNode = tagElement.getNode();
        if (astNode != null) {
            final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
            if (endTagStart != null) {
                final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
                if (document != null) {
                    document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
                }
            }
        }
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 8 with XmlTag

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

the class XmlNamespaceAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String namespace = tag.getNamespace();
        for (XmlNSColorProvider provider : PROVIDERS) {
            TextAttributesKey key = provider.getKeyForNamespace(namespace, tag);
            if (key != null) {
                TextRange range = XmlTagUtil.getStartTagRange(tag);
                if (range != null) {
                    holder.createInfoAnnotation(range, null).setTextAttributes(key);
                }
                TextRange endTagRange = XmlTagUtil.getEndTagRange(tag);
                if (endTagRange != null) {
                    holder.createInfoAnnotation(endTagRange, null).setTextAttributes(key);
                }
                return;
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with XmlTag

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

the class XmlNsPrefixAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
        return;
    PsiElement parent = element.getParent();
    if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
        return;
    TextRange elementRange = element.getTextRange();
    List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
    for (SchemaPrefixReference ref : references) {
        TextRange rangeInElement = ref.getRangeInElement();
        if (rangeInElement.isEmpty())
            continue;
        TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
        if (!range.intersects(elementRange))
            continue;
        holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with XmlTag

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

the class XmlUnusedNamespaceInspection method buildVisitor.

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

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            PsiFile file = holder.getFile();
            if (!(file instanceof XmlFile))
                return;
            XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
            if (refCountHolder == null)
                return;
            if (!attribute.isNamespaceDeclaration()) {
                checkUnusedLocations(attribute, holder, refCountHolder);
                return;
            }
            String namespace = attribute.getValue();
            String declaredPrefix = getDeclaredPrefix(attribute);
            if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
                ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
                for (ImplicitUsageProvider provider : implicitUsageProviders) {
                    if (provider.isImplicitUsage(attribute))
                        return;
                }
                XmlAttributeValue value = attribute.getValueElement();
                assert value != null;
                holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
                XmlTag parent = attribute.getParent();
                if (declaredPrefix.isEmpty()) {
                    XmlAttribute location = getDefaultLocation(parent);
                    if (location != null) {
                        holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                } else if (!refCountHolder.isUsedNamespace(namespace)) {
                    for (PsiReference reference : getLocationReferences(namespace, parent)) {
                        if (!XmlHighlightVisitor.hasBadResolve(reference, false))
                            holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                }
            }
        }
    };
}
Also used : XmlRefCountHolder(com.intellij.xml.util.XmlRefCountHolder) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlTag (com.intellij.psi.xml.XmlTag)673 XmlFile (com.intellij.psi.xml.XmlFile)151 PsiElement (com.intellij.psi.PsiElement)130 XmlAttribute (com.intellij.psi.xml.XmlAttribute)121 Nullable (org.jetbrains.annotations.Nullable)99 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)89 NotNull (org.jetbrains.annotations.NotNull)89 PsiFile (com.intellij.psi.PsiFile)71 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)52 VirtualFile (com.intellij.openapi.vfs.VirtualFile)50 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)50 Project (com.intellij.openapi.project.Project)48 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)47 ArrayList (java.util.ArrayList)43 XmlDocument (com.intellij.psi.xml.XmlDocument)39 DomElement (com.intellij.util.xml.DomElement)35 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 TextRange (com.intellij.openapi.util.TextRange)32 Result (com.intellij.openapi.application.Result)24 Document (com.intellij.openapi.editor.Document)24