Search in sources :

Example 41 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class ExtractUtil method mustAddVariableDeclaration.

/*
  To declare or not a variable to which method call result will be assigned.
   */
private static List<VariableInfo> mustAddVariableDeclaration(@NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
    Map<String, VariableInfo> names = new HashMap<>();
    for (VariableInfo var : vars) {
        names.put(var.getName(), var);
    }
    List<VariableInfo> result = new ArrayList<>();
    for (GrStatement statement : statements) {
        if (statement instanceof GrVariableDeclaration) {
            GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
            for (GrVariable variable : declaration.getVariables()) {
                final VariableInfo removed = names.remove(variable.getName());
                if (removed != null) {
                    result.add(removed);
                }
            }
        }
    }
    for (String varName : names.keySet()) {
        if (ResolveUtil.resolveProperty(statements[statements.length - 1], varName) == null) {
            result.add(names.get(varName));
        }
    }
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) HashMap(com.intellij.util.containers.HashMap) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 42 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class ExpressionGenerator method initializeField.

private void initializeField(String varName, PsiType type, PsiClass resolved, PsiSubstitutor substitutor, String fieldName, GrExpression expression) {
    StringBuilder builder = new StringBuilder();
    final PsiMethod setter = GroovyPropertyUtils.findPropertySetter(resolved, fieldName, false, true);
    if (setter != null) {
        final GrVariableDeclaration var = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, varName);
        final GrReferenceExpression caller = factory.createReferenceExpressionFromText(varName, var);
        invokeMethodOn(setter, caller, new GrExpression[] { expression }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, substitutor, expression);
    } else {
        builder.append(varName).append('.').append(fieldName).append(" = ");
        expression.accept(new ExpressionGenerator(builder, context));
    }
    context.myStatements.add(builder.toString());
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)

Example 43 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class ClassGenerator method writeMembers.

public void writeMembers(StringBuilder text, PsiClass typeDefinition) {
    if (typeDefinition instanceof GrEnumTypeDefinition) {
        final GrEnumConstant[] enumConstants = ((GrEnumTypeDefinition) typeDefinition).getEnumConstants();
        for (GrEnumConstant constant : enumConstants) {
            classItemGenerator.writeEnumConstant(text, constant);
            text.append(',');
        }
        if (enumConstants.length > 0) {
            //text.removeFromTheEnd(1).append(";\n");
            text.delete(text.length() - 1, text.length());
        }
        text.append(";\n");
    }
    writeAllMethods(text, classItemGenerator.collectMethods(typeDefinition), typeDefinition);
    if (typeDefinition instanceof GrTypeDefinition) {
        for (GrMembersDeclaration declaration : ((GrTypeDefinition) typeDefinition).getMemberDeclarations()) {
            if (declaration instanceof GrVariableDeclaration) {
                classItemGenerator.writeVariableDeclarations(text, (GrVariableDeclaration) declaration);
            }
        }
        for (PsiClass inner : typeDefinition.getInnerClasses()) {
            writeTypeDefinition(text, inner, false, false);
            text.append('\n');
        }
    }
    classItemGenerator.writePostponed(text, typeDefinition);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMembersDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) PsiClass(com.intellij.psi.PsiClass)

Aggregations

GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)43 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)19 PsiElement (com.intellij.psi.PsiElement)15 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)9 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)7 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5 TextRange (com.intellij.openapi.util.TextRange)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 Template (com.intellij.codeInsight.template.Template)3