Translate IfWhenUtils to Kotlin
This commit is contained in:
-162
@@ -1,162 +0,0 @@
|
||||
package org.jetbrains.jet.plugin.intentions.branchedTransformations;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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 java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
|
||||
|
||||
public class IfWhenUtils {
|
||||
|
||||
public static final String TRANSFORM_WITHOUT_CHECK =
|
||||
"Expression must be checked before applying transformation";
|
||||
|
||||
private IfWhenUtils() {
|
||||
}
|
||||
|
||||
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
|
||||
return ifExpression.getThen() != null;
|
||||
}
|
||||
|
||||
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
||||
return !whenExpression.getEntries().isEmpty();
|
||||
}
|
||||
|
||||
private static void assertNotNull(JetExpression expression) {
|
||||
assert expression != null : TRANSFORM_WITHOUT_CHECK;
|
||||
}
|
||||
|
||||
private static List<JetExpression> splitExpressionToOrBranches(@Nullable JetExpression expression) {
|
||||
if (expression == null) return Collections.emptyList();
|
||||
|
||||
final List<JetExpression> branches = new ArrayList<JetExpression>();
|
||||
|
||||
expression.accept(
|
||||
new JetVisitorVoid() {
|
||||
@Override
|
||||
public void visitBinaryExpression(JetBinaryExpression expression) {
|
||||
if (expression.getOperationToken() == JetTokens.OROR) {
|
||||
JetExpression left = expression.getLeft();
|
||||
JetExpression right = expression.getRight();
|
||||
|
||||
if (left != null) {
|
||||
left.accept(this);
|
||||
}
|
||||
|
||||
if (right != null) {
|
||||
right.accept(this);
|
||||
}
|
||||
} else {
|
||||
visitExpression(expression);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitParenthesizedExpression(JetParenthesizedExpression expression) {
|
||||
JetExpression baseExpression = expression.getExpression();
|
||||
|
||||
if (baseExpression != null) {
|
||||
baseExpression.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitExpression(JetExpression expression) {
|
||||
branches.add(expression);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return branches;
|
||||
}
|
||||
|
||||
public static void transformIfToWhen(@NotNull JetIfExpression ifExpression) {
|
||||
JetPsiFactory.WhenBuilder builder = new JetPsiFactory.WhenBuilder();
|
||||
|
||||
JetIfExpression currIfExpression = ifExpression;
|
||||
do {
|
||||
JetExpression condition = currIfExpression.getCondition();
|
||||
JetExpression thenBranch = currIfExpression.getThen();
|
||||
JetExpression elseBranch = currIfExpression.getElse();
|
||||
|
||||
assertNotNull(thenBranch);
|
||||
|
||||
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
||||
|
||||
if (orBranches.isEmpty()) {
|
||||
builder.condition("");
|
||||
} else {
|
||||
for (JetExpression orBranch : orBranches) {
|
||||
builder.condition(orBranch);
|
||||
}
|
||||
}
|
||||
|
||||
builder.branchExpression(thenBranch);
|
||||
|
||||
if (elseBranch instanceof JetIfExpression) {
|
||||
currIfExpression = (JetIfExpression) elseBranch;
|
||||
}
|
||||
else {
|
||||
currIfExpression = null;
|
||||
if (elseBranch != null) {
|
||||
builder.elseEntry(elseBranch);
|
||||
}
|
||||
}
|
||||
} while (currIfExpression != null);
|
||||
|
||||
JetWhenExpression whenExpression = builder.toExpression(ifExpression.getProject());
|
||||
if (BranchedTransformationsPackage.canIntroduceSubject(whenExpression)) {
|
||||
whenExpression = BranchedTransformationsPackage.introduceSubject(whenExpression);
|
||||
}
|
||||
|
||||
ifExpression.replace(whenExpression);
|
||||
}
|
||||
|
||||
private static String combineWhenConditions(JetWhenCondition[] conditions, JetExpression subject) {
|
||||
int n = conditions.length;
|
||||
if (n == 0) return "";
|
||||
|
||||
JetWhenCondition condition = conditions[0];
|
||||
assert condition != null : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String text = BranchedTransformationsPackage.toExpressionText(condition, subject);
|
||||
if (n > 1) {
|
||||
text = parenthesizeTextIfNeeded(text);
|
||||
}
|
||||
sb.append(text);
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
JetWhenCondition currCondition = conditions[i];
|
||||
assert currCondition != null : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
sb.append(" || ").append(parenthesizeTextIfNeeded(BranchedTransformationsPackage.toExpressionText(currCondition, subject)));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void transformWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
||||
JetPsiFactory.IfChainBuilder builder = new JetPsiFactory.IfChainBuilder();
|
||||
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
for (JetWhenEntry entry : entries) {
|
||||
JetExpression branch = entry.getExpression();
|
||||
|
||||
if (entry.isElse()) {
|
||||
builder.elseBranch(branch);
|
||||
} else {
|
||||
String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression());
|
||||
builder.ifBranch(branchConditionText, JetPsiUtil.getText(branch));
|
||||
}
|
||||
}
|
||||
|
||||
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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<JetExpression> {
|
||||
val branches = ArrayList<JetExpression>()
|
||||
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<JetIfExpression> = object: Iterator<JetIfExpression> {
|
||||
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<JetWhenCondition>, 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()))
|
||||
}
|
||||
+3
-3
@@ -17,15 +17,15 @@
|
||||
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions
|
||||
|
||||
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils
|
||||
import org.jetbrains.jet.lang.psi.JetWhenExpression
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.*
|
||||
|
||||
public class IfToWhenIntention : JetSelfTargetingIntention<JetIfExpression>("if.to.when", javaClass()) {
|
||||
override fun isApplicableTo(element: JetIfExpression): Boolean = IfWhenUtils.checkIfToWhen(element)
|
||||
override fun isApplicableTo(element: JetIfExpression): Boolean = element.canTransformToWhen()
|
||||
|
||||
override fun applyTo(element: JetIfExpression, editor: Editor) {
|
||||
IfWhenUtils.transformIfToWhen(element)
|
||||
element.transformToWhen()
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -17,15 +17,15 @@
|
||||
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions
|
||||
|
||||
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils
|
||||
import org.jetbrains.jet.lang.psi.JetWhenExpression
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.*
|
||||
|
||||
public class WhenToIfIntention : JetSelfTargetingIntention<JetWhenExpression>("if.to.when", javaClass()) {
|
||||
override fun isApplicableTo(element: JetWhenExpression): Boolean = IfWhenUtils.checkWhenToIf(element)
|
||||
override fun isApplicableTo(element: JetWhenExpression): Boolean = element.canTransformToIf()
|
||||
|
||||
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
||||
IfWhenUtils.transformWhenToIf(element)
|
||||
element.transformToIf()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user