Search in sources :

Example 31 with Annotation

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

the class GroovyAnnotator method checkDuplicateModifiers.

private static void checkDuplicateModifiers(AnnotationHolder holder, @NotNull GrModifierList list, PsiMember member) {
    final PsiElement[] modifiers = list.getModifiers();
    Set<String> set = new THashSet<>(modifiers.length);
    for (PsiElement modifier : modifiers) {
        if (modifier instanceof GrAnnotation)
            continue;
        @GrModifier.GrModifierConstant String name = modifier.getText();
        if (set.contains(name)) {
            final Annotation annotation = holder.createErrorAnnotation(list, GroovyBundle.message("duplicate.modifier", name));
            registerFix(annotation, new GrModifierFix(member, name, false, false, GrModifierFix.MODIFIER_LIST), list);
        } else {
            set.add(name);
        }
    }
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) THashSet(gnu.trove.THashSet) Annotation(com.intellij.lang.annotation.Annotation)

Example 32 with Annotation

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

the class GroovyAnnotator method visitMethod.

@Override
public void visitMethod(@NotNull GrMethod method) {
    checkDuplicateMethod(method);
    checkMethodWithTypeParamsShouldHaveReturnType(myHolder, method);
    checkInnerMethod(myHolder, method);
    checkOptionalParametersInAbstractMethod(myHolder, method);
    checkConstructorOfImmutableClass(myHolder, method);
    checkGetterOfImmutable(myHolder, method);
    final PsiElement nameIdentifier = method.getNameIdentifierGroovy();
    if (nameIdentifier.getNode().getElementType() == GroovyTokenTypes.mSTRING_LITERAL) {
        checkStringLiteral(nameIdentifier);
    }
    GrOpenBlock block = method.getBlock();
    if (block != null && TypeInferenceHelper.isTooComplexTooAnalyze(block)) {
        myHolder.createWeakWarningAnnotation(nameIdentifier, GroovyBundle.message("method.0.is.too.complex.too.analyze", method.getName()));
    }
    final PsiClass containingClass = method.getContainingClass();
    if (method.isConstructor()) {
        if (containingClass instanceof GrAnonymousClassDefinition) {
            myHolder.createErrorAnnotation(nameIdentifier, GroovyBundle.message("constructors.are.not.allowed.in.anonymous.class"));
        } else if (containingClass != null && containingClass.isInterface()) {
            myHolder.createErrorAnnotation(nameIdentifier, GroovyBundle.message("constructors.are.not.allowed.in.interface"));
        }
    }
    if (method.getBlock() == null && !method.hasModifierProperty(PsiModifier.NATIVE) && !GrTraitUtil.isMethodAbstract(method)) {
        final Annotation annotation = myHolder.createErrorAnnotation(nameIdentifier, GroovyBundle.message("not.abstract.method.should.have.body"));
    //annotation.registerFix(new AddMethodBodyFix(method)); //todo make intentions work
    //registerFix(annotation, new GrModifierFix(method, ABSTRACT, false, true, GrModifierFix.MODIFIER_LIST_OWNER), method);
    }
    checkOverridingMethod(myHolder, method);
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Annotation(com.intellij.lang.annotation.Annotation)

Example 33 with Annotation

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

the class GroovyAnnotator method visitVariable.

@Override
public void visitVariable(@NotNull GrVariable variable) {
    checkName(variable);
    PsiElement parent = variable.getParent();
    if (parent instanceof GrForInClause) {
        PsiElement delimiter = ((GrForInClause) parent).getDelimiter();
        if (delimiter.getNode().getElementType() == GroovyTokenTypes.mCOLON) {
            GrTypeElement typeElement = variable.getTypeElementGroovy();
            GrModifierList modifierList = variable.getModifierList();
            if (typeElement == null && StringUtil.isEmptyOrSpaces(modifierList.getText())) {
                Annotation annotation = myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message("java.style.for.each.statement.requires.a.type.declaration"));
                annotation.registerFix(new ReplaceDelimiterFix());
            }
        }
    }
    PsiNamedElement duplicate = ResolveUtil.findDuplicate(variable);
    if (duplicate instanceof GrVariable && (variable instanceof GrField || ResolveUtil.isScriptField(variable) || !(duplicate instanceof GrField))) {
        final String key = duplicate instanceof GrField ? "field.already.defined" : "variable.already.defined";
        myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message(key, variable.getName()));
    }
    PsiType type = variable.getDeclaredType();
    if (type instanceof PsiEllipsisType && !isLastParameter(variable)) {
        TextRange range = getEllipsisRange(variable);
        if (range == null) {
            range = getTypeRange(variable);
        }
        if (range != null) {
            myHolder.createErrorAnnotation(range, GroovyBundle.message("ellipsis.type.is.not.allowed.here"));
        }
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)

