Search in sources :

Example 1 with Annotation

use of com.intellij.lang.annotation.Annotation in project idea-handlebars by dmarcotte.

the class HbBlockMismatchAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof HbOpenBlockMustache) {
        HbOpenBlockMustache openBlockMustache = (HbOpenBlockMustache) element;
        HbMustacheName openBlockMustacheName = openBlockMustache.getBlockMustacheName();
        HbCloseBlockMustache closeBlockMustache = openBlockMustache.getPairedElement();
        if (closeBlockMustache != null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (openBlockMustacheName == null || closeBlockMustacheName == null) {
                return;
            }
            String openBlockName = openBlockMustacheName.getName();
            String closeBlockName = closeBlockMustacheName.getName();
            if (!openBlockName.equals(closeBlockName)) {
                Annotation openBlockAnnotation = holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.open.block", openBlockName, closeBlockName));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                Annotation closeBlockAnnotation = holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.close.block", openBlockName, closeBlockName));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
            }
        } else {
            holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.end.block", openBlockMustache.getName()));
        }
    }
    if (element instanceof HbCloseBlockMustache) {
        HbCloseBlockMustache closeBlockMustache = (HbCloseBlockMustache) element;
        PsiElement openBlockElement = closeBlockMustache.getPairedElement();
        if (openBlockElement == null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (closeBlockMustacheName == null) {
                return;
            }
            holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.start.block", closeBlockMustache.getName()));
        }
    }
}
Also used : HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbMustacheName(com.dmarcotte.handlebars.psi.HbMustacheName) HbCloseBlockMustache(com.dmarcotte.handlebars.psi.HbCloseBlockMustache) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement)

Example 2 with Annotation

use of com.intellij.lang.annotation.Annotation in project scss-lint-plugin by idok.

the class ScssLintAnnotationResult method createAnnotation.

@Nullable
public static Annotation createAnnotation(@NotNull AnnotationHolder holder, @NotNull HighlightSeverity severity, @Nullable TextAttributes forcedTextAttributes, @NotNull TextRange range, @NotNull String message) {
    if (forcedTextAttributes != null) {
        Annotation annotation = createAnnotation(holder, severity, range, message);
        annotation.setEnforcedTextAttributes(forcedTextAttributes);
        return annotation;
    }
    return createAnnotation(holder, severity, range, message);
}
Also used : Annotation(com.intellij.lang.annotation.Annotation) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Annotation

use of com.intellij.lang.annotation.Annotation in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoAnnotator method annotate.

