Converted file to Kotlin

This commit is contained in:
Valentin Kipyatkov
2015-05-11 15:48:19 +03:00
parent 224e73e4f0
commit a81f445190
3 changed files with 177 additions and 186 deletions
@@ -1,180 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.idea.intentions.branchedTransformations;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils;
import org.jetbrains.kotlin.psi.*;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern;
public class BranchedUnfoldingUtils {
private BranchedUnfoldingUtils() {
}
private static JetExpression getOutermostLastBlockElement(@Nullable JetExpression expression) {
return (JetExpression) JetPsiUtil.getOutermostLastBlockElement(expression, JetPsiUtil.ANY_JET_ELEMENT);
}
@Nullable
public static UnfoldableKind getUnfoldableExpressionKind(@Nullable JetExpression root) {
if (root == null) return null;
if (JetPsiUtil.isAssignment(root)) {
JetBinaryExpression assignment = (JetBinaryExpression) root;
if (assignment.getLeft() == null) return null;
JetExpression rhs = assignment.getRight();
if (rhs instanceof JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF;
if (rhs instanceof JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse((JetWhenExpression) rhs)) {
return UnfoldableKind.ASSIGNMENT_TO_WHEN;
}
}
else if (root instanceof JetReturnExpression) {
JetExpression resultExpr = ((JetReturnExpression) root).getReturnedExpression();
if (resultExpr instanceof JetIfExpression) return UnfoldableKind.RETURN_TO_IF;
if (resultExpr instanceof JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse((JetWhenExpression) resultExpr)) {
return UnfoldableKind.RETURN_TO_WHEN;
}
}
else if (root instanceof JetProperty) {
JetProperty property = (JetProperty) root;
if (!property.isLocal()) return null;
JetExpression initializer = property.getInitializer();
if (initializer instanceof JetIfExpression) return UnfoldableKind.PROPERTY_TO_IF;
if (initializer instanceof JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse((JetWhenExpression) initializer)) {
return UnfoldableKind.PROPERTY_TO_WHEN;
}
}
return null;
}
public static final String UNFOLD_WITHOUT_CHECK = "Expression must be checked before unfolding";
private static void assertNotNull(Object value) {
assert value != null : UNFOLD_WITHOUT_CHECK;
}
public static void unfoldAssignmentToIf(@NotNull JetBinaryExpression assignment, @NotNull Editor editor) {
String op = assignment.getOperationReference().getText();
JetExpression lhs = assignment.getLeft();
JetIfExpression ifExpression = (JetIfExpression) assignment.getRight();
assertNotNull(ifExpression);
//noinspection ConstantConditions
JetIfExpression newIfExpression = (JetIfExpression) ifExpression.copy();
JetExpression thenExpr = getOutermostLastBlockElement(newIfExpression.getThen());
JetExpression elseExpr = getOutermostLastBlockElement(newIfExpression.getElse());
assertNotNull(thenExpr);
assertNotNull(elseExpr);
//noinspection ConstantConditions
JetPsiFactory psiFactory = JetPsiFactory(assignment);
thenExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, thenExpr));
elseExpr.replace(createExpressionByPattern(psiFactory, "$0 $1 $2", lhs, op, elseExpr));
PsiElement resultElement = assignment.replace(newIfExpression);
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
}
public static void unfoldAssignmentToWhen(@NotNull JetBinaryExpression assignment, @NotNull Editor editor) {
String op = assignment.getOperationReference().getText();
JetExpression lhs = assignment.getLeft();
JetWhenExpression whenExpression = (JetWhenExpression) assignment.getRight();
assertNotNull(whenExpression);
//noinspection ConstantConditions
JetWhenExpression newWhenExpression = (JetWhenExpression) whenExpression.copy();
for (JetWhenEntry entry : newWhenExpression.getEntries()) {
JetExpression currExpr = getOutermostLastBlockElement(entry.getExpression());
assertNotNull(currExpr);
//noinspection ConstantConditions
currExpr.replace(createExpressionByPattern(JetPsiFactory(assignment), "$0 $1 $2", lhs, op, currExpr));
}
PsiElement resultElement = assignment.replace(newWhenExpression);
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
}
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull Editor editor) {
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property);
unfoldAssignmentToIf(assignment, editor);
}
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull Editor editor) {
JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property);
unfoldAssignmentToWhen(assignment, editor);
}
public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
JetIfExpression ifExpression = (JetIfExpression) returnExpression.getReturnedExpression();
assertNotNull(ifExpression);
//noinspection ConstantConditions
JetIfExpression newIfExpression = (JetIfExpression) ifExpression.copy();
JetExpression thenExpr = getOutermostLastBlockElement(newIfExpression.getThen());
JetExpression elseExpr = getOutermostLastBlockElement(newIfExpression.getElse());
assertNotNull(thenExpr);
assertNotNull(elseExpr);
JetPsiFactory psiFactory = JetPsiFactory(returnExpression);
thenExpr.replace(createExpressionByPattern(psiFactory, "return $0", thenExpr));
elseExpr.replace(createExpressionByPattern(psiFactory, "return $0", elseExpr));
returnExpression.replace(newIfExpression);
}
public static void unfoldReturnToWhen(@NotNull JetReturnExpression returnExpression) {
JetWhenExpression whenExpression = (JetWhenExpression) returnExpression.getReturnedExpression();
assertNotNull(whenExpression);
//noinspection ConstantConditions
JetWhenExpression newWhenExpression = (JetWhenExpression) whenExpression.copy();
for (JetWhenEntry entry : newWhenExpression.getEntries()) {
JetExpression currExpr = getOutermostLastBlockElement(entry.getExpression());
assertNotNull(currExpr);
currExpr.replace(createExpressionByPattern(JetPsiFactory(returnExpression), "return $0", currExpr));
}
returnExpression.replace(newWhenExpression);
}
}
@@ -0,0 +1,171 @@
/*
* Copyright 2010-2015 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.kotlin.idea.intentions.branchedTransformations
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
import org.jetbrains.kotlin.psi.*
public object BranchedUnfoldingUtils {
private fun getOutermostLastBlockElement(expression: JetExpression?): JetExpression {
return JetPsiUtil.getOutermostLastBlockElement(expression, JetPsiUtil.ANY_JET_ELEMENT) as JetExpression
}
public fun getUnfoldableExpressionKind(root: JetExpression?): UnfoldableKind? {
if (root == null) return null
if (JetPsiUtil.isAssignment(root)) {
val assignment = root as JetBinaryExpression
if (assignment.getLeft() == null) return null
val rhs = assignment.getRight()
if (rhs is JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF
if (rhs is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(rhs)) {
return UnfoldableKind.ASSIGNMENT_TO_WHEN
}
}
else if (root is JetReturnExpression) {
val resultExpr = root.getReturnedExpression()
if (resultExpr is JetIfExpression) return UnfoldableKind.RETURN_TO_IF
if (resultExpr is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(resultExpr)) {
return UnfoldableKind.RETURN_TO_WHEN
}
}
else if (root is JetProperty) {
if (!root.isLocal()) return null
val initializer = root.getInitializer()
if (initializer is JetIfExpression) return UnfoldableKind.PROPERTY_TO_IF
if (initializer is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)) {
return UnfoldableKind.PROPERTY_TO_WHEN
}
}
return null
}
public val UNFOLD_WITHOUT_CHECK: String = "Expression must be checked before unfolding"
private fun assertNotNull(value: Any?) {
assert(value != null) { UNFOLD_WITHOUT_CHECK }
}
public fun unfoldAssignmentToIf(assignment: JetBinaryExpression, editor: Editor) {
val op = assignment.getOperationReference().getText()
val lhs = assignment.getLeft()
val ifExpression = assignment.getRight() as JetIfExpression
assertNotNull(ifExpression)
//noinspection ConstantConditions
val newIfExpression = ifExpression.copy() as JetIfExpression
val thenExpr = getOutermostLastBlockElement(newIfExpression.getThen())
val elseExpr = getOutermostLastBlockElement(newIfExpression.getElse())
assertNotNull(thenExpr)
assertNotNull(elseExpr)
//noinspection ConstantConditions
val psiFactory = JetPsiFactory(assignment)
thenExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", lhs, op, thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", lhs, op, elseExpr))
val resultElement = assignment.replace(newIfExpression)
editor.getCaretModel().moveToOffset(resultElement.getTextOffset())
}
public fun unfoldAssignmentToWhen(assignment: JetBinaryExpression, editor: Editor) {
val op = assignment.getOperationReference().getText()
val lhs = assignment.getLeft()
val whenExpression = assignment.getRight() as JetWhenExpression
assertNotNull(whenExpression)
//noinspection ConstantConditions
val newWhenExpression = whenExpression.copy() as JetWhenExpression
for (entry in newWhenExpression.getEntries()) {
val currExpr = getOutermostLastBlockElement(entry.getExpression())
assertNotNull(currExpr)
//noinspection ConstantConditions
currExpr.replace(JetPsiFactory(assignment).createExpressionByPattern("$0 $1 $2", lhs, op, currExpr))
}
val resultElement = assignment.replace(newWhenExpression)
editor.getCaretModel().moveToOffset(resultElement.getTextOffset())
}
public fun unfoldPropertyToIf(property: JetProperty, editor: Editor) {
val assignment = DeclarationUtils.splitPropertyDeclaration(property)
unfoldAssignmentToIf(assignment, editor)
}
public fun unfoldPropertyToWhen(property: JetProperty, editor: Editor) {
val assignment = DeclarationUtils.splitPropertyDeclaration(property)
unfoldAssignmentToWhen(assignment, editor)
}
public fun unfoldReturnToIf(returnExpression: JetReturnExpression) {
val ifExpression = returnExpression.getReturnedExpression() as JetIfExpression
assertNotNull(ifExpression)
//noinspection ConstantConditions
val newIfExpression = ifExpression.copy() as JetIfExpression
val thenExpr = getOutermostLastBlockElement(newIfExpression.getThen())
val elseExpr = getOutermostLastBlockElement(newIfExpression.getElse())
assertNotNull(thenExpr)
assertNotNull(elseExpr)
val psiFactory = JetPsiFactory(returnExpression)
thenExpr.replace(psiFactory.createExpressionByPattern("return $0", thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("return $0", elseExpr))
returnExpression.replace(newIfExpression)
}
public fun unfoldReturnToWhen(returnExpression: JetReturnExpression) {
val whenExpression = returnExpression.getReturnedExpression() as JetWhenExpression
assertNotNull(whenExpression)
//noinspection ConstantConditions
val newWhenExpression = whenExpression.copy() as JetWhenExpression
for (entry in newWhenExpression.getEntries()) {
val currExpr = getOutermostLastBlockElement(entry.getExpression())
assertNotNull(currExpr)
currExpr.replace(JetPsiFactory(returnExpression).createExpressionByPattern("return $0", currExpr))
}
returnExpression.replace(newWhenExpression)
}
}
@@ -28,37 +28,37 @@ public enum UnfoldableKind implements Transformer {
ASSIGNMENT_TO_IF("unfold.assignment.to.if") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldAssignmentToIf((JetBinaryExpression) element, editor);
BranchedUnfoldingUtils.INSTANCE$.unfoldAssignmentToIf((JetBinaryExpression) element, editor);
}
},
PROPERTY_TO_IF("unfold.property.to.if") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, editor);
BranchedUnfoldingUtils.INSTANCE$.unfoldPropertyToIf((JetProperty) element, editor);
}
},
RETURN_TO_IF("unfold.return.to.if") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldReturnToIf((JetReturnExpression) element);
BranchedUnfoldingUtils.INSTANCE$.unfoldReturnToIf((JetReturnExpression) element);
}
},
ASSIGNMENT_TO_WHEN("unfold.assignment.to.when") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldAssignmentToWhen((JetBinaryExpression) element, editor);
BranchedUnfoldingUtils.INSTANCE$.unfoldAssignmentToWhen((JetBinaryExpression) element, editor);
}
},
PROPERTY_TO_WHEN("unfold.property.to.when") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, editor);
BranchedUnfoldingUtils.INSTANCE$.unfoldPropertyToWhen((JetProperty) element, editor);
}
},
RETURN_TO_WHEN("unfold.return.to.when") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
BranchedUnfoldingUtils.unfoldReturnToWhen((JetReturnExpression) element);
BranchedUnfoldingUtils.INSTANCE$.unfoldReturnToWhen((JetReturnExpression) element);
}
};