Example 34 with Annotation

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

the class GroovyAnnotator method checkFieldModifiers.

private static void checkFieldModifiers(AnnotationHolder holder, GrVariableDeclaration fieldDeclaration) {
    GrVariable[] variables = fieldDeclaration.getVariables();
    if (variables.length == 0)
        return;
    GrVariable variable = variables[0];
    if (!(variable instanceof GrField))
        return;
    final GrField member = (GrField) variable;
    final GrModifierList modifierList = fieldDeclaration.getModifierList();
    checkAccessModifiers(holder, modifierList, member);
    checkDuplicateModifiers(holder, modifierList, member);
    if (modifierList.hasExplicitModifier(PsiModifier.VOLATILE) && modifierList.hasExplicitModifier(PsiModifier.FINAL)) {
        final Annotation annotation = holder.createErrorAnnotation(modifierList, GroovyBundle.message("illegal.combination.of.modifiers.volatile.and.final"));
        registerFix(annotation, new GrModifierFix(member, PsiModifier.VOLATILE, true, false, GrModifierFix.MODIFIER_LIST), modifierList);
        registerFix(annotation, new GrModifierFix(member, PsiModifier.FINAL, true, false, GrModifierFix.MODIFIER_LIST), modifierList);
    }
    if (member.getContainingClass() instanceof GrInterfaceDefinition) {
        checkModifierIsNotAllowed(modifierList, PsiModifier.PRIVATE, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PRIVATE), holder);
        checkModifierIsNotAllowed(modifierList, PsiModifier.PROTECTED, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PROTECTED), holder);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) Annotation(com.intellij.lang.annotation.Annotation)

Example 35 with Annotation

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

the class AndroidColorAnnotator method annotateXml.

private static void annotateXml(PsiElement element, AnnotationHolder holder, String value) {
    if (value.startsWith("#")) {
        final PsiFile file = element.getContainingFile();
        if (file != null && AndroidResourceUtil.isInResourceSubdirectory(file, null)) {
            if (element instanceof XmlTag) {
                Annotation annotation = holder.createInfoAnnotation(element, null);
                annotation.setGutterIconRenderer(new MyRenderer(element, null));
            } else {
                assert element instanceof XmlAttributeValue;
                Color color = ResourceHelper.parseColor(value);
                if (color != null) {
                    Annotation annotation = holder.createInfoAnnotation(element, null);
                    annotation.setGutterIconRenderer(new MyRenderer(element, null));
                }
            }
        }
    } else if (value.startsWith(COLOR_RESOURCE_PREFIX)) {
        annotateResourceReference(ResourceType.COLOR, holder, element, value.substring(COLOR_RESOURCE_PREFIX.length()), false);
    } else if (value.startsWith(ANDROID_COLOR_RESOURCE_PREFIX)) {
        annotateResourceReference(ResourceType.COLOR, holder, element, value.substring(ANDROID_COLOR_RESOURCE_PREFIX.length()), true);
    } else if (value.startsWith(DRAWABLE_PREFIX)) {
        annotateResourceReference(ResourceType.DRAWABLE, holder, element, value.substring(DRAWABLE_PREFIX.length()), false);
    } else if (value.startsWith(ANDROID_DRAWABLE_PREFIX)) {
        annotateResourceReference(ResourceType.DRAWABLE, holder, element, value.substring(ANDROID_DRAWABLE_PREFIX.length()), true);
    } else if (value.startsWith(MIPMAP_PREFIX)) {
        annotateResourceReference(ResourceType.MIPMAP, holder, element, value.substring(MIPMAP_PREFIX.length()), false);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Annotation(com.intellij.lang.annotation.Annotation) XmlTag(com.intellij.psi.xml.XmlTag)

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