Search in sources :

Example 21 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-community by JetBrains.

the class RegExpAnnotator method visitRegExpChar.

@Override
public void visitRegExpChar(final RegExpChar ch) {
    final PsiElement child = ch.getFirstChild();
    final IElementType type = child.getNode().getElementType();
    if (type == StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN) {
        myHolder.createErrorAnnotation(ch, "Illegal/unsupported escape sequence");
        return;
    } else if (type == RegExpTT.BAD_HEX_VALUE) {
        myHolder.createErrorAnnotation(ch, "Illegal hexadecimal escape sequence");
        return;
    } else if (type == RegExpTT.BAD_OCT_VALUE) {
        myHolder.createErrorAnnotation(ch, "Illegal octal escape sequence");
        return;
    } else if (type == StringEscapesTokenTypes.INVALID_UNICODE_ESCAPE_TOKEN) {
        myHolder.createErrorAnnotation(ch, "Illegal unicode escape sequence");
        return;
    }
    final String text = ch.getUnescapedText();
    if (type == RegExpTT.ESC_CTRL_CHARACTER && text.equals("\\b") && !myLanguageHosts.supportsLiteralBackspace(ch)) {
        myHolder.createErrorAnnotation(ch, "Illegal/unsupported escape sequence");
    }
    if (text.startsWith("\\") && myLanguageHosts.isRedundantEscape(ch, text)) {
        final ASTNode astNode = ch.getNode().getFirstChildNode();
        if (astNode != null && astNode.getElementType() == RegExpTT.REDUNDANT_ESCAPE) {
            final Annotation a = myHolder.createWeakWarningAnnotation(ch, "Redundant character escape");
            registerFix(a, new RemoveRedundantEscapeAction(ch));
        }
    }
    final RegExpChar.Type charType = ch.getType();
    if (charType == RegExpChar.Type.HEX || charType == RegExpChar.Type.UNICODE) {
        if (ch.getValue() == -1) {
            myHolder.createErrorAnnotation(ch, "Illegal unicode escape sequence");
            return;
        }
        if (text.charAt(text.length() - 1) == '}') {
            if (!myLanguageHosts.supportsExtendedHexCharacter(ch)) {
                myHolder.createErrorAnnotation(ch, "This hex character syntax is not supported in this regex dialect");
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 22 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-community by JetBrains.

the class RegExpAnnotator method visitPosixBracketExpression.

@Override
public void visitPosixBracketExpression(RegExpPosixBracketExpression posixBracketExpression) {
    final String className = posixBracketExpression.getClassName();
    if (!POSIX_CHARACTER_CLASSES.contains(className)) {
        final ASTNode node = posixBracketExpression.getNode().findChildByType(RegExpTT.NAME);
        if (node != null) {
            final Annotation annotation = myHolder.createErrorAnnotation(node, "Unknown POSIX character class");
            annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
        }
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) Annotation(com.intellij.lang.annotation.Annotation)

Example 23 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-community by JetBrains.

the class RegExpAnnotator method visitRegExpProperty.

@Override
public void visitRegExpProperty(RegExpProperty property) {
    final ASTNode category = property.getCategoryNode();
    if (category == null) {
        return;
    }
    if (!myLanguageHosts.isValidCategory(category.getPsi(), category.getText())) {
        final Annotation a = myHolder.createErrorAnnotation(category, "Unknown character category");
        a.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) Annotation(com.intellij.lang.annotation.Annotation)

Example 24 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-community by JetBrains.

the class RegExpAnnotator method visitRegExpNamedGroupRef.

@Override
public void visitRegExpNamedGroupRef(RegExpNamedGroupRef groupRef) {
    if (!myLanguageHosts.supportsNamedGroupRefSyntax(groupRef)) {
        myHolder.createErrorAnnotation(groupRef, "This named group reference syntax is not supported in this regex dialect");
        return;
    }
    if (groupRef.getGroupName() == null) {
        return;
    }
    final RegExpGroup group = groupRef.resolve();
    if (group == null) {
        final ASTNode node = groupRef.getNode().findChildByType(RegExpTT.NAME);
        if (node != null) {
            final Annotation a = myHolder.createErrorAnnotation(node, "Unresolved named group reference");
            a.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
        }
    } else if (PsiTreeUtil.isAncestor(group, groupRef, true)) {
        myHolder.createWarningAnnotation(groupRef, "Group reference is nested into the named group it refers to");
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) Annotation(com.intellij.lang.annotation.Annotation)

Example 25 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-community by JetBrains.

the class InspectionValidatorWrapper method runXmlFileSchemaValidation.

private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation(@NotNull XmlFile xmlFile) {
    final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile));
    final List<ExternalAnnotator> annotators = ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile);
    for (ExternalAnnotator<?, ?> annotator : annotators) {
        processAnnotator(xmlFile, holder, annotator);
    }
    if (!holder.hasAnnotations())
        return Collections.emptyMap();
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<>();
    for (final Annotation annotation : holder) {
        final HighlightInfo info = HighlightInfo.fromAnnotation(annotation);
        if (info.getSeverity() == HighlightSeverity.INFORMATION)
            continue;
        final PsiElement startElement = xmlFile.findElementAt(info.startOffset);
        final PsiElement endElement = info.startOffset == info.endOffset ? startElement : xmlFile.findElementAt(info.endOffset - 1);
        if (startElement == null || endElement == null)
            continue;
        final ProblemDescriptor descriptor = myInspectionManager.createProblemDescriptor(startElement, endElement, info.getDescription(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false);
        final HighlightDisplayLevel level = info.getSeverity() == HighlightSeverity.ERROR ? HighlightDisplayLevel.ERROR : HighlightDisplayLevel.WARNING;
        problemsMap.put(descriptor, level);
    }
    return problemsMap;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Aggregations

Annotation (com.intellij.lang.annotation.Annotation)97 PsiElement (com.intellij.psi.PsiElement)24 ASTNode (com.intellij.lang.ASTNode)22 TextRange (com.intellij.openapi.util.TextRange)19 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)15 Project (com.intellij.openapi.project.Project)11 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)8 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)6 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)4 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)4 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)3 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)3 PsiReference (com.intellij.psi.PsiReference)3 IElementType (com.intellij.psi.tree.IElementType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 Nullable (org.jetbrains.annotations.Nullable)3 HbCloseBlockMustache (com.dmarcotte.handlebars.psi.HbCloseBlockMustache)2