Search in sources :

Example 11 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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 12 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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 13 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GrUnnecessaryPublicModifierInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {

        @Override
        public void visitElement(PsiElement modifier) {
            if (modifier.getNode().getElementType() != GroovyTokenTypes.kPUBLIC)
                return;
            PsiElement list = modifier.getParent();
            if (!(list instanceof GrModifierList))
                return;
            PsiElement parent = list.getParent();
            // It may be put there explicitly to prevent getter/setter generation.
            if (parent instanceof GrVariableDeclaration)
                return;
            holder.registerProblem(modifier, GroovyInspectionBundle.message("unnecessary.modifier.description", PsiModifier.PUBLIC), ProblemHighlightType.LIKE_UNUSED_SYMBOL, FIX);
        }
    };
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class SpockUnrollReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    GrAnnotationNameValuePair nvp = (GrAnnotationNameValuePair) element.getParent();
    String name = nvp.getName();
    if (name != null && !name.equals("value"))
        return PsiReference.EMPTY_ARRAY;
    PsiElement argumentList = nvp.getParent();
    if (!(argumentList instanceof GrAnnotationArgumentList))
        return PsiReference.EMPTY_ARRAY;
    PsiElement eAnnotation = argumentList.getParent();
    if (!(eAnnotation instanceof GrAnnotation))
        return PsiReference.EMPTY_ARRAY;
    GrAnnotation annotation = (GrAnnotation) eAnnotation;
    String shortName = annotation.getShortName();
    if (!shortName.equals("Unroll") && !shortName.equals("spock.lang.Unroll"))
        return PsiReference.EMPTY_ARRAY;
    PsiElement modifierList = annotation.getParent();
    if (!(modifierList instanceof GrModifierList))
        return PsiReference.EMPTY_ARRAY;
    PsiElement eMethod = modifierList.getParent();
    if (!(eMethod instanceof GrMethod))
        return PsiReference.EMPTY_ARRAY;
    final GrMethod method = (GrMethod) eMethod;
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    TextRange rangeInElement = manipulator.getRangeInElement(element);
    String text = rangeInElement.substring(element.getText());
    final List<SpockVariableReference> references = new ArrayList<>();
    Matcher matcher = PATTERN.matcher(text);
    while (matcher.find()) {
        TextRange range = new TextRange(rangeInElement.getStartOffset() + matcher.start(1), rangeInElement.getStartOffset() + matcher.end(1));
        references.add(new SpockVariableReference(element, range, references, method));
    }
    return references.toArray(new PsiReference[references.size()]);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) Matcher(java.util.regex.Matcher) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GrVariableDeclarationImpl method findSuitableModifier.

private PsiElement findSuitableModifier() {
    final GrModifierList list = getModifierList();
    PsiElement defModifier = list.getModifier(GrModifier.DEF);
    if (defModifier != null)
        return defModifier;
    PsiElement finalModifier = list.getModifier(PsiModifier.FINAL);
    if (finalModifier != null)
        return finalModifier;
    for (PsiElement element : list.getModifiers()) {
        if (!(element instanceof GrAnnotation)) {
            return element;
        }
    }
    return null;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Aggregations

GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)39 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)9 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)5 Annotation (com.intellij.lang.annotation.Annotation)4 PsiElement (com.intellij.psi.PsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 NotNull (org.jetbrains.annotations.NotNull)3 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 Matcher (java.util.regex.Matcher)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)2 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)1