From a24ee8745c8a3f79b508b18fb7e4e466155d81a4 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 18 Nov 2013 14:44:15 +0400 Subject: [PATCH] Merge WhenUtils with IfWhenUtils --- .../branchedTransformations/IfWhenUtils.kt | 123 ------------------ ...tils.kt => branchedTransformationUtils.kt} | 100 ++++++++++++++ 2 files changed, 100 insertions(+), 123 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/IfWhenUtils.kt rename idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/{WhenUtils.kt => branchedTransformationUtils.kt} (68%) diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/IfWhenUtils.kt b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/IfWhenUtils.kt deleted file mode 100644 index 5bcec481cf3..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/IfWhenUtils.kt +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.plugin.intentions.branchedTransformations - -import org.jetbrains.annotations.Nullable -import org.jetbrains.jet.lang.psi.* -import org.jetbrains.jet.lexer.JetTokens -import java.util.ArrayList -import java.util.Collections -import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.* - -public fun JetIfExpression.canTransformToWhen(): Boolean = getThen() != null - -public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty() - -public fun JetIfExpression.transformToWhen() { - fun JetExpression.splitToOrBranches(): List { - val branches = ArrayList() - accept( - object : JetVisitorVoid() { - public override fun visitBinaryExpression(expression: JetBinaryExpression) { - if (expression.getOperationToken() == JetTokens.OROR) { - expression.getLeft()?.accept(this) - expression.getRight()?.accept(this) - } - else { - visitExpression(expression) - } - } - - public override fun visitParenthesizedExpression(expression: JetParenthesizedExpression) { - expression.getExpression()?.accept(this) - } - - public override fun visitExpression(expression: JetExpression) { - branches.add(expression) - } - } - ) - return branches - } - - fun branchIterator(ifExpression: JetIfExpression): Iterator = object: Iterator { - private var expression: JetIfExpression? = ifExpression - - override fun next(): JetIfExpression { - val current = expression!! - expression = current.getElse()?.let { next -> if (next is JetIfExpression) next else null } - return current - } - - override fun hasNext(): Boolean = expression != null - } - - val builder = JetPsiFactory.WhenBuilder() - branchIterator(this).forEach { ifExpression -> - ifExpression.getCondition()?.let { condition -> - val orBranches = condition.splitToOrBranches() - if (orBranches.isEmpty()) { - builder.condition("") - } - else { - orBranches.forEach { branch -> builder.condition(branch) } - } - } - - builder.branchExpression(ifExpression.getThen()) - - ifExpression.getElse()?.let { elseBranch -> - if (elseBranch !is JetIfExpression) { - builder.elseEntry(elseBranch) - } - } - } - - val whenExpression = builder.toExpression(getProject()).let { whenExpression -> - if (whenExpression.canIntroduceSubject()) whenExpression.introduceSubject() else whenExpression - } - replace(whenExpression) -} - -public fun JetWhenExpression.transformToIf() { - fun combineWhenConditions(conditions: Array, subject: JetExpression?): String { - return when (conditions.size) { - 0 -> "" - 1 -> conditions[0].toExpressionText(subject) - else -> { - conditions - .map { condition -> parenthesizeTextIfNeeded(condition.toExpressionText(subject)) } - .makeString(separator = " || ") - } - } - } - - val builder = JetPsiFactory.IfChainBuilder() - - for (entry in getEntries()) { - val branch = entry.getExpression() - if (entry.isElse()) { - builder.elseBranch(branch) - } - else { - val branchConditionText = combineWhenConditions(entry.getConditions(), getSubjectExpression()) - builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch)) - } - } - - replace(builder.toExpression(getProject())) -} diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/WhenUtils.kt b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/branchedTransformationUtils.kt similarity index 68% rename from idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/WhenUtils.kt rename to idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/branchedTransformationUtils.kt index 5a27f66d8b2..f618e6c5f45 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/WhenUtils.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/branchedTransformationUtils.kt @@ -22,6 +22,7 @@ import org.jetbrains.jet.lexer.JetTokens import org.jetbrains.jet.plugin.util.JetPsiMatcher import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.* import org.jetbrains.jet.lang.psi.psiUtil.* +import java.util.ArrayList public val TRANSFORM_WITHOUT_CHECK: String = "Expression must be checked before applying transformation" @@ -216,3 +217,102 @@ public fun JetWhenExpression.eliminateSubject(): JetWhenExpression { return replaced(builder.toExpression(getProject())) } + +public fun JetIfExpression.canTransformToWhen(): Boolean = getThen() != null + +public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty() + +public fun JetIfExpression.transformToWhen() { + fun JetExpression.splitToOrBranches(): List { + val branches = ArrayList() + accept( + object : JetVisitorVoid() { + public override fun visitBinaryExpression(expression: JetBinaryExpression) { + if (expression.getOperationToken() == JetTokens.OROR) { + expression.getLeft()?.accept(this) + expression.getRight()?.accept(this) + } + else { + visitExpression(expression) + } + } + + public override fun visitParenthesizedExpression(expression: JetParenthesizedExpression) { + expression.getExpression()?.accept(this) + } + + public override fun visitExpression(expression: JetExpression) { + branches.add(expression) + } + } + ) + return branches + } + + fun branchIterator(ifExpression: JetIfExpression): Iterator = object: Iterator { + private var expression: JetIfExpression? = ifExpression + + override fun next(): JetIfExpression { + val current = expression!! + expression = current.getElse()?.let { next -> if (next is JetIfExpression) next else null } + return current + } + + override fun hasNext(): Boolean = expression != null + } + + val builder = JetPsiFactory.WhenBuilder() + branchIterator(this).forEach { ifExpression -> + ifExpression.getCondition()?.let { condition -> + val orBranches = condition.splitToOrBranches() + if (orBranches.isEmpty()) { + builder.condition("") + } + else { + orBranches.forEach { branch -> builder.condition(branch) } + } + } + + builder.branchExpression(ifExpression.getThen()) + + ifExpression.getElse()?.let { elseBranch -> + if (elseBranch !is JetIfExpression) { + builder.elseEntry(elseBranch) + } + } + } + + val whenExpression = builder.toExpression(getProject()).let { whenExpression -> + if (whenExpression.canIntroduceSubject()) whenExpression.introduceSubject() else whenExpression + } + replace(whenExpression) +} + +public fun JetWhenExpression.transformToIf() { + fun combineWhenConditions(conditions: Array, subject: JetExpression?): String { + return when (conditions.size) { + 0 -> "" + 1 -> conditions[0].toExpressionText(subject) + else -> { + conditions + .map { condition -> parenthesizeTextIfNeeded(condition.toExpressionText(subject)) } + .makeString(separator = " || ") + } + } + } + + val builder = JetPsiFactory.IfChainBuilder() + + for (entry in getEntries()) { + val branch = entry.getExpression() + if (entry.isElse()) { + builder.elseBranch(branch) + } + else { + val branchConditionText = combineWhenConditions(entry.getConditions(), getSubjectExpression()) + builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch)) + } + } + + replace(builder.toExpression(getProject())) +}