@SuppressWarnings("ConstantConditions") public static void foldWhenExpressionWithAssignments(JetWhenExpression whenExpression) { Project project = whenExpression.getProject(); assert !whenExpression.getEntries().isEmpty() : FOLD_WITHOUT_CHECK; JetBinaryExpression firstAssignment = getFoldableBranchedAssignment(whenExpression.getEntries().get(0).getExpression()); assertNotNull(firstAssignment); String op = firstAssignment.getOperationReference().getText(); JetSimpleNameExpression lhs = (JetSimpleNameExpression) firstAssignment.getLeft(); JetBinaryExpression assignment = JetPsiFactory.createBinaryExpression(project, lhs, op, whenExpression); JetWhenExpression newWhenExpression = (JetWhenExpression) assignment.getRight(); assertNotNull(newWhenExpression); for (JetWhenEntry entry : newWhenExpression.getEntries()) { JetBinaryExpression currAssignment = getFoldableBranchedAssignment(entry.getExpression()); assertNotNull(currAssignment); JetExpression currRhs = currAssignment.getRight(); assertNotNull(currRhs); currAssignment.replace(currRhs); } whenExpression.replace(assignment); }
public static void foldIfExpressionWithAssignments(JetIfExpression ifExpression) { Project project = ifExpression.getProject(); JetBinaryExpression thenAssignment = getFoldableBranchedAssignment(ifExpression.getThen()); assertNotNull(thenAssignment); String op = thenAssignment.getOperationReference().getText(); JetSimpleNameExpression lhs = (JetSimpleNameExpression) thenAssignment.getLeft(); JetBinaryExpression assignment = JetPsiFactory.createBinaryExpression(project, lhs, op, ifExpression); JetIfExpression newIfExpression = (JetIfExpression) assignment.getRight(); assertNotNull(newIfExpression); //noinspection ConstantConditions thenAssignment = getFoldableBranchedAssignment(newIfExpression.getThen()); JetBinaryExpression elseAssignment = getFoldableBranchedAssignment(newIfExpression.getElse()); assertNotNull(thenAssignment); assertNotNull(elseAssignment); JetExpression thenRhs = thenAssignment.getRight(); JetExpression elseRhs = elseAssignment.getRight(); assertNotNull(thenRhs); assertNotNull(elseRhs); //noinspection ConstantConditions thenAssignment.replace(thenRhs); //noinspection ConstantConditions elseAssignment.replace(elseRhs); ifExpression.replace(assignment); }