Code corrections after conversion

This commit is contained in:
Valentin Kipyatkov
2015-05-09 11:04:15 +03:00
parent b1ef08c9a1
commit d77b77809c
@@ -23,48 +23,36 @@ import org.jetbrains.kotlin.psi.*
import java.util.ArrayList import java.util.ArrayList
public object BranchedFoldingUtils { public object BranchedFoldingUtils {
private fun checkEquivalence(e1: JetExpression, e2: JetExpression): Boolean {
return e1.getText() == e2.getText()
}
private val CHECK_ASSIGNMENT = object : Predicate<JetElement> { private val CHECK_ASSIGNMENT = object : Predicate<JetElement> {
override fun apply(input: JetElement?): Boolean { override fun apply(input: JetElement): Boolean {
if (input == null || !JetPsiUtil.isAssignment(input)) { if (!JetPsiUtil.isAssignment(input)) return false
return false
}
val assignment = input as JetBinaryExpression val assignment = input as JetBinaryExpression
if (assignment.getRight() == null || assignment.getLeft() !is JetSimpleNameExpression) { val left = assignment.getLeft() as? JetSimpleNameExpression ?: return false
return false if (assignment.getRight() == null) return false
}
if (assignment.getParent() is JetBlockExpression) { val parent = assignment.getParent()
//noinspection ConstantConditions if (parent is JetBlockExpression) {
return !JetPsiUtil.checkVariableDeclarationInBlock(assignment.getParent() as JetBlockExpression, assignment.getLeft()!!.getText()) return !JetPsiUtil.checkVariableDeclarationInBlock(parent, left.getText())
} }
return true return true
} }
} }
private val CHECK_RETURN = object : Predicate<JetElement> { private fun getFoldableBranchedAssignment(branch: JetExpression?): JetBinaryExpression? {
override fun apply(input: JetElement?): Boolean {
return (input is JetReturnExpression) && input.getReturnedExpression() != null
}
}
private fun getFoldableBranchedAssignment(branch: JetExpression): JetBinaryExpression? {
return JetPsiUtil.getOutermostLastBlockElement(branch, CHECK_ASSIGNMENT) as JetBinaryExpression? return JetPsiUtil.getOutermostLastBlockElement(branch, CHECK_ASSIGNMENT) as JetBinaryExpression?
} }
private fun getFoldableBranchedReturn(branch: JetExpression): JetReturnExpression? { private fun getFoldableBranchedReturn(branch: JetExpression?): JetReturnExpression? {
return JetPsiUtil.getOutermostLastBlockElement(branch, CHECK_RETURN) as JetReturnExpression? return JetPsiUtil.getOutermostLastBlockElement(branch) {
(it as? JetReturnExpression)?.getReturnedExpression() != null
} as JetReturnExpression?
} }
private fun checkAssignmentsMatch(a1: JetBinaryExpression, a2: JetBinaryExpression): Boolean { private fun checkAssignmentsMatch(a1: JetBinaryExpression, a2: JetBinaryExpression): Boolean {
return checkEquivalence(a1.getLeft(), a2.getLeft()) && a1.getOperationToken() == a2.getOperationToken() return a1.getLeft()?.getText() == a2.getLeft()?.getText() && a1.getOperationToken() == a2.getOperationToken()
} }
private fun checkFoldableIfExpressionWithAssignments(ifExpression: JetIfExpression): Boolean { private fun checkFoldableIfExpressionWithAssignments(ifExpression: JetIfExpression): Boolean {
@@ -145,16 +133,8 @@ public object BranchedFoldingUtils {
return null return null
} }
public val FOLD_WITHOUT_CHECK: String = "Expression must be checked before folding"
private fun assertNotNull(expression: JetExpression?) {
assert(expression != null) { FOLD_WITHOUT_CHECK }
}
public fun foldIfExpressionWithAssignments(ifExpression: JetIfExpression) { public fun foldIfExpressionWithAssignments(ifExpression: JetIfExpression) {
var thenAssignment: JetBinaryExpression = getFoldableBranchedAssignment(ifExpression.getThen()) var thenAssignment = getFoldableBranchedAssignment(ifExpression.getThen()!!)!!
assertNotNull(thenAssignment)
val op = thenAssignment.getOperationReference().getText() val op = thenAssignment.getOperationReference().getText()
val lhs = thenAssignment.getLeft() as JetSimpleNameExpression val lhs = thenAssignment.getLeft() as JetSimpleNameExpression
@@ -162,24 +142,13 @@ public object BranchedFoldingUtils {
val assignment = JetPsiFactory(ifExpression).createExpressionByPattern("$0 $1 $2", lhs, op, ifExpression) val assignment = JetPsiFactory(ifExpression).createExpressionByPattern("$0 $1 $2", lhs, op, ifExpression)
val newIfExpression = (assignment as JetBinaryExpression).getRight() as JetIfExpression val newIfExpression = (assignment as JetBinaryExpression).getRight() as JetIfExpression
assertNotNull(newIfExpression) thenAssignment = getFoldableBranchedAssignment(newIfExpression.getThen()!!)!!
val elseAssignment = getFoldableBranchedAssignment(newIfExpression.getElse()!!)!!
//noinspection ConstantConditions val thenRhs = thenAssignment.getRight()!!
thenAssignment = getFoldableBranchedAssignment(newIfExpression.getThen()) val elseRhs = elseAssignment.getRight()!!
val elseAssignment = getFoldableBranchedAssignment(newIfExpression.getElse())
assertNotNull(thenAssignment)
assertNotNull(elseAssignment)
val thenRhs = thenAssignment.getRight()
val elseRhs = elseAssignment.getRight()
assertNotNull(thenRhs)
assertNotNull(elseRhs)
//noinspection ConstantConditions
thenAssignment.replace(thenRhs) thenAssignment.replace(thenRhs)
//noinspection ConstantConditions
elseAssignment.replace(elseRhs) elseAssignment.replace(elseRhs)
ifExpression.replace(assignment) ifExpression.replace(assignment)
@@ -189,76 +158,46 @@ public object BranchedFoldingUtils {
val newReturnExpression = JetPsiFactory(ifExpression).createReturn(ifExpression) val newReturnExpression = JetPsiFactory(ifExpression).createReturn(ifExpression)
val newIfExpression = newReturnExpression.getReturnedExpression() as JetIfExpression val newIfExpression = newReturnExpression.getReturnedExpression() as JetIfExpression
assertNotNull(newIfExpression) val thenReturn = getFoldableBranchedReturn(newIfExpression.getThen()!!)!!
val elseReturn = getFoldableBranchedReturn(newIfExpression.getElse()!!)!!
//noinspection ConstantConditions val thenExpr = thenReturn.getReturnedExpression()!!
val thenReturn = getFoldableBranchedReturn(newIfExpression.getThen()) val elseExpr = elseReturn.getReturnedExpression()!!
val elseReturn = getFoldableBranchedReturn(newIfExpression.getElse())
assertNotNull(thenReturn)
assertNotNull(elseReturn)
val thenExpr = thenReturn.getReturnedExpression()
val elseExpr = elseReturn.getReturnedExpression()
assertNotNull(thenExpr)
assertNotNull(elseExpr)
//noinspection ConstantConditions
thenReturn.replace(thenExpr) thenReturn.replace(thenExpr)
//noinspection ConstantConditions
elseReturn.replace(elseExpr) elseReturn.replace(elseExpr)
ifExpression.replace(newReturnExpression) ifExpression.replace(newReturnExpression)
} }
public fun foldIfExpressionWithAsymmetricReturns(ifExpression: JetIfExpression) { public fun foldIfExpressionWithAsymmetricReturns(ifExpression: JetIfExpression) {
val condition = ifExpression.getCondition() val condition = ifExpression.getCondition()!!
val thenRoot = ifExpression.getThen() val thenRoot = ifExpression.getThen()!!
val elseRoot = JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression) as JetExpression val elseRoot = JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression) as JetExpression
assertNotNull(condition)
assertNotNull(thenRoot)
assertNotNull(elseRoot)
//noinspection ConstantConditions
val psiFactory = JetPsiFactory(ifExpression) val psiFactory = JetPsiFactory(ifExpression)
var newIfExpression = psiFactory.createIf(condition, thenRoot, elseRoot) var newIfExpression = psiFactory.createIf(condition, thenRoot, elseRoot)
val newReturnExpression = psiFactory.createReturn(newIfExpression) val newReturnExpression = psiFactory.createReturn(newIfExpression)
newIfExpression = newReturnExpression.getReturnedExpression() as JetIfExpression newIfExpression = newReturnExpression.getReturnedExpression() as JetIfExpression
assertNotNull(newIfExpression) val thenReturn = getFoldableBranchedReturn(newIfExpression.getThen()!!)!!
val elseReturn = getFoldableBranchedReturn(newIfExpression.getElse()!!)!!
//noinspection ConstantConditions val thenExpr = thenReturn.getReturnedExpression()!!
val thenReturn = getFoldableBranchedReturn(newIfExpression.getThen()) val elseExpr = elseReturn.getReturnedExpression()!!
val elseReturn = getFoldableBranchedReturn(newIfExpression.getElse())
assertNotNull(thenReturn)
assertNotNull(elseReturn)
val thenExpr = thenReturn.getReturnedExpression()
val elseExpr = elseReturn.getReturnedExpression()
assertNotNull(thenExpr)
assertNotNull(elseExpr)
//noinspection ConstantConditions
thenReturn.replace(thenExpr) thenReturn.replace(thenExpr)
//noinspection ConstantConditions
elseReturn.replace(elseExpr) elseReturn.replace(elseExpr)
elseRoot.delete() elseRoot.delete()
ifExpression.replace(newReturnExpression) ifExpression.replace(newReturnExpression)
} }
SuppressWarnings("ConstantConditions")
public fun foldWhenExpressionWithAssignments(whenExpression: JetWhenExpression) { public fun foldWhenExpressionWithAssignments(whenExpression: JetWhenExpression) {
assert(!whenExpression.getEntries().isEmpty()) { FOLD_WITHOUT_CHECK } assert(!whenExpression.getEntries().isEmpty())
val firstAssignment = getFoldableBranchedAssignment(whenExpression.getEntries().get(0).getExpression()) val firstAssignment = getFoldableBranchedAssignment(whenExpression.getEntries().get(0).getExpression()!!)!!
assertNotNull(firstAssignment)
val op = firstAssignment.getOperationReference().getText() val op = firstAssignment.getOperationReference().getText()
val lhs = firstAssignment.getLeft() as JetSimpleNameExpression val lhs = firstAssignment.getLeft() as JetSimpleNameExpression
@@ -266,17 +205,9 @@ public object BranchedFoldingUtils {
val assignment = JetPsiFactory(whenExpression).createExpressionByPattern("$0 $1 $2", lhs, op, whenExpression) val assignment = JetPsiFactory(whenExpression).createExpressionByPattern("$0 $1 $2", lhs, op, whenExpression)
val newWhenExpression = (assignment as JetBinaryExpression).getRight() as JetWhenExpression val newWhenExpression = (assignment as JetBinaryExpression).getRight() as JetWhenExpression
assertNotNull(newWhenExpression)
for (entry in newWhenExpression.getEntries()) { for (entry in newWhenExpression.getEntries()) {
val currAssignment = getFoldableBranchedAssignment(entry.getExpression()) val currAssignment = getFoldableBranchedAssignment(entry.getExpression()!!)!!
val currRhs = currAssignment.getRight()!!
assertNotNull(currAssignment)
val currRhs = currAssignment.getRight()
assertNotNull(currRhs)
currAssignment.replace(currRhs) currAssignment.replace(currRhs)
} }
@@ -284,24 +215,14 @@ public object BranchedFoldingUtils {
} }
public fun foldWhenExpressionWithReturns(whenExpression: JetWhenExpression) { public fun foldWhenExpressionWithReturns(whenExpression: JetWhenExpression) {
assert(!whenExpression.getEntries().isEmpty()) { FOLD_WITHOUT_CHECK } assert(!whenExpression.getEntries().isEmpty())
val newReturnExpression = JetPsiFactory(whenExpression).createReturn(whenExpression) val newReturnExpression = JetPsiFactory(whenExpression).createReturn(whenExpression)
val newWhenExpression = newReturnExpression.getReturnedExpression() as JetWhenExpression val newWhenExpression = newReturnExpression.getReturnedExpression() as JetWhenExpression
assertNotNull(newWhenExpression)
//noinspection ConstantConditions
for (entry in newWhenExpression.getEntries()) { for (entry in newWhenExpression.getEntries()) {
val currReturn = getFoldableBranchedReturn(entry.getExpression()) val currReturn = getFoldableBranchedReturn(entry.getExpression()!!)!!
val currExpr = currReturn.getReturnedExpression()!!
assertNotNull(currReturn)
val currExpr = currReturn.getReturnedExpression()
assertNotNull(currExpr)
//noinspection ConstantConditions
currReturn.replace(currExpr) currReturn.replace(currExpr)
} }