Search in sources :

Example 46 with Annotation

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

the class ActionScriptAnnotatingVisitor method visitJSAttributeList.

@Override
public void visitJSAttributeList(JSAttributeList attributeList) {
    PsiElement namespaceElement = attributeList.getNamespaceElement();
    PsiElement accessTypeElement = attributeList.findAccessTypeElement();
    PsiElement namespaceOrAccessModifierElement = namespaceElement;
    ASTNode[] children = attributeList.getNode().getChildren(JSAttributeListImpl.ourModifiersTypeSet);
    if (namespaceOrAccessModifierElement == null) {
        namespaceOrAccessModifierElement = accessTypeElement;
    } else if (accessTypeElement != null) {
        myHolder.createErrorAnnotation(namespaceOrAccessModifierElement, JSBundle.message("javascript.validation.message.use.namespace.reference.or.access.modifier")).registerFix(new RemoveASTNodeFix("javascript.fix.remove.namespace.reference", namespaceOrAccessModifierElement.getNode()));
        myHolder.createErrorAnnotation(accessTypeElement, JSBundle.message("javascript.validation.message.use.namespace.reference.or.access.modifier")).registerFix(new RemoveASTNodeFix("javascript.fix.remove.visibility.modifier", accessTypeElement.getNode()));
    }
    if (children.length > 1 && namespaceElement == null) {
        for (ASTNode astNode : children) {
            myHolder.createErrorAnnotation(astNode, JSBundle.message("javascript.validation.message.one.visibility.modifier.allowed")).registerFix(new RemoveASTNodeFix("javascript.fix.remove.visibility.modifier", astNode));
        }
    }
    PsiElement element = attributeList.getParent();
    PsiElement parentForCheckingNsOrAccessModifier = JSResolveUtil.findParent(element);
    if (namespaceOrAccessModifierElement != null) {
        if (!(parentForCheckingNsOrAccessModifier instanceof JSClass)) {
            String typeElementText;
            boolean nodeUnderPackage;
            if (!(nodeUnderPackage = parentForCheckingNsOrAccessModifier instanceof JSPackageStatement) && !hasQualifiedName(element) && (!(parentForCheckingNsOrAccessModifier instanceof JSFile) || attributeList.getAccessType() != JSAttributeList.AccessType.PACKAGE_LOCAL) || !"public".equals(typeElementText = namespaceOrAccessModifierElement.getText()) && !"internal".equals(typeElementText)) {
                boolean nsRef = namespaceOrAccessModifierElement instanceof JSReferenceExpression;
                Annotation annotation;
                String message = JSBundle.message(nodeUnderPackage ? "javascript.validation.message.access.modifier.allowed.only.for.package.members" : nsRef ? "javascript.validation.message.namespace.allowed.only.for.class.members" : "javascript.validation.message.access.modifier.allowed.only.for.class.members");
                if (parentForCheckingNsOrAccessModifier instanceof JSFile && !(element instanceof JSClass)) {
                    // TODO: till we resolve issues with includes
                    annotation = myHolder.createWarningAnnotation(namespaceOrAccessModifierElement, message);
                } else {
                    annotation = myHolder.createErrorAnnotation(namespaceOrAccessModifierElement, message);
                }
                annotation.registerFix(new RemoveASTNodeFix(nsRef ? "javascript.fix.remove.namespace.reference" : "javascript.fix.remove.access.modifier", namespaceOrAccessModifierElement.getNode()));
            }
        } else if (((JSClass) parentForCheckingNsOrAccessModifier).isInterface()) {
            if (attributeList.getAccessType() != JSAttributeList.AccessType.PACKAGE_LOCAL || attributeList.getNode().findChildByType(JSTokenTypes.INTERNAL_KEYWORD) != null) {
                ASTNode astNode = attributeList.getNode().findChildByType(JSTokenTypes.ACCESS_MODIFIERS);
                String message = JSBundle.message("javascript.validation.message.interface.members.cannot.have.access.modifiers");
                String fixMessageKey = "javascript.fix.remove.access.modifier";
                if (astNode == null) {
                    astNode = attributeList.getNode().findChildByType(JSElementTypes.REFERENCE_EXPRESSION);
                    message = JSBundle.message("javascript.validation.message.interface.members.cannot.have.namespace.attributes");
                    fixMessageKey = "javascript.fix.remove.namespace.reference";
                }
                final Annotation annotation = myHolder.createErrorAnnotation(astNode, message);
                annotation.registerFix(new RemoveASTNodeFix(fixMessageKey, astNode));
            }
        } else if (JSResolveUtil.isConstructorFunction(element)) {
            JSAttributeList.AccessType accessType = attributeList.getAccessType();
            if (accessType != JSAttributeList.AccessType.PUBLIC) {
                myHolder.createErrorAnnotation(namespaceOrAccessModifierElement.getNode(), JSBundle.message("javascript.validation.message.constructor.cannot.have.custom.visibility"));
            }
        }
    }
    if (attributeList.hasModifier(JSAttributeList.ModifierType.FINAL)) {
        PsiElement parent;
        if (element instanceof JSClass) {
            if (((JSClass) element).isInterface()) {
                finalModifierProblem(attributeList, "javascript.validation.message.interface.cannot.be.final.modifiers");
            }
        } else if (parentForCheckingNsOrAccessModifier instanceof JSClass && ((JSClass) parentForCheckingNsOrAccessModifier).isInterface()) {
            finalModifierProblem(attributeList, "javascript.validation.message.interface.members.cannot.be.final.modifiers");
        } else if (!(element instanceof JSFunction) || (parent = element.getParent()) instanceof JSPackageStatement || parent instanceof JSFile && parent.getContext() == null) {
            finalModifierProblem(attributeList, "javascript.validation.message.final.modifier.allowed.only.for.methods");
        }
    }
    if (attributeList.hasExplicitModifier(JSAttributeList.ModifierType.STATIC)) {
        if (element instanceof JSFunction || element instanceof JSVarStatement) {
            if (!(parentForCheckingNsOrAccessModifier instanceof JSClass)) {
                PsiElement modifierElement = attributeList.findModifierElement(JSAttributeList.ModifierType.STATIC);
                String message = JSBundle.message("javascript.validation.message.static.modifier.is.allowed.only.for.class.members");
                Annotation annotation;
                if (parentForCheckingNsOrAccessModifier instanceof JSFile) {
                    annotation = myHolder.createWarningAnnotation(modifierElement, message);
                } else {
                    annotation = myHolder.createErrorAnnotation(modifierElement, message);
                }
                annotation.registerFix(new RemoveASTNodeFix("javascript.fix.remove.static.modifier", modifierElement.getNode()));
            } else if (JSResolveUtil.isConstructorFunction(element)) {
                modifierProblem(attributeList, JSAttributeList.ModifierType.STATIC, "javascript.validation.message.constructor.cannot.be.static", "javascript.fix.remove.static.modifier");
            }
        } else if (element instanceof JSNamespaceDeclaration || element instanceof JSClass) {
            modifierProblem(attributeList, JSAttributeList.ModifierType.STATIC, "javascript.validation.message.static.modifier.is.allowed.only.for.class.members", "javascript.fix.remove.static.modifier");
        }
        if (attributeList.hasModifier(JSAttributeList.ModifierType.FINAL) && element instanceof JSFunction) {
            finalModifierProblem(attributeList, "javascript.validation.message.static.method.cannot.be.final");
        }
    }
    if (attributeList.hasModifier(JSAttributeList.ModifierType.OVERRIDE) && !(element instanceof JSFunction)) {
        modifierProblem(attributeList, JSAttributeList.ModifierType.OVERRIDE, "javascript.validation.message.override.can.be.applied.to.method", "javascript.fix.remove.override.modifier");
    }
    if (attributeList.hasModifier(JSAttributeList.ModifierType.DYNAMIC) && (!(element instanceof JSClass) || ((JSClass) element).isInterface())) {
        modifierProblem(attributeList, JSAttributeList.ModifierType.DYNAMIC, "javascript.validation.message.dynamic.can.be.applied.to.class", "javascript.fix.remove.dynamic.modifier");
    }
    checkMultipleModifiersProblem(attributeList);
}
Also used : Annotation(com.intellij.lang.annotation.Annotation) ASTNode(com.intellij.lang.ASTNode)