// todo: unify with DlvApi.Variable.Kind
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (!(element instanceof GoCompositeElement) || !element.isValid())
        return;
    if (element instanceof GoPackageClause) {
        PsiElement identifier = ((GoPackageClause) element).getIdentifier();
        if (identifier != null && identifier.textMatches("_")) {
            holder.createErrorAnnotation(identifier, "Invalid package name");
            return;
        }
    }
    if (element instanceof GoContinueStatement) {
        if (!(PsiTreeUtil.getParentOfType(element, GoForStatement.class, GoFunctionLit.class) instanceof GoForStatement)) {
            Annotation annotation = holder.createErrorAnnotation(element, "Continue statement not inside a for loop");
            annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element));
        }
    } else if (element instanceof GoBreakStatement) {
        if (GoPsiImplUtil.getBreakStatementOwner(element) == null) {
            Annotation annotation = holder.createErrorAnnotation(element, "Break statement not inside a for loop, select or switch");
            annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element));
        }
    } else if (element instanceof GoReferenceExpression) {
        GoReferenceExpression reference = (GoReferenceExpression) element;
        PsiElement resolvedReference = reference.resolve();
        if (resolvedReference instanceof PsiDirectory || resolvedReference instanceof GoImportSpec) {
            // It's a package reference. It should either be inside a package clause or part of a larger reference expression.
            if (!(element.getParent() instanceof GoReferenceExpression) && PsiTreeUtil.getParentOfType(reference, GoPackageClause.class) == null) {
                holder.createErrorAnnotation(element, "Use of package " + element.getText() + " without selector");
            }
        }
        if (resolvedReference instanceof GoTypeSpec && isIllegalUseOfTypeAsExpression(reference)) {
            holder.createErrorAnnotation(element, "Type " + element.getText() + " is not an expression");
        }
        if (resolvedReference instanceof GoConstDefinition && resolvedReference.getParent() instanceof GoConstSpec && PsiTreeUtil.getParentOfType(element, GoConstDeclaration.class) != null) {
            checkSelfReference((GoReferenceExpression) element, resolvedReference, holder);
        }
        if (resolvedReference instanceof GoVarDefinition && resolvedReference.getParent() instanceof GoVarSpec && PsiTreeUtil.getParentOfType(element, GoVarDeclaration.class) != null) {
            checkSelfReference((GoReferenceExpression) element, resolvedReference, holder);
        }
    } else if (element instanceof GoLiteralTypeExpr) {
        if (isIllegalUseOfTypeAsExpression(element)) {
            holder.createErrorAnnotation(element, "Type " + element.getText() + " is not an expression");
        }
    } else if (element instanceof GoCompositeLit) {
        GoCompositeLit literal = (GoCompositeLit) element;
        if (literal.getType() instanceof GoMapType) {
            GoLiteralValue literalValue = literal.getLiteralValue();
            if (literalValue != null) {
                for (GoElement literalElement : literalValue.getElementList()) {
                    if (literalElement.getKey() == null) {
                        holder.createErrorAnnotation(literalElement, "Missing key in map literal");
                    }
                }
            }
        }
    } else if (element instanceof GoTypeAssertionExpr) {
        GoType type = ((GoTypeAssertionExpr) element).getExpression().getGoType(null);
        if (type != null) {
            GoType underlyingType = type.getUnderlyingType();
            if (!(underlyingType instanceof GoInterfaceType)) {
                String message = String.format("Invalid type assertion: %s, (non-interface type %s on left)", element.getText(), type.getText());
                holder.createErrorAnnotation(((GoTypeAssertionExpr) element).getExpression(), message);
            }
        }
    } else if (element instanceof GoBuiltinCallExpr) {
        GoBuiltinCallExpr call = (GoBuiltinCallExpr) element;
        if ("make".equals(call.getReferenceExpression().getText())) {
            checkMakeCall(call, holder);
        }
    } else if (element instanceof GoCallExpr) {
        GoCallExpr call = (GoCallExpr) element;
        GoExpression callExpression = call.getExpression();
        if (GoInspectionUtil.getFunctionResultCount(call) == 0) {
            PsiElement parent = call.getParent();
            boolean simpleStatement = parent instanceof GoLeftHandExprList && parent.getParent() instanceof GoSimpleStatement;
            boolean inDeferOrGo = parent instanceof GoDeferStatement || parent instanceof GoGoStatement;
            if (!simpleStatement && !inDeferOrGo) {
                holder.createErrorAnnotation(call, call.getText() + " used as value");
            }
        }
        if (callExpression instanceof GoReferenceExpression) {
            GoReferenceExpression reference = (GoReferenceExpression) callExpression;
            if (reference.textMatches("cap")) {
                if (GoPsiImplUtil.builtin(reference.resolve())) {
                    checkCapCall(call, holder);
                }
            }
        }
    } else if (element instanceof GoTopLevelDeclaration) {
        if (element.getParent() instanceof GoFile) {
            if (element instanceof GoTypeDeclaration) {
                for (GoTypeSpec spec : ((GoTypeDeclaration) element).getTypeSpecList()) {
                    if (spec.getIdentifier().textMatches(GoConstants.INIT)) {
                        holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                    }
                }
            } else if (element instanceof GoVarDeclaration) {
                for (GoVarSpec spec : ((GoVarDeclaration) element).getVarSpecList()) {
                    for (GoVarDefinition definition : spec.getVarDefinitionList()) {
                        if (definition.getIdentifier().textMatches(GoConstants.INIT)) {
                            holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                        }
                    }
                }
            } else if (element instanceof GoConstDeclaration) {
                for (GoConstSpec spec : ((GoConstDeclaration) element).getConstSpecList()) {
                    for (GoConstDefinition definition : spec.getConstDefinitionList()) {
                        if (definition.getIdentifier().textMatches(GoConstants.INIT)) {
                            holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                        }
                    }
                }
            } else if (element instanceof GoFunctionDeclaration) {
                GoFunctionDeclaration declaration = (GoFunctionDeclaration) element;
                if (declaration.getIdentifier().textMatches(GoConstants.INIT) || declaration.getIdentifier().textMatches(GoConstants.MAIN) && GoConstants.MAIN.equals(declaration.getContainingFile().getPackageName())) {
                    GoSignature signature = declaration.getSignature();
                    if (signature != null) {
                        GoResult result = signature.getResult();
                        if (result != null && !result.isVoid()) {
                            Annotation annotation = holder.createErrorAnnotation(result, declaration.getName() + " function must have no arguments and no return values");
                            annotation.registerFix(new GoEmptySignatureQuickFix(declaration));
                        }
                        GoParameters parameters = signature.getParameters();
                        if (!parameters.getParameterDeclarationList().isEmpty()) {
                            Annotation annotation = holder.createErrorAnnotation(parameters, declaration.getName() + " function must have no arguments and no return values");
                            annotation.registerFix(new GoEmptySignatureQuickFix(declaration));
                        }
                    }
                }
            }
        }
    } else if (element instanceof GoIndexOrSliceExpr) {
        GoIndexOrSliceExpr slice = (GoIndexOrSliceExpr) element;
        GoExpression expr = slice.getExpression();
        GoExpression thirdIndex = slice.getIndices().third;
        if (expr == null || thirdIndex == null) {
            return;
        }
        if (GoTypeUtil.isString(expr.getGoType(null))) {
            ASTNode[] colons = slice.getNode().getChildren(TokenSet.create(GoTypes.COLON));
            if (colons.length == 2) {
                PsiElement secondColon = colons[1].getPsi();
                TextRange r = TextRange.create(secondColon.getTextRange().getStartOffset(), thirdIndex.getTextRange().getEndOffset());
                Annotation annotation = holder.createErrorAnnotation(r, "Invalid operation " + slice.getText() + " (3-index slice of string)");
                annotation.registerFix(new GoDeleteRangeQuickFix(secondColon, thirdIndex, "Delete third index"));
            }
        }
    }
}
Also used : GoReplaceWithReturnStatementQuickFix(com.goide.quickfix.GoReplaceWithReturnStatementQuickFix) PsiDirectory(com.intellij.psi.PsiDirectory) PsiElement(com.intellij.psi.PsiElement) GoDeleteRangeQuickFix(com.goide.quickfix.GoDeleteRangeQuickFix) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) GoEmptySignatureQuickFix(com.goide.quickfix.GoEmptySignatureQuickFix)

