Search in sources :

Example 6 with GrModifierList

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

the class StubGenerator method writeVariableDeclarations.

@Override
public void writeVariableDeclarations(StringBuilder text, GrVariableDeclaration variableDeclaration) {
    GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
    final GrModifierList modifierList = variableDeclaration.getModifierList();
    final PsiNameHelper nameHelper = PsiNameHelper.getInstance(variableDeclaration.getProject());
    for (final GrVariable variable : variableDeclaration.getVariables()) {
        String name = variable.getName();
        if (!nameHelper.isIdentifier(name)) {
            //does not have a java image
            continue;
        }
        ModifierListGenerator.writeModifiers(text, modifierList, STUB_FIELD_MODIFIERS, false);
        //type
        PsiType declaredType = typeElement == null ? PsiType.getJavaLangObject(variable.getManager(), variable.getResolveScope()) : typeElement.getType();
        TypeWriter.writeType(text, declaredType, variableDeclaration, classNameProvider);
        text.append(' ').append(name).append(" = ").append(getVariableInitializer(variable, declaredType));
        text.append(";\n");
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)

Example 7 with GrModifierList

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

the class CodeBlockGenerator method writeTupleDeclaration.

private void writeTupleDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
    GrVariable[] variables = variableDeclaration.getVariables();
    final GrExpression tupleInitializer = variableDeclaration.getTupleInitializer();
    if (tupleInitializer instanceof GrListOrMap) {
        for (GrVariable variable : variables) {
            GenerationUtil.writeVariableSeparately(variable, builder, expressionContext);
            builder.append(";\n");
        }
    } else if (tupleInitializer != null) {
        GroovyResolveResult iteratorMethodResult = GenerationUtil.resolveMethod(tupleInitializer, "iterator", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, variableDeclaration);
        final PsiType iteratorType = inferIteratorType(iteratorMethodResult, tupleInitializer);
        final String iteratorName = genIteratorVar(variableDeclaration, builder, expressionContext, tupleInitializer, iteratorType, iteratorMethodResult);
        final GrModifierList modifierList = variableDeclaration.getModifierList();
        PsiType iterableTypeParameter = PsiUtil.extractIterableTypeParameter(iteratorType, false);
        for (final GrVariable v : variables) {
            ModifierListGenerator.writeModifiers(builder, modifierList);
            final PsiType type = context.typeProvider.getVarType(v);
            TypeWriter.writeType(builder, type, variableDeclaration);
            builder.append(' ').append(v.getName());
            builder.append(" = ");
            GenerationUtil.wrapInCastIfNeeded(builder, type, iterableTypeParameter, tupleInitializer, expressionContext, new StatementWriter() {

                @Override
                public void writeStatement(StringBuilder builder, ExpressionContext context) {
                    builder.append(iteratorName).append(".hasNext() ? ").append(iteratorName).append(".next() : null");
                }
            });
            builder.append(";\n");
        }
    } else {
        GenerationUtil.writeSimpleVarDeclaration(variableDeclaration, builder, expressionContext);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 8 with GrModifierList

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

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 9 with GrModifierList

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

the class GroovySuppressableInspectionTool method getElementToolSuppressedIn.

@Nullable
public static PsiElement getElementToolSuppressedIn(final PsiElement place, @NotNull String toolId) {
    if (place == null)
        return null;
    AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        final PsiElement statement = PsiUtil.findEnclosingStatement(place);
        if (statement != null) {
            PsiElement prev = statement.getPrevSibling();
            while (prev != null && StringUtil.isEmpty(prev.getText().trim())) {
                prev = prev.getPrevSibling();
            }
            if (prev instanceof PsiComment) {
                String text = prev.getText();
                Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
                if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
                    return prev;
                }
            }
        }
        GrMember member = null;
        GrDocComment docComment = PsiTreeUtil.getParentOfType(place, GrDocComment.class);
        if (docComment != null) {
            GrDocCommentOwner owner = docComment.getOwner();
            if (owner instanceof GrMember) {
                member = (GrMember) owner;
            }
        }
        if (member == null) {
            member = PsiTreeUtil.getNonStrictParentOfType(place, GrMember.class);
        }
        while (member != null) {
            GrModifierList modifierList = member.getModifierList();
            for (String ids : getInspectionIdsSuppressedInAnnotation(modifierList)) {
                if (SuppressionUtil.isInspectionToolIdMentioned(ids, toolId)) {
                    return modifierList;
                }
            }
            member = PsiTreeUtil.getParentOfType(member, GrMember.class);
        }
        return null;
    } finally {
        accessToken.finish();
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) PsiComment(com.intellij.psi.PsiComment) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) Matcher(java.util.regex.Matcher) AccessToken(com.intellij.openapi.application.AccessToken) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrModifierList

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

the class GroovyAnnotator method visitPackageDefinition.

@Override
public void visitPackageDefinition(@NotNull GrPackageDefinition packageDefinition) {
    final GrModifierList modifierList = packageDefinition.getAnnotationList();
    checkAnnotationList(myHolder, modifierList, GroovyBundle.message("package.definition.cannot.have.modifiers"));
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)

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