Search in sources :

Example 61 with GrVariable

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

the class ExtractUtil method generateBody.

public static void generateBody(ExtractInfoHelper helper, boolean isVoid, StringBuilder buffer, boolean forceReturn) {
    VariableInfo[] outputInfos = helper.getOutputVariableInfos();
    ParameterInfo[] infos = helper.getParameterInfos();
    Set<String> declaredVars = new HashSet<>();
    for (ParameterInfo info : infos) {
        declaredVars.add(info.getName());
    }
    for (VariableInfo info : mustAddVariableDeclaration(helper.getStatements(), outputInfos)) {
        declaredVars.add(info.getName());
    }
    List<VariableInfo> genDecl = new ArrayList<>();
    final Collection<GrVariable> outside = collectUsedLocalVarsOrParamsDeclaredOutside(helper);
    for (final GrVariable variable : outside) {
        if (!declaredVars.contains(variable.getName())) {
            genDecl.add(new VariableInfo() {

                @NotNull
                @Override
                public String getName() {
                    return variable.getName();
                }

                @Override
                public PsiType getType() {
                    return variable.getDeclaredType();
                }
            });
        }
    }
    final List<GrStatement> statements = generateVarDeclarations(genDecl, helper.getProject(), null);
    for (GrStatement statement : statements) {
        buffer.append(statement.getText()).append('\n');
    }
    final StringPartInfo stringPartInfo = helper.getStringPartInfo();
    if (!isSingleExpression(helper.getStatements()) && stringPartInfo == null) {
        for (PsiElement element : helper.getInnerElements()) {
            buffer.append(element.getText());
        }
        //append return statement
        if (!isVoid && outputInfos.length > 0) {
            buffer.append('\n');
            if (forceReturn) {
                buffer.append("return ");
            }
            if (outputInfos.length > 1)
                buffer.append('[');
            for (VariableInfo info : outputInfos) {
                buffer.append(info.getName()).append(", ");
            }
            buffer.delete(buffer.length() - 2, buffer.length());
            if (outputInfos.length > 1)
                buffer.append(']');
        }
    } else {
        GrExpression expr = stringPartInfo != null ? stringPartInfo.createLiteralFromSelected() : (GrExpression) PsiUtil.skipParentheses(helper.getStatements()[0], false);
        boolean addReturn = !isVoid && forceReturn && !PsiUtil.isVoidMethodCall(expr);
        if (addReturn) {
            buffer.append("return ");
            final GrExpression methodCall = ApplicationStatementUtil.convertToMethodCallExpression(expr);
            buffer.append(methodCall.getText());
        } else {
            buffer.append(expr != null ? expr.getText() : "");
        }
    }
}
Also used : VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 62 with GrVariable

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

the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.

private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
    final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
    final GroovyResolveResult[] results = refExpr.multiResolve(false);
    for (GroovyResolveResult result : results) {
        final PsiElement resolved = result.getElement();
        if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
            final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
            if (parent != null) {
                PsiElement current = element;
                while (current != null && current != parent) {
                    if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
                        return;
                    }
                    current = current.getParent();
                }
            }
            if (!place.getManager().areElementsEquivalent(element, resolved)) {
                visitor.visitCollidingVariable((PsiVariable) resolved);
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 63 with GrVariable

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

the class ExpressionGenerator method createVarByInitializer.

private String createVarByInitializer(@NotNull GrExpression initializer) {
    GrExpression inner = initializer;
    while (inner instanceof GrParenthesizedExpression) inner = ((GrParenthesizedExpression) inner).getOperand();
    if (inner != null)
        initializer = inner;
    if (initializer instanceof GrReferenceExpression) {
        final GrExpression qualifier = ((GrReferenceExpression) initializer).getQualifier();
        if (qualifier == null) {
            final PsiElement resolved = ((GrReferenceExpression) initializer).resolve();
            if (resolved instanceof GrVariable && !(resolved instanceof GrField)) {
                //don't create new var. it is already exists
                return ((GrVariable) resolved).getName();
            }
        }
    }
    final String name = GenerationUtil.suggestVarName(initializer, context);
    final StringBuilder builder = new StringBuilder();
    builder.append("final ");
    TypeWriter.writeType(builder, initializer.getType(), initializer);
    builder.append(' ').append(name).append(" = ");
    initializer.accept(new ExpressionGenerator(builder, context));
    builder.append(';');
    context.myStatements.add(builder.toString());
    return name;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 64 with GrVariable

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

the class GenerationUtil method writeSimpleVarDeclaration.

public static void writeSimpleVarDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
    GrVariable[] variables = variableDeclaration.getVariables();
    //if (types.size() > 1) {
    if (variables.length > 1 && variableDeclaration.getParent() instanceof GrControlStatement) {
        expressionContext.setInsertCurlyBrackets();
    }
    for (GrVariable variable : variables) {
        writeVariableSeparately(variable, builder, expressionContext);
        builder.append(";\n");
    }
    builder.delete(builder.length() - 1, builder.length());
//builder.removeFromTheEnd(1);
/*return;
    }


    ModifierListGenerator.writeModifiers(builder, variableDeclaration.getModifierList());

    PsiType type = getVarType(variables[0]);
    writeType(builder, type, variableDeclaration);

    builder.append(" ");
    for (GrVariable variable : variables) {
      writeVariableWithoutType(builder, expressionContext, variable);
      builder.append(", ");
    }
    if (variables.length > 0) {
      builder.delete(builder.length() - 2, builder.length());
    }
    builder.append(";");*/
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)

Example 65 with GrVariable

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

the class LocalVarAnalyzer method visitReferenceExpression.

@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
    super.visitReferenceExpression(ref);
    PsiElement resolved = ref.resolve();
    if (!allVars.contains(resolved))
        return;
    GrVariable var = (GrVariable) resolved;
    if (PsiUtil.isAccessedForWriting(ref)) {
        rewritten.add(var);
    }
    if (allVars.get(var) < grade) {
        touched.add((PsiVariable) resolved);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5