Minor if-to-when refactoring

This commit is contained in:
Mikhail Glukhikh
2016-03-18 18:54:01 +03:00
parent fa069214fd
commit 90b5d3b9f5
@@ -24,6 +24,7 @@ import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject 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.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
@@ -66,9 +67,6 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
} }
} }
private fun KtExpression?.unwrapBlockIfNeeded(): KtExpression? =
(this as? KtBlockExpression)?.statements?.singleOrNull() ?: this
private fun KtIfExpression.siblingsUpTo(other: KtExpression): List<PsiElement> { private fun KtIfExpression.siblingsUpTo(other: KtExpression): List<PsiElement> {
val result = ArrayList<PsiElement>() val result = ArrayList<PsiElement>()
var nextSibling = nextSibling var nextSibling = nextSibling
@@ -85,7 +83,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?) { private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?) {
appendFixedText("else->") appendFixedText("else->")
appendExpression(block.unwrapBlockIfNeeded()) appendExpression(block?.unwrapBlockOrParenthesis())
appendFixedText("\n") appendFixedText("\n")
} }
@@ -112,7 +110,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
appendFixedText("->") appendFixedText("->")
val currentThenBranch = currentIfExpression.then val currentThenBranch = currentIfExpression.then
appendExpression(currentThenBranch.unwrapBlockIfNeeded()) appendExpression(currentThenBranch?.unwrapBlockOrParenthesis())
appendFixedText("\n") appendFixedText("\n")
canPassThrough = canPassThrough || canPassThrough(currentThenBranch) canPassThrough = canPassThrough || canPassThrough(currentThenBranch)