Example 47 with Annotation

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

the class ScssLintAnnotationResult method createAnnotation.

@Nullable
private static Annotation createAnnotation(@NotNull AnnotationHolder holder, @NotNull PsiFile file, @NotNull Document document, @NotNull Lint.Issue issue, @NotNull String messagePrefix, int tabSize, @NotNull HighlightSeverity severity, @Nullable TextAttributes forcedTextAttributes, @NotNull HighlightDisplayKey inspectionKey, boolean showErrorOnWholeLine) {
    int errorLine = issue.line - 1;
    int errorColumn = issue.column - 1;
    if (errorLine < 0 || errorLine >= document.getLineCount()) {
        return null;
    }
    int lineEndOffset = document.getLineEndOffset(errorLine);
    int lineStartOffset = document.getLineStartOffset(errorLine);
    int errorLineStartOffset = PsiUtil.calcErrorStartOffsetInDocument(document, lineStartOffset, lineEndOffset, errorColumn, tabSize);
    if (errorLineStartOffset == -1) {
        return null;
    }
    PsiElement element = file.findElementAt(errorLineStartOffset);
    //        if (element != null /*&& JSInspection.isSuppressedForStatic(element, getInspectionClass(), inspectionKey.getID())*/)
    //            return null;
    TextRange range;
    if (showErrorOnWholeLine) {
        range = new TextRange(lineStartOffset, lineEndOffset);
    } else {
        //            int offset = StringUtil.lineColToOffset(document.getText(), warn.line - 1, warn.column);
        PsiElement lit = PsiUtil.getElementAtOffset(file, errorLineStartOffset);
        range = lit.getTextRange();
    //            range = new TextRange(errorLineStartOffset, errorLineStartOffset + 1);
    }
    range = new TextRange(errorLineStartOffset, errorLineStartOffset + issue.length);
    Annotation annotation = createAnnotation(holder, severity, forcedTextAttributes, range, messagePrefix + issue.reason.trim() + " (" + (issue.linter == null ? "none" : issue.linter) + ')');
    if (annotation != null) {
        annotation.setAfterEndOfLine(errorLineStartOffset == lineEndOffset);
    }
    return annotation;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Lint(com.scss.utils.scssLint.Lint) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with Annotation

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

the class ScssLintAnnotationResult method apply.

@Override
public void apply(@NotNull PsiFile file, ScssLintAnnotationResult annotationResult, @NotNull AnnotationHolder holder) {
    if (annotationResult == null) {
        return;
    }
    InspectionProjectProfileManager inspectionProjectProfileManager = InspectionProjectProfileManager.getInstance(file.getProject());
    SeverityRegistrar severityRegistrar = inspectionProjectProfileManager.getSeverityRegistrar();
    HighlightDisplayKey inspectionKey = getHighlightDisplayKeyByClass();
    EditorColorsScheme colorsScheme = annotationResult.input.colorsScheme;
    Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document == null) {
        return;
    }
    if (annotationResult.fileLevel != null) {
        Annotation annotation = holder.createWarningAnnotation(file, annotationResult.fileLevel);
        annotation.registerFix(new EditSettingsAction(new ScssLintSettingsPage(file.getProject())));
        annotation.setFileLevelAnnotation(true);
        return;
    }
    // TODO consider adding a fix to edit configuration file
    if (annotationResult.result == null || annotationResult.result.lint == null || annotationResult.result.lint.isEmpty()) {
        return;
    }
    // String relativeFile = FileUtils.makeRelative(file.getProject(), file.getVirtualFile());
    List<Lint.Issue> issues = annotationResult.result.lint.values().iterator().next();
    if (issues == null) {
        return;
    }
    ScssLintProjectComponent component = annotationResult.input.project.getComponent(ScssLintProjectComponent.class);
    int tabSize = 4;
    for (Lint.Issue issue : issues) {
        HighlightSeverity severity = getHighlightSeverity(issue, component.treatAsWarnings);
        TextAttributes forcedTextAttributes = AnnotatorUtils.getTextAttributes(colorsScheme, severityRegistrar, severity);
        Annotation annotation = createAnnotation(holder, file, document, issue, "SCSS Lint: ", tabSize, severity, forcedTextAttributes, inspectionKey, false);
        if (annotation != null) {
            int offset = StringUtil.lineColToOffset(document.getText(), issue.line - 1, issue.column);
            PsiElement lit = PsiUtil.getElementAtOffset(file, offset);
            BaseActionFix actionFix = Fixes.getFixForRule(issue.linter, lit);
            if (actionFix != null) {
                annotation.registerFix(actionFix, null, inspectionKey);
            }
        //                annotation.registerFix(new SuppressActionFix(issue.rule, lit), null, inspectionKey);
        }
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) Lint(com.scss.utils.scssLint.Lint) ScssLintSettingsPage(com.scss.settings.ScssLintSettingsPage) Document(com.intellij.openapi.editor.Document) Annotation(com.intellij.lang.annotation.Annotation) Lint(com.scss.utils.scssLint.Lint) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) InspectionProjectProfileManager(com.intellij.profile.codeInspection.InspectionProjectProfileManager) SeverityRegistrar(com.intellij.codeInsight.daemon.impl.SeverityRegistrar) PsiElement(com.intellij.psi.PsiElement) BaseActionFix(com.scss.annotator.BaseActionFix)

