Search in sources :

Example 16 with Annotation

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

the class PyBuiltinAnnotator method highlightAsAttribute.

/**
   * Try to highlight a node as a class attribute.
   *
   * @param node what to work with
   * @return true iff the node was highlighted.
   */
private boolean highlightAsAttribute(@NotNull PyQualifiedExpression node, @NotNull String name) {
    final LanguageLevel languageLevel = LanguageLevel.forElement(node);
    if (PyNames.UnderscoredAttributes.contains(name) || PyNames.getBuiltinMethods(languageLevel).containsKey(name)) {
        // things like __len__: foo.__len__ or class Foo: ... __len__ = my_len_impl
        if (node.isQualified() || ScopeUtil.getScopeOwner(node) instanceof PyClass) {
            final ASTNode astNode = node.getNode();
            if (astNode != null) {
                // only the id, not all qualifiers subtree
                final ASTNode tgt = astNode.findChildByType(PyTokenTypes.IDENTIFIER);
                if (tgt != null) {
                    final Annotation ann = getHolder().createInfoAnnotation(tgt, null);
                    ann.setTextAttributes(PyHighlighter.PY_PREDEFINED_USAGE);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Annotation(com.intellij.lang.annotation.Annotation)

Example 17 with Annotation

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

the class PyDefinitionsAnnotator method visitPyClass.

@Override
public void visitPyClass(PyClass node) {
    final ASTNode name_node = node.getNameNode();
    if (name_node != null) {
        Annotation ann = getHolder().createInfoAnnotation(name_node, null);
        ann.setTextAttributes(PyHighlighter.PY_CLASS_DEFINITION);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) Annotation(com.intellij.lang.annotation.Annotation)

Example 18 with Annotation

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

the class ReferenceAnnotator method addError.

private void addError(PsiReference reference) {
    final TextRange rangeInElement = reference.getRangeInElement();
    final TextRange range = TextRange.from(reference.getElement().getTextRange().getStartOffset() + rangeInElement.getStartOffset(), rangeInElement.getLength());
    final Annotation annotation;
    if (reference instanceof EmptyResolveMessageProvider) {
        final String s = ((EmptyResolveMessageProvider) reference).getUnresolvedMessagePattern();
        annotation = myHolder.createErrorAnnotation(range, MessageFormat.format(s, reference.getCanonicalText()));
    } else {
        annotation = myHolder.createErrorAnnotation(range, "Cannot resolve symbol");
    }
    annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
    if (reference instanceof LocalQuickFixProvider) {
        LocalQuickFix[] fixes = ((LocalQuickFixProvider) reference).getQuickFixes();
        if (fixes != null) {
            InspectionManager inspectionManager = InspectionManager.getInstance(reference.getElement().getProject());
            for (LocalQuickFix fix : fixes) {
                ProblemDescriptor descriptor = inspectionManager.createProblemDescriptor(reference.getElement(), annotation.getMessage(), fix, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, true);
                annotation.registerFix(fix, null, null, descriptor);
            }
        }
    }
}
Also used : EmptyResolveMessageProvider(com.intellij.codeInsight.daemon.EmptyResolveMessageProvider) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation)

Example 19 with Annotation

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

the class RegExpAnnotator method visitRegExpBackref.

@Override
public void visitRegExpBackref(final RegExpBackref backref) {
    final RegExpGroup group = backref.resolve();
    if (group == null) {
        final Annotation a = myHolder.createErrorAnnotation(backref, "Unresolved back reference");
        a.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
    } else if (PsiTreeUtil.isAncestor(group, backref, true)) {
        myHolder.createWarningAnnotation(backref, "Back reference is nested into the capturing group it refers to");
    }
}
Also used : Annotation(com.intellij.lang.annotation.Annotation)

Example 20 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)

Aggregations

Annotation (com.intellij.lang.annotation.Annotation)98 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 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)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 Module (com.intellij.openapi.module.Module)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