From 90b5d3b9f50fa2e609a5255a979f1eb871415993 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 18 Mar 2016 18:54:01 +0300 Subject: [PATCH] Minor if-to-when refactoring --- .../intentions/IfToWhenIntention.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt index ab794bf9cc2..3cb20b10139 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt @@ -24,6 +24,7 @@ import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.unwrapBlockOrParenthesis import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* @@ -66,9 +67,6 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres } } - private fun KtExpression?.unwrapBlockIfNeeded(): KtExpression? = - (this as? KtBlockExpression)?.statements?.singleOrNull() ?: this - private fun KtIfExpression.siblingsUpTo(other: KtExpression): List { val result = ArrayList() var nextSibling = nextSibling @@ -85,7 +83,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?) { appendFixedText("else->") - appendExpression(block.unwrapBlockIfNeeded()) + appendExpression(block?.unwrapBlockOrParenthesis()) appendFixedText("\n") } @@ -112,7 +110,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres appendFixedText("->") val currentThenBranch = currentIfExpression.then - appendExpression(currentThenBranch.unwrapBlockIfNeeded()) + appendExpression(currentThenBranch?.unwrapBlockOrParenthesis()) appendFixedText("\n") canPassThrough = canPassThrough || canPassThrough(currentThenBranch)