Example 49 with Annotation

use of com.intellij.lang.annotation.Annotation in project buck by facebook.

the class BuckAnnotator method annotateIdentifier.

private boolean annotateIdentifier(PsiElement psiElement, AnnotationHolder annotationHolder) {
    if (psiElement.getNode().getElementType() != BuckTypes.IDENTIFIER) {
        return false;
    }
    PsiElement parent = psiElement.getParent();
    assert parent != null;
    final Annotation annotation = annotationHolder.createInfoAnnotation(psiElement, null);
    if (BuckPsiUtils.testType(parent, BuckTypes.RULE_NAME)) {
        annotation.setTextAttributes(BuckSyntaxHighlighter.BUCK_RULE_NAME);
        return true;
    } else if (BuckPsiUtils.testType(parent, BuckTypes.PROPERTY_LVALUE)) {
        annotation.setTextAttributes(BuckSyntaxHighlighter.BUCK_KEYWORD);
        return true;
    }
    return false;
}
Also used : PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 50 with Annotation

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

the class RegExpAnnotator method visitRegExpQuantifier.

@Override
public void visitRegExpQuantifier(RegExpQuantifier quantifier) {
    if (quantifier.isCounted()) {
        final RegExpNumber minElement = quantifier.getMin();
        final String min = minElement == null ? "" : minElement.getText();
        final RegExpNumber maxElement = quantifier.getMax();
        final String max = maxElement == null ? "" : maxElement.getText();
        if (!max.isEmpty() && max.equals(min)) {
            if ("1".equals(max)) {
                final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Single repetition");
                registerFix(a, new SimplifyQuantifierAction(quantifier, null));
            } else {
                final ASTNode node = quantifier.getNode();
                if (node.findChildByType(RegExpTT.COMMA) != null) {
                    final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Fixed repetition range");
                    registerFix(a, new SimplifyQuantifierAction(quantifier, "{" + max + "}"));
                }
            }
        } else if (("0".equals(min) || min.isEmpty()) && "1".equals(max)) {
            final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '?'");
            registerFix(a, new SimplifyQuantifierAction(quantifier, "?"));
        } else if (("0".equals(min) || min.isEmpty()) && max.isEmpty()) {
            final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '*'");
            registerFix(a, new SimplifyQuantifierAction(quantifier, "*"));
        } else if ("1".equals(min) && max.isEmpty()) {
            final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '+'");
            registerFix(a, new SimplifyQuantifierAction(quantifier, "+"));
        }
        Number minValue = null;
        if (minElement != null) {
            minValue = myLanguageHosts.getQuantifierValue(minElement);
            if (minValue == null)
                myHolder.createErrorAnnotation(minElement, "Repetition value too large");
        }
        Number maxValue = null;
        if (maxElement != null) {
            maxValue = myLanguageHosts.getQuantifierValue(maxElement);
            if (maxValue == null)
                myHolder.createErrorAnnotation(maxElement, "Repetition value too large");
        }
        if (minValue != null && maxValue != null) {
            if (minValue.longValue() > maxValue.longValue() || minValue.doubleValue() > maxValue.doubleValue()) {
                final TextRange range = new TextRange(minElement.getTextOffset(), maxElement.getTextOffset() + maxElement.getTextLength());
                myHolder.createErrorAnnotation(range, "Illegal repetition range (min > max)");
            }
        }
    }
    if (quantifier.isPossessive() && !myLanguageHosts.supportsPossessiveQuantifiers(quantifier)) {
        final ASTNode modifier = quantifier.getModifier();
        assert modifier != null;
        myHolder.createErrorAnnotation(modifier, "Nested quantifier in regexp");
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) 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