Search in sources :

Example 1 with HighlightInfoType

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

the class XmlHighlightVisitor method doCheckRefs.

private void doCheckRefs(final PsiElement value, final PsiReference[] references, int start) {
    for (int i = start; i < references.length; ++i) {
        PsiReference reference = references[i];
        ProgressManager.checkCanceled();
        if (isUrlReference(reference))
            continue;
        if (!hasBadResolve(reference, false)) {
            continue;
        }
        String description = getErrorDescription(reference);
        final int startOffset = reference.getElement().getTextRange().getStartOffset();
        final TextRange referenceRange = reference.getRangeInElement();
        // logging for IDEADEV-29655
        if (referenceRange.getStartOffset() > referenceRange.getEndOffset()) {
            LOG.error("Reference range start offset > end offset:  " + reference + ", start offset: " + referenceRange.getStartOffset() + ", end offset: " + referenceRange.getEndOffset());
        }
        HighlightInfoType type = getTagProblemInfoType(PsiTreeUtil.getParentOfType(value, XmlTag.class));
        if (value instanceof XmlAttributeValue) {
            PsiElement parent = value.getParent();
            if (parent instanceof XmlAttribute) {
                String name = ((XmlAttribute) parent).getName().toLowerCase();
                if (type.getSeverity(null).compareTo(HighlightInfoType.WARNING.getSeverity(null)) > 0 && name.endsWith("stylename")) {
                    type = HighlightInfoType.WARNING;
                }
            }
        }
        HighlightInfo info = HighlightInfo.newHighlightInfo(type).range(startOffset + referenceRange.getStartOffset(), startOffset + referenceRange.getEndOffset()).descriptionAndTooltip(description).create();
        addToResults(info);
        if (reference instanceof LocalQuickFixProvider) {
            LocalQuickFix[] fixes = ((LocalQuickFixProvider) reference).getQuickFixes();
            if (fixes != null) {
                InspectionManager manager = InspectionManager.getInstance(reference.getElement().getProject());
                for (LocalQuickFix fix : fixes) {
                    ProblemDescriptor descriptor = manager.createProblemDescriptor(value, description, fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true);
                    QuickFixAction.registerQuickFixAction(info, new LocalQuickFixAsIntentionAdapter(fix, descriptor));
                }
            }
        }
        UnresolvedReferenceQuickFixProvider.registerReferenceFixes(reference, new QuickFixActionRegistrarImpl(info));
    }
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) QuickFixActionRegistrarImpl(com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl)

Example 2 with HighlightInfoType

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

the class XmlHighlightVisitor method visitXmlAttributeValue.

@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
    checkReferences(value);
    final PsiElement parent = value.getParent();
    if (!(parent instanceof XmlAttribute)) {
        return;
    }
    XmlAttribute attribute = (XmlAttribute) parent;
    XmlTag tag = attribute.getParent();
    XmlElementDescriptor elementDescriptor = tag.getDescriptor();
    XmlAttributeDescriptor attributeDescriptor = elementDescriptor != null ? elementDescriptor.getAttributeDescriptor(attribute) : null;
    if (attributeDescriptor != null && !skipValidation(value)) {
        String error = attributeDescriptor.validateValue(value, attribute.getValue());
        if (error != null) {
            HighlightInfoType type = getTagProblemInfoType(tag);
            addToResults(HighlightInfo.newHighlightInfo(type).range(value).descriptionAndTooltip(error).create());
        }
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Example 3 with HighlightInfoType

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

the class XmlHighlightVisitor method reportAttributeProblem.

@Nullable
private HighlightInfo reportAttributeProblem(final XmlTag tag, final String localName, final XmlAttribute attribute, @NotNull String localizedMessage) {
    final RemoveAttributeIntentionFix removeAttributeIntention = new RemoveAttributeIntentionFix(localName, attribute);
    if (!(tag instanceof HtmlTag)) {
        final HighlightInfoType tagProblemInfoType = HighlightInfoType.WRONG_REF;
        final ASTNode node = SourceTreeToPsiMap.psiElementToTree(attribute);
        assert node != null;
        final ASTNode child = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(node);
        assert child != null;
        final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(tagProblemInfoType).range(child).descriptionAndTooltip(localizedMessage).create();
        addToResults(highlightInfo);
        QuickFixAction.registerQuickFixAction(highlightInfo, removeAttributeIntention);
        return highlightInfo;
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) HtmlTag(com.intellij.psi.html.HtmlTag) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with HighlightInfoType

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

the class XmlHighlightVisitor method checkAttribute.

private void checkAttribute(XmlAttribute attribute) {
    XmlTag tag = attribute.getParent();
    if (tag == null)
        return;
    final String name = attribute.getName();
    PsiElement prevLeaf = PsiTreeUtil.prevLeaf(attribute);
    if (!(prevLeaf instanceof PsiWhiteSpace)) {
        TextRange textRange = attribute.getTextRange();
        HighlightInfoType type = tag instanceof HtmlTag ? HighlightInfoType.WARNING : HighlightInfoType.ERROR;
        String description = XmlErrorMessages.message("attribute.should.be.preceded.with.space");
        HighlightInfo info = HighlightInfo.newHighlightInfo(type).range(textRange.getStartOffset(), textRange.getStartOffset()).descriptionAndTooltip(description).create();
        addToResults(info);
    }
    if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(attribute.getNamespace())) {
        //checkReferences(attribute.getValueElement());
        return;
    }
    XmlElementDescriptor elementDescriptor = tag.getDescriptor();
    if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor || ourDoJaxpTesting) {
        return;
    }
    XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
    if (attributeDescriptor == null) {
        if (!XmlUtil.attributeFromTemplateFramework(name, tag)) {
            final String localizedMessage = XmlErrorMessages.message("attribute.is.not.allowed.here", name);
            final HighlightInfo highlightInfo = reportAttributeProblem(tag, name, attribute, localizedMessage);
            if (highlightInfo != null) {
                PsiFile file = tag.getContainingFile();
                if (file != null) {
                    for (XmlUndefinedElementFixProvider fixProvider : Extensions.getExtensions(XmlUndefinedElementFixProvider.EP_NAME)) {
                        IntentionAction[] fixes = fixProvider.createFixes(attribute);
                        if (fixes != null) {
                            for (IntentionAction action : fixes) {
                                QuickFixAction.registerQuickFixAction(highlightInfo, action);
                            }
                            break;
                        }
                    }
                }
            }
        }
    } else {
        checkDuplicateAttribute(tag, attribute);
        // we skip resolve of attribute references since there is separate check when taking attribute descriptors
        PsiReference[] attrRefs = attribute.getReferences();
        doCheckRefs(attribute, attrRefs, !attribute.getNamespacePrefix().isEmpty() ? 2 : 1);
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) HtmlTag(com.intellij.psi.html.HtmlTag) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor)

