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()]);
}
}
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) };
}
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;
}
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;
}
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());
}
Aggregations