Search in sources :

Example 1 with ProblemHighlightType

use of com.intellij.codeInspection.ProblemHighlightType 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 2 with ProblemHighlightType

use of com.intellij.codeInspection.ProblemHighlightType in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoUnresolvedReferenceInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitFieldName(@NotNull GoFieldName o) {
            super.visitFieldName(o);
            PsiElement resolve = o.resolve();
            if (resolve == null) {
                PsiElement id = o.getIdentifier();
                holder.registerProblem(id, "Unknown field <code>#ref</code> in struct literal #loc", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            super.visitReferenceExpression(o);
            GoReference reference = o.getReference();
            GoReferenceExpression qualifier = o.getQualifier();
            GoReference qualifierRef = qualifier != null ? qualifier.getReference() : null;
            PsiElement qualifierResolve = qualifierRef != null ? qualifierRef.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            ResolveResult[] results = reference.multiResolve(false);
            PsiElement id = o.getIdentifier();
            String name = id.getText();
            if (results.length > 1) {
                holder.registerProblem(id, "Ambiguous reference " + "'" + name + "'", GENERIC_ERROR_OR_WARNING);
            } else if (reference.resolve() == null) {
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited(o, qualifier)) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    boolean canBeLocal = PsiTreeUtil.getParentOfType(o, GoBlock.class) != null;
                    List<LocalQuickFix> fixesList = ContainerUtil.newArrayList(new GoIntroduceGlobalVariableFix(id, name));
                    if (canBeLocal) {
                        fixesList.add(new GoIntroduceLocalVariableFix(id, name));
                    }
                    PsiElement parent = o.getParent();
                    if (o.getReadWriteAccess() == ReadWriteAccessDetector.Access.Read) {
                        fixesList.add(new GoIntroduceGlobalConstantFix(id, name));
                        if (canBeLocal) {
                            fixesList.add(new GoIntroduceLocalConstantFix(id, name));
                        }
                    } else if (canBeLocal) {
                        PsiElement grandParent = parent.getParent();
                        if (grandParent instanceof GoAssignmentStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(grandParent));
                        } else if (parent instanceof GoRangeClause || parent instanceof GoRecvStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(parent));
                        }
                    }
                    if (parent instanceof GoCallExpr && PsiTreeUtil.getParentOfType(o, GoConstDeclaration.class) == null) {
                        fixesList.add(new GoIntroduceFunctionFix(parent, name));
                    }
                    fixes = fixesList.toArray(new LocalQuickFix[fixesList.size()]);
                }
                holder.registerProblem(id, "Unresolved reference " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }

        @Override
        public void visitImportSpec(@NotNull GoImportSpec o) {
            if (o.isCImport())
                return;
            GoImportString string = o.getImportString();
            if (string.getTextLength() < 2)
                return;
            PsiReference[] references = string.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof FileReference) {
                    ResolveResult[] resolveResults = ((FileReference) reference).multiResolve(false);
                    if (resolveResults.length == 0) {
                        ProblemHighlightType type = reference.getRangeInElement().isEmpty() ? GENERIC_ERROR_OR_WARNING : LIKE_UNKNOWN_SYMBOL;
                        holder.registerProblem(reference, ProblemsHolder.unresolvedReferenceMessage(reference), type);
                    }
                }
            }
        }

        @Override
        public void visitLabelRef(@NotNull GoLabelRef o) {
            PsiReference reference = o.getReference();
            String name = o.getText();
            if (reference.resolve() == null) {
                holder.registerProblem(o, "Unresolved label " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) {
            super.visitTypeReferenceExpression(o);
            PsiReference reference = o.getReference();
            GoTypeReferenceExpression qualifier = o.getQualifier();
            PsiElement qualifierResolve = qualifier != null ? qualifier.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            if (reference.resolve() == null) {
                PsiElement id = o.getIdentifier();
                String name = id.getText();
                boolean isProhibited = isProhibited(o, qualifier);
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    fixes = new LocalQuickFix[] { new GoIntroduceTypeFix(id, name) };
                }
                holder.registerProblem(id, "Unresolved type " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiReference(com.intellij.psi.PsiReference) GoReference(com.goide.psi.impl.GoReference) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ProblemHighlightType

use of com.intellij.codeInspection.ProblemHighlightType in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method visitCastExpression.

@Override
public void visitCastExpression(@NotNull GrTypeCastExpression expression) {
    super.visitCastExpression(expression);
    final GrExpression operand = expression.getOperand();
    if (operand == null)
        return;
    final PsiType actualType = operand.getType();
    if (actualType == null)
        return;
    if (expression.getCastTypeElement() == null)
        return;
    final PsiType expectedType = expression.getCastTypeElement().getType();
    final ConversionResult result = TypesUtil.canCast(expectedType, actualType, expression);
    if (result == ConversionResult.OK)
        return;
    final ProblemHighlightType highlightType = result == ConversionResult.ERROR ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
    final String message = GroovyBundle.message("cannot.cast", actualType.getPresentableText(), expectedType.getPresentableText());
    registerError(expression, highlightType, message);
}
Also used : ConversionResult(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.ConversionResult) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType)

Example 4 with ProblemHighlightType

use of com.intellij.codeInspection.ProblemHighlightType in project intellij-plugins by JetBrains.

the class DartAnnotator method createAnnotation.

@Nullable
private static Annotation createAnnotation(@NotNull final AnnotationHolder holder, @NotNull final DartServerData.DartError error, final int fileTextLength) {
    int highlightingStart = error.getOffset();
    int highlightingEnd = error.getOffset() + error.getLength();
    if (highlightingEnd > fileTextLength)
        highlightingEnd = fileTextLength;
    if (highlightingStart > 0 && highlightingStart >= highlightingEnd)
        highlightingStart = highlightingEnd - 1;
    final TextRange textRange = new TextRange(highlightingStart, highlightingEnd);
    final String severity = error.getSeverity();
    final String message = StringUtil.notNullize(error.getMessage());
    final ProblemHighlightType specialHighlightType = getSpecialHighlightType(message);
    final Annotation annotation;
    if (AnalysisErrorSeverity.INFO.equals(severity) && specialHighlightType == null) {
        annotation = holder.createWeakWarningAnnotation(textRange, message);
        annotation.setTextAttributes(DartSyntaxHighlighterColors.HINT);
    } else if (AnalysisErrorSeverity.WARNING.equals(severity) || (AnalysisErrorSeverity.INFO.equals(severity) && specialHighlightType != null)) {
        annotation = holder.createWarningAnnotation(textRange, message);
        annotation.setTextAttributes(DartSyntaxHighlighterColors.WARNING);
    } else if (AnalysisErrorSeverity.ERROR.equals(severity)) {
        annotation = holder.createErrorAnnotation(textRange, message);
        annotation.setTextAttributes(DartSyntaxHighlighterColors.ERROR);
    } else {
        annotation = null;
    }
    if (annotation != null && specialHighlightType != null) {
        annotation.setTextAttributes(null);
        annotation.setHighlightType(specialHighlightType);
    }
    return annotation;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) Annotation(com.intellij.lang.annotation.Annotation) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 PsiElement (com.intellij.psi.PsiElement)2 GoReference (com.goide.psi.impl.GoReference)1 Annotation (com.intellij.lang.annotation.Annotation)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 ResolveResult (com.intellij.psi.ResolveResult)1 HtmlTag (com.intellij.psi.html.HtmlTag)1 HtmlElementDescriptorImpl (com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl)1 FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)1