Example 5 with HighlightInfoType

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

the class XmlUnboundNsPrefixInspection method checkUnboundNamespacePrefix.

private static void checkUnboundNamespacePrefix(final XmlElement element, final XmlTag context, String namespacePrefix, final XmlToken token, final ProblemsHolder holder, boolean isOnTheFly) {
    if (namespacePrefix.isEmpty() && (!(element instanceof XmlTag) || !(element.getParent() instanceof XmlDocument)) || XML.equals(namespacePrefix)) {
        return;
    }
    final String namespaceByPrefix = context.getNamespaceByPrefix(namespacePrefix);
    if (!namespaceByPrefix.isEmpty()) {
        return;
    }
    PsiFile psiFile = context.getContainingFile();
    if (!(psiFile instanceof XmlFile))
        return;
    final XmlFile containingFile = (XmlFile) psiFile;
    if (!HighlightingLevelManager.getInstance(containingFile.getProject()).shouldInspect(containingFile))
        return;
    final XmlExtension extension = XmlExtension.getExtension(containingFile);
    if (extension.getPrefixDeclaration(context, namespacePrefix) != null) {
        return;
    }
    final String localizedMessage = isOnTheFly ? XmlErrorMessages.message("unbound.namespace", namespacePrefix) : XmlErrorMessages.message("unbound.namespace.no.param");
    if (namespacePrefix.isEmpty()) {
        final XmlTag tag = (XmlTag) element;
        if (!XmlUtil.JSP_URI.equals(tag.getNamespace())) {
            LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
            reportTagProblem(tag, localizedMessage, null, ProblemHighlightType.INFORMATION, fix, holder);
        }
        return;
    }
    final int prefixLength = namespacePrefix.length();
    final TextRange range = new TextRange(0, prefixLength);
    final HighlightInfoType infoType = extension.getHighlightInfoType(containingFile);
    final ProblemHighlightType highlightType = infoType == HighlightInfoType.ERROR ? ProblemHighlightType.ERROR : ProblemHighlightType.LIKE_UNKNOWN_SYMBOL;
    if (element instanceof XmlTag) {
        LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
        reportTagProblem(element, localizedMessage, range, highlightType, fix, holder);
    } else if (element instanceof XmlAttribute) {
        LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(element, namespacePrefix, token) : null;
        XmlAttribute attribute = (XmlAttribute) element;
        holder.registerProblem(attribute.getNameElement(), localizedMessage, highlightType, range, fix);
    } else {
        holder.registerProblem(element, localizedMessage, highlightType, range);
    }
}
Also used : XmlExtension(com.intellij.xml.XmlExtension) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Aggregations

HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)22 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)14 TextRange (com.intellij.openapi.util.TextRange)8 Nullable (org.jetbrains.annotations.Nullable)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)4 SeveritiesProvider (com.intellij.codeInsight.daemon.impl.SeveritiesProvider)3 NotNull (org.jetbrains.annotations.NotNull)3 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)2 QuickFixActionRegistrarImpl (com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl)1 SeverityEditorDialog (com.intellij.codeInspection.ex.SeverityEditorDialog)1 ASTNode (com.intellij.lang.ASTNode)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Module (com.intellij.openapi.module.Module)1 AttributesDescriptor (com.intellij.openapi.options.colors.AttributesDescriptor)1