diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt index 3e1eb0bb681..8c54260d450 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPsiFactory.kt @@ -374,44 +374,6 @@ public class JetPsiFactory(private val project: Project) { return createClass("class A { constructor()$colonOrEmpty$text {}").getSecondaryConstructors().first().getDelegationCall() } - public inner class IfChainBuilder() { - private val sb = StringBuilder() - private var first = true - private var frozen = false - - public fun ifBranch(conditionText: String, expressionText: String): IfChainBuilder { - if (first) { - first = false - } - else { - sb.append("else ") - } - - sb.append("if (").append(conditionText).append(") ").append(expressionText).append("\n") - return this - } - - public fun ifBranch(condition: JetExpression, expression: JetExpression): IfChainBuilder { - return ifBranch(condition.getText()!!, expression.getText()!!) - } - - public fun elseBranch(expressionText: String): IfChainBuilder { - sb.append("else ").append(expressionText) - return this - } - - public fun elseBranch(expression: JetExpression?): IfChainBuilder { - return elseBranch(JetPsiUtil.getText(expression)) - } - - public fun toExpression(): JetIfExpression { - if (!frozen) { - frozen = true - } - return createExpression(sb.toString()) as JetIfExpression - } - } - public class CallableBuilder(private val target: Target) { public enum class Target { FUNCTION diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt index a015fb70124..71117d374fa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt @@ -23,25 +23,37 @@ import org.jetbrains.kotlin.psi.* public class WhenToIfIntention : JetSelfTargetingIntention(javaClass(), "Replace 'when' with 'if'") { override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean { - if (element.getEntries().isEmpty()) return false + val entries = element.getEntries() + if (entries.isEmpty()) return false + val lastEntry = entries.last() + if (entries.any { it != lastEntry && it.isElse() }) return false + return element.getWhenKeywordElement().getTextRange().containsOffset(caretOffset) } override fun applyTo(element: JetWhenExpression, editor: Editor) { - val builder = JetPsiFactory(element).IfChainBuilder() - - for (entry in element.getEntries()) { - val branch = entry.getExpression() - if (entry.isElse()) { - builder.elseBranch(branch) - } - else { - val branchConditionText = combineWhenConditions(entry.getConditions(), element.getSubjectExpression()) - builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch)) + val ifExpression = JetPsiFactory(element).buildExpression { + for ((i, entry) in element.getEntries().withIndex()) { + if (i > 0) { + appendFixedText("else ") + } + val branch = entry.getExpression() + if (entry.isElse()) { + appendExpression(branch) + appendFixedText("\n") + } + else { + val branchConditionText = combineWhenConditions(entry.getConditions(), element.getSubjectExpression()) + appendFixedText("if (") + appendNonFormattedText(branchConditionText) + appendFixedText(")") + appendExpression(branch) + appendFixedText("\n") + } } } - element.replace(builder.toExpression()) + element.replace(ifExpression) } private fun combineWhenConditions(conditions: Array, subject: JetExpression?): String {