Example 4 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-elixir by KronicDeth.

the class Module method annotate.

@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull com.intellij.lang.annotation.AnnotationHolder annotationHolder) {
    if (psiElement instanceof ErlangAtom) {
        ErlangAtom erlangAtom = (ErlangAtom) psiElement;
        String name = erlangAtom.getName();
        if (name.startsWith(ELIXIR_ALIAS_PREFIX)) {
            Project project = psiElement.getProject();
            Collection<NamedElement> namedElementCollection = StubIndex.getElements(AllName.KEY, name, project, GlobalSearchScope.allScope(project), NamedElement.class);
            if (namedElementCollection.size() > 0) {
                TextRange textRange = psiElement.getTextRange();
                String unprefixedName = name.substring(ELIXIR_ALIAS_PREFIX.length(), name.length());
                Annotation annotation = annotationHolder.createInfoAnnotation(textRange, "Resolves to Elixir Module " + unprefixedName);
                annotation.setTextAttributes(DefaultLanguageHighlighterColors.LINE_COMMENT);
            } else {
                TextRange textRange = psiElement.getTextRange();
                annotationHolder.createErrorAnnotation(textRange, "Unresolved Elixir Module");
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) TextRange(com.intellij.openapi.util.TextRange) ErlangAtom(org.intellij.erlang.psi.ErlangAtom) NamedElement(org.elixir_lang.psi.NamedElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 5 with Annotation

use of com.intellij.lang.annotation.Annotation in project Main by SpartanRefactoring.

the class SpartanizerAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement e, @NotNull AnnotationHolder h) {
    try {
        if (!Spartanizer.canTip(e) || e.getContainingFile().getName().contains("Spartanizer"))
            return;
        Annotation annotation = h.createInfoAnnotation(e, "Spartanize This!");
        annotation.registerFix(new IntentionAction() {

            @SuppressWarnings("unchecked")
            @Nls
            @NotNull
            @Override
            public String getText() {
                return Toolbox.getInstance().getTipper(e).description(e);
            }

            @Nls
            @NotNull
            @Override
            public String getFamilyName() {
                return "SpartanizerAction";
            }

            @Override
            public boolean isAvailable(@NotNull Project __, Editor e, PsiFile f) {
                return true;
            }

            @Override
            public void invoke(@NotNull Project p, Editor ed, PsiFile f) throws IncorrectOperationException {
                Spartanizer.spartanizeElement(e);
            }

            @Override
            public boolean startInWriteAction() {
                return false;
            }
        });
        TextAttributesKey.createTextAttributesKey("");
        annotation.setEnforcedTextAttributes((new TextAttributes(null, null, JBColor.BLUE, EffectType.WAVE_UNDERSCORE, 0)));
    } catch (Throwable t) {
        Logger l = new Logger(this.getClass());
        l.error("", t);
    }
}
Also used : Logger(il.org.spartan.Leonidas.plugin.utils.logging.Logger) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) Nls(org.jetbrains.annotations.Nls) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

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