Invert if: more correct handling of empty returns #KT-13444 Fixed

(cherry picked from commit be2adaf)
This commit is contained in:
Mikhail Glukhikh
2016-08-22 17:11:25 +03:00
committed by Mikhail Glukhikh
parent 25eefdb6ec
commit 605ac0b25e
12 changed files with 141 additions and 3 deletions
@@ -113,14 +113,21 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
val last = afterIfInBlock.last()
// build new then branch from statements after if (we will add exit statement if necessary later)
//TODO: no block if single?
val newThenRange = PsiChildRange(first, last).trimWhiteSpaces()
val newThenRange = if (isEmptyReturn(lastThenStatement) && isEmptyReturn(lastStatementInBlock)) {
PsiChildRange(first, lastStatementInBlock.prevSibling).trimWhiteSpaces()
}
else {
PsiChildRange(first, last).trimWhiteSpaces()
}
val newIf = factory.createExpressionByPattern("if ($0) { $1 }", newCondition, newThenRange) as KtIfExpression
// remove statements after if as they are moving under if
block.deleteChildRange(first, last)
if (lastThenStatement is KtReturnExpression && lastThenStatement.returnedExpression == null) {
lastThenStatement.delete()
if (isEmptyReturn(lastThenStatement)) {
if (block.parent is KtDeclarationWithBody && block.parent !is KtFunctionLiteral) {
lastThenStatement.delete()
}
}
val updatedIf = copyThenBranchAfter(ifExpression)
@@ -148,6 +155,9 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
return updatedIf.replace(newIf) as KtIfExpression
}
private fun isEmptyReturn(statement: KtExpression) =
statement is KtReturnExpression && statement.returnedExpression == null && statement.labeledExpression == null
private fun copyThenBranchAfter(ifExpression: KtIfExpression): KtIfExpression {
val factory = KtPsiFactory(ifExpression)
val thenBranch = ifExpression.then ?: return ifExpression