diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt index c6c405612f0..cdceee719d1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt @@ -46,14 +46,14 @@ object BranchedFoldingUtils { } fun getFoldableBranchedReturn(branch: KtExpression?): KtReturnExpression? = - (branch?.lastBlockStatementOrThis() as? KtReturnExpression)?.takeIf { - it.returnedExpression != null && - it.returnedExpression !is KtLambdaExpression && - it.getTargetLabel() == null - } + (branch?.lastBlockStatementOrThis() as? KtReturnExpression)?.takeIf { + it.returnedExpression != null && + it.returnedExpression !is KtLambdaExpression && + it.getTargetLabel() == null + } private fun checkAssignmentsMatch(a1: KtBinaryExpression, a2: KtBinaryExpression): Boolean = - a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken + a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken internal fun getFoldableAssignmentNumber(expression: KtExpression?): Int { expression ?: return -1 @@ -61,18 +61,15 @@ object BranchedFoldingUtils { fun collectAssignmentsAndCheck(e: KtExpression?): Boolean = when (e) { is KtWhenExpression -> { val entries = e.entries - !e.hasMissingCases() && - entries.isNotEmpty() && - entries.all { entry -> + !e.hasMissingCases() && entries.isNotEmpty() && entries.all { entry -> val assignment = getFoldableBranchedAssignment(entry.expression)?.run { assignments.add(this) } assignment != null || collectAssignmentsAndCheck(entry.expression?.lastBlockStatementOrThis()) } } is KtIfExpression -> { val branches = e.branches - branches.size > 1 && - (branches.lastOrNull()?.getStrictParentOfType()?.`else` != null) && - branches.all { branch -> + val elseBranch = branches.lastOrNull()?.getStrictParentOfType()?.`else` + branches.size > 1 && elseBranch != null && branches.all { branch -> val assignment = getFoldableBranchedAssignment(branch)?.run { assignments.add(this) } assignment != null || collectAssignmentsAndCheck(branch?.lastBlockStatementOrThis()) } @@ -105,25 +102,25 @@ object BranchedFoldingUtils { else false } - )) { + ) + ) { return -1 } return assignments.size } private fun getFoldableReturns(branches: List): List? = - branches.fold?>(mutableListOf()) { prevList, branch -> - if (prevList == null) return@fold null - val foldableBranchedReturn = getFoldableBranchedReturn(branch) - if (foldableBranchedReturn != null) { - prevList.add(foldableBranchedReturn) - } - else { - val currReturns = getFoldableReturns(branch?.lastBlockStatementOrThis()) ?: return@fold null - prevList += currReturns - } - prevList + branches.fold?>(mutableListOf()) { prevList, branch -> + if (prevList == null) return@fold null + val foldableBranchedReturn = getFoldableBranchedReturn(branch) + if (foldableBranchedReturn != null) { + prevList.add(foldableBranchedReturn) + } else { + val currReturns = getFoldableReturns(branch?.lastBlockStatementOrThis()) ?: return@fold null + prevList += currReturns } + prevList + } internal fun getFoldableReturns(expression: KtExpression?): List? = when (expression) { is KtWhenExpression -> { @@ -175,6 +172,7 @@ object BranchedFoldingUtils { replace(rhs) } } + fun lift(e: KtExpression?) { when (e) { is KtWhenExpression -> e.entries.forEach { entry -> @@ -196,19 +194,18 @@ object BranchedFoldingUtils { fun KtReturnExpression.replaceWithReturned() { replace(returnedExpression!!) } + fun lift(e: KtExpression?) { when (e) { is KtWhenExpression -> e.entries.forEach { entry -> - getFoldableBranchedReturn(entry.expression)?.replaceWithReturned() - ?: lift(entry.expression?.lastBlockStatementOrThis()) + val entryExpr = entry.expression + getFoldableBranchedReturn(entryExpr)?.replaceWithReturned() ?: lift(entryExpr?.lastBlockStatementOrThis()) } is KtIfExpression -> e.branches.forEach { branch -> - getFoldableBranchedReturn(branch)?.replaceWithReturned() ?: - lift(branch?.lastBlockStatementOrThis()) + getFoldableBranchedReturn(branch)?.replaceWithReturned() ?: lift(branch?.lastBlockStatementOrThis()) } is KtTryExpression -> e.tryBlockAndCatchBodies().forEach { - getFoldableBranchedReturn(it)?.replaceWithReturned() ?: - lift(it?.lastBlockStatementOrThis()) + getFoldableBranchedReturn(it)?.replaceWithReturned() ?: lift(it?.lastBlockStatementOrThis()) } } } @@ -219,6 +216,6 @@ object BranchedFoldingUtils { private fun KtTryExpression.tryBlockAndCatchBodies(): List = listOf(tryBlock) + catchClauses.map { it.catchBody } private fun KtWhenExpression.hasMissingCases(): Boolean = - !KtPsiUtil.checkWhenExpressionHasSingleElse(this) && WhenChecker.getMissingCases(this, this.analyze()).isNotEmpty() + !KtPsiUtil.checkWhenExpressionHasSingleElse(this) && WhenChecker.getMissingCases(this, this.analyze()).isNotEmpty() }