Search in sources :

Example 31 with GrModifierList

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

the class GroovyAnnotator method visitForInClause.

@Override
public void visitForInClause(@NotNull GrForInClause forInClause) {
    final GrVariable var = forInClause.getDeclaredVariable();
    if (var == null)
        return;
    final GrModifierList modifierList = var.getModifierList();
    if (modifierList == null)
        return;
    final PsiElement[] modifiers = modifierList.getModifiers();
    for (PsiElement modifier : modifiers) {
        if (modifier instanceof PsiAnnotation)
            continue;
        final String modifierText = modifier.getText();
        if (PsiModifier.FINAL.equals(modifierText))
            continue;
        if (GrModifier.DEF.equals(modifierText))
            continue;
        myHolder.createErrorAnnotation(modifier, GroovyBundle.message("not.allowed.modifier.in.forin", modifierText));
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)

Example 32 with GrModifierList

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

the class GroovyAnnotator method visitVariableDeclaration.

@Override
public void visitVariableDeclaration(@NotNull GrVariableDeclaration variableDeclaration) {
    if (variableDeclaration.isTuple()) {
        final GrModifierList list = variableDeclaration.getModifierList();
        final PsiElement last = PsiUtil.skipWhitespacesAndComments(list.getLastChild(), false);
        if (last != null) {
            final IElementType type = last.getNode().getElementType();
            if (type != GroovyTokenTypes.kDEF) {
                myHolder.createErrorAnnotation(list, GroovyBundle.message("tuple.declaration.should.end.with.def.modifier"));
            }
        } else {
            myHolder.createErrorAnnotation(list, GroovyBundle.message("tuple.declaration.should.end.with.def.modifier"));
        }
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) IElementType(com.intellij.psi.tree.IElementType)

Example 33 with GrModifierList

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

the class GrMethodBaseImpl method insertPlaceHolderToModifierList.

private void insertPlaceHolderToModifierList() {
    final GrModifierList list = getModifierList();
    PsiImplUtil.insertPlaceHolderToModifierListAtEndIfNeeded(list);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)

Example 34 with GrModifierList

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

the class ConvertClosureToMethodIntention method execute.

private static void execute(final GrField field, final Collection<PsiElement> fieldUsages) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(field.getProject());
    final StringBuilder builder = new StringBuilder(field.getTextLength());
    final GrClosableBlock block = (GrClosableBlock) field.getInitializerGroovy();
    final GrModifierList modifierList = field.getModifierList();
    if (modifierList.getModifiers().length > 0 || modifierList.getAnnotations().length > 0) {
        builder.append(modifierList.getText());
    } else {
        builder.append(GrModifier.DEF);
    }
    builder.append(' ').append(field.getName());
    builder.append('(');
    if (block.hasParametersSection()) {
        builder.append(block.getParameterList().getText());
    } else {
        builder.append("def it = null");
    }
    builder.append(") {");
    ApplicationManager.getApplication().runWriteAction(() -> {
        block.getParameterList().delete();
        block.getLBrace().delete();
        final PsiElement psiElement = PsiUtil.skipWhitespacesAndComments(block.getFirstChild(), true);
        if (psiElement != null && "->".equals(psiElement.getText())) {
            psiElement.delete();
        }
        builder.append(block.getText());
        final GrMethod method = GroovyPsiElementFactory.getInstance(field.getProject()).createMethodFromText(builder.toString());
        field.getParent().replace(method);
        for (PsiElement usage : fieldUsages) {
            if (usage instanceof GrReferenceExpression) {
                final PsiElement parent = usage.getParent();
                StringBuilder newRefText = new StringBuilder();
                if (parent instanceof GrReferenceExpression && usage == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
                    newRefText.append(usage.getText());
                    usage = parent;
                } else {
                    PsiElement qualifier = ((GrReferenceExpression) usage).getQualifier();
                    if (qualifier == null) {
                        if (parent instanceof GrReferenceExpression && ((GrReferenceExpression) parent).getQualifier() != null && usage != ((GrReferenceExpression) parent).getQualifier()) {
                            qualifier = ((GrReferenceExpression) parent).getQualifier();
                            usage = parent;
                        }
                    }
                    if (qualifier != null) {
                        newRefText.append(qualifier.getText()).append('.');
                        ((GrReferenceExpression) usage).setQualifier(null);
                    } else {
                        newRefText.append("this.");
                    }
                    newRefText.append('&').append(usage.getText());
                }
                usage.replace(factory.createReferenceExpressionFromText(newRefText.toString()));
            }
        }
    });
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 35 with GrModifierList

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

the class GrAnnotationCollector method collectAliasedAnnotationsFromAnnotationCollectorAnnotations.

private static void collectAliasedAnnotationsFromAnnotationCollectorAnnotations(@NotNull PsiModifierList modifierList, @NotNull Map<String, Map<String, PsiNameValuePair>> annotations) {
    PsiElement parent = modifierList.getParent();
    if (parent instanceof PsiClass && GroovyCommonClassNames.GROOVY_TRANSFORM_COMPILE_DYNAMIC.equals(((PsiClass) parent).getQualifiedName())) {
        Map<String, PsiNameValuePair> params = ContainerUtil.newLinkedHashMap();
        annotations.put(GroovyCommonClassNames.GROOVY_TRANSFORM_COMPILE_STATIC, params);
        GrAnnotation annotation = GroovyPsiElementFactory.getInstance(modifierList.getProject()).createAnnotationFromText("@CompileStatic(TypeCheckingMode.SKIP)");
        params.put("value", annotation.getParameterList().getAttributes()[0]);
        return;
    }
    PsiAnnotation[] rawAnnotations = modifierList instanceof GrModifierList ? ((GrModifierList) modifierList).getRawAnnotations() : modifierList.getAnnotations();
    for (PsiAnnotation annotation : rawAnnotations) {
        final String qname = annotation.getQualifiedName();
        if (qname == null || qname.equals(GroovyCommonClassNames.GROOVY_TRANSFORM_ANNOTATION_COLLECTOR))
            continue;
        final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
        for (PsiNameValuePair pair : attributes) {
            Map<String, PsiNameValuePair> map = annotations.get(qname);
            if (map == null) {
                map = ContainerUtil.newLinkedHashMap();
                annotations.put(qname, map);
            }
            map.put(pair.getName() != null ? pair.getName() : "value", pair);
        }
        if (attributes.length == 0 && !annotations.containsKey(qname)) {
            annotations.put(qname, ContainerUtil.<String, PsiNameValuePair>newLinkedHashMap());
        }
    }
}
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