Search in sources :

Example 1 with VariableInfo

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo in project intellij-community by JetBrains.

the class ExtractUtil method createResultStatement.

@NotNull
private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
    VariableInfo[] outputVars = helper.getOutputVariableInfos();
    PsiType type = helper.getOutputType();
    GrStatement[] statements = helper.getStatements();
    GrMethodCallExpression callExpression = createMethodCall(helper);
    if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
        return new GrStatement[] { callExpression };
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    if (helper.hasReturnValue()) {
        return new GrStatement[] { factory.createStatementFromText("return " + callExpression.getText()) };
    }
    LOG.assertTrue(outputVars.length > 0);
    final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
    if (mustAdd.isEmpty()) {
        return new GrStatement[] { createAssignment(outputVars, callExpression, helper.getProject()) };
    } else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
        return new GrVariableDeclaration[] { factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, callExpression, outputVars[0].getType(), outputVars[0].getName()) };
    } else if (varsAreEqual(mustAdd, outputVars)) {
        return createTupleDeclaration(outputVars, callExpression, helper.getProject());
    } else {
        final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
        result.add(createAssignment(outputVars, callExpression, helper.getProject()));
        return result.toArray(new GrStatement[result.size()]);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with VariableInfo

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo in project intellij-community by JetBrains.

the class ExtractUtil method createTupleDeclaration.

private static GrStatement[] createTupleDeclaration(final VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    StringBuilder tuple = new StringBuilder();
    tuple.append("def (");
    for (VariableInfo info : infos) {
        final PsiType type = info.getType();
        if (type != null) {
            final PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(type);
            tuple.append(unboxed.getCanonicalText());
            tuple.append(' ');
        }
        tuple.append(info.getName());
        tuple.append(",");
    }
    StringUtil.trimEnd(tuple, ",");
    tuple.append(")=");
    tuple.append(callExpression.getText());
    return new GrStatement[] { factory.createStatementFromText(tuple) };
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 3 with VariableInfo

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo in project intellij-community by JetBrains.

the class ExtractUtil method haveDifferentTypes.

private static boolean haveDifferentTypes(List<VariableInfo> varInfos) {
    if (varInfos.size() < 2)
        return true;
    Set<String> diffTypes = new HashSet<>();
    for (VariableInfo info : varInfos) {
        final PsiType t = info.getType();
        diffTypes.add(t == null ? null : TypesUtil.unboxPrimitiveTypeWrapper(t).getCanonicalText());
    }
    return diffTypes.size() > 1;
}
Also used : VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType)

Example 4 with VariableInfo

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo in project intellij-community by JetBrains.

the class ExtractUtil method generateVarDeclarations.

private static List<GrStatement> generateVarDeclarations(List<VariableInfo> varInfos, Project project, @Nullable GrExpression initializer) {
    List<GrStatement> result = new ArrayList<>();
    if (varInfos.isEmpty())
        return result;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    boolean distinctDeclaration = haveDifferentTypes(varInfos);
    if (distinctDeclaration) {
        for (VariableInfo info : varInfos) {
            result.add(factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", info.getType(), info.getName()));
        }
    } else {
        String[] names = new String[varInfos.size()];
        for (int i = 0, mustAddLength = varInfos.size(); i < mustAddLength; i++) {
            names[i] = varInfos.get(i).getName();
        }
        result.add(factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, initializer, varInfos.get(0).getType(), names));
    }
    return result;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 5 with VariableInfo

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo in project intellij-community by JetBrains.

the class ExtractUtil method createAssignment.

private static GrStatement createAssignment(VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
    StringBuilder text = new StringBuilder();
    if (infos.length > 1)
        text.append('(');
    for (VariableInfo info : infos) {
        text.append(info.getName()).append(", ");
    }
    if (infos.length > 1) {
        text.replace(text.length() - 2, text.length(), ") =");
    } else {
        text.replace(text.length() - 2, text.length(), " = ");
    }
    text.append(callExpression.getText());
    return GroovyPsiElementFactory.getInstance(project).createExpressionFromText(text.toString());
}
Also used : VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo)

Aggregations

VariableInfo (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo)8 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 PsiType (com.intellij.psi.PsiType)4 NotNull (org.jetbrains.annotations.NotNull)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 PsiElement (com.intellij.psi.PsiElement)1 HashMap (com.intellij.util.containers.HashMap)1 HashSet (com.intellij.util.containers.HashSet)1 ArrayList (java.util.ArrayList)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)1 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)1 ControlFlowBuilder (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder)1 FragmentVariableInfos (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.FragmentVariableInfos)1 GrRefactoringError (org.jetbrains.plugins.groovy.refactoring.GrRefactoringError)1