Add code transformations for if/when in local property initializers
This commit is contained in:
@@ -743,6 +743,11 @@ public class JetPsiUtil {
|
||||
return element != null ? element.getText() : "";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getNullableText(@Nullable PsiElement element) {
|
||||
return element != null ? element.getText() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* CommentUtilCore.isComment fails if element <strong>inside</strong> comment.
|
||||
*
|
||||
|
||||
@@ -330,6 +330,8 @@ public class GenerateTests {
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/folding/whenToReturn", "doTestFoldWhenToReturn"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignmentToIf", "doTestUnfoldAssignmentToIf"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignmentToWhen", "doTestUnfoldAssignmentToWhen"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf", "doTestUnfoldPropertyToIf"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen", "doTestUnfoldPropertyToWhen"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/returnToIf", "doTestUnfoldReturnToIf"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/returnToWhen", "doTestUnfoldReturnToWhen"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/ifWhen/ifToWhen", "doTestIfToWhen"),
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
val res: String
|
||||
if (ok) {
|
||||
res = "ok"
|
||||
} else {
|
||||
res = "failed"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
val res = if (ok) {
|
||||
"ok"
|
||||
} else {
|
||||
"failed"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts property with 'if' initializer to uninitialized property followed by 'if' expression where each branch is terminated with assignment
|
||||
</body>
|
||||
</html>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
val res: String
|
||||
when (n) {
|
||||
1 -> res = "one"
|
||||
2 -> res = "two"
|
||||
else -> res = "many"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
val res = when (n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> "many"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts property with 'when' initializer to uninitialized property followed by 'when' expression where each branch is terminated with assignment
|
||||
</body>
|
||||
</html>
|
||||
@@ -331,11 +331,21 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldPropertyToIfIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldAssignmentToWhenIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldPropertyToWhenIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldReturnToIfIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -171,12 +171,16 @@ fold.when.to.call=Replace 'when' expression with method call
|
||||
fold.when.to.call.family=Replace 'when' Expression with Method Call
|
||||
unfold.assignment.to.if=Replace assignment with 'if' expression
|
||||
unfold.assignment.to.if.family=Replace Assignment with 'if' Expression
|
||||
unfold.property.to.if=Replace property initializer with 'if' expression
|
||||
unfold.property.to.if.family=Replace Property Initializer with 'if' Expression
|
||||
unfold.return.to.if=Replace return with 'if' expression
|
||||
unfold.return.to.if.family=Replace Return with 'if' Expression
|
||||
unfold.call.to.if=Replace method call with 'if' expression
|
||||
unfold.call.to.if.family=Replace Method Call with 'if' Expression
|
||||
unfold.assignment.to.when=Replace assignment with 'when' expression
|
||||
unfold.assignment.to.when.family=Replace Assignment with 'when' Expression
|
||||
unfold.property.to.when=Replace property initializer with 'when' expression
|
||||
unfold.property.to.when.family=Replace Property Initializer with 'when' Expression
|
||||
unfold.return.to.when=Replace return with 'when' expression
|
||||
unfold.return.to.when.family=Replace Return with 'when' Expression
|
||||
unfold.call.to.when=Replace method call with 'when' expression
|
||||
|
||||
+115
-9
@@ -22,6 +22,13 @@ import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class BranchedUnfoldingUtils {
|
||||
private BranchedUnfoldingUtils() {
|
||||
@@ -36,7 +43,7 @@ public class BranchedUnfoldingUtils {
|
||||
if (root == null) return null;
|
||||
|
||||
if (JetPsiUtil.isAssignment(root)) {
|
||||
JetBinaryExpression assignment = (JetBinaryExpression)root;
|
||||
JetBinaryExpression assignment = (JetBinaryExpression) root;
|
||||
|
||||
assertNotNull(assignment.getLeft());
|
||||
|
||||
@@ -45,29 +52,41 @@ public class BranchedUnfoldingUtils {
|
||||
if (rhs instanceof JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse((JetWhenExpression) rhs)) {
|
||||
return UnfoldableKind.ASSIGNMENT_TO_WHEN;
|
||||
}
|
||||
} else if (root instanceof JetReturnExpression) {
|
||||
JetExpression resultExpr = ((JetReturnExpression)root).getReturnedExpression();
|
||||
}
|
||||
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(JetExpression expression) {
|
||||
assert expression != null : UNFOLD_WITHOUT_CHECK;
|
||||
private static void assertNotNull(Object value) {
|
||||
assert value != null : UNFOLD_WITHOUT_CHECK;
|
||||
}
|
||||
|
||||
public static void unfoldAssignmentToIf(@NotNull JetBinaryExpression assignment, @NotNull Editor editor) {
|
||||
Project project = assignment.getProject();
|
||||
String op = assignment.getOperationReference().getText();
|
||||
JetExpression lhs = assignment.getLeft();
|
||||
JetIfExpression ifExpression = (JetIfExpression)assignment.getRight();
|
||||
JetIfExpression ifExpression = (JetIfExpression) assignment.getRight();
|
||||
|
||||
assertNotNull(ifExpression);
|
||||
|
||||
@@ -93,7 +112,7 @@ public class BranchedUnfoldingUtils {
|
||||
Project project = assignment.getProject();
|
||||
String op = assignment.getOperationReference().getText();
|
||||
JetExpression lhs = assignment.getLeft();
|
||||
JetWhenExpression whenExpression = (JetWhenExpression)assignment.getRight();
|
||||
JetWhenExpression whenExpression = (JetWhenExpression) assignment.getRight();
|
||||
|
||||
assertNotNull(whenExpression);
|
||||
|
||||
@@ -114,9 +133,96 @@ public class BranchedUnfoldingUtils {
|
||||
editor.getCaretModel().moveToOffset(resultElement.getTextOffset());
|
||||
}
|
||||
|
||||
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property, @NotNull JetFile file) {
|
||||
if (property.getTypeRef() != null) return null;
|
||||
return AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext().get(BindingContext.EXPRESSION_TYPE, property.getInitializer());
|
||||
}
|
||||
|
||||
protected interface PropertyUnfolder<T extends JetExpression> {
|
||||
void processInitializer(@NotNull T newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project);
|
||||
}
|
||||
|
||||
protected static final PropertyUnfolder<JetIfExpression> IF_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder<JetIfExpression>() {
|
||||
@Override
|
||||
public void processInitializer(
|
||||
@NotNull JetIfExpression newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project
|
||||
) {
|
||||
JetExpression thenExpr = getOutermostLastBlockElement(newInitializer.getThen());
|
||||
JetExpression elseExpr = getOutermostLastBlockElement(newInitializer.getElse());
|
||||
|
||||
assertNotNull(thenExpr);
|
||||
assertNotNull(elseExpr);
|
||||
|
||||
thenExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", thenExpr));
|
||||
elseExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", elseExpr));
|
||||
}
|
||||
};
|
||||
|
||||
protected static final PropertyUnfolder<JetWhenExpression> WHEN_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder<JetWhenExpression>() {
|
||||
@Override
|
||||
public void processInitializer(
|
||||
@NotNull JetWhenExpression newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project
|
||||
) {
|
||||
for (JetWhenEntry entry : newInitializer.getEntries()) {
|
||||
JetExpression currExpr = getOutermostLastBlockElement(entry.getExpression());
|
||||
|
||||
assertNotNull(currExpr);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
currExpr.replace(JetPsiFactory.createBinaryExpression(project, propertyRef, "=", currExpr));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static <T extends JetExpression> void unfoldProperty(
|
||||
@NotNull JetProperty property, @NotNull JetFile file, PropertyUnfolder<T> unfolder
|
||||
) {
|
||||
Project project = property.getProject();
|
||||
|
||||
PsiElement parent = property.getParent();
|
||||
assertNotNull(parent);
|
||||
|
||||
//noinspection unchecked
|
||||
T initializer = (T) property.getInitializer();
|
||||
assertNotNull(initializer);
|
||||
|
||||
JetSimpleNameExpression propertyName = JetPsiFactory.createSimpleName(project, property.getName());
|
||||
|
||||
//noinspection ConstantConditions, unchecked
|
||||
T newInitializer = (T) initializer.copy();
|
||||
|
||||
unfolder.processInitializer(newInitializer, propertyName, project);
|
||||
|
||||
parent.addAfter(newInitializer, property);
|
||||
parent.addAfter(JetPsiFactory.createNewLine(project), property);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
JetType inferredType = getPropertyTypeIfNeeded(property, file);
|
||||
|
||||
String typeStr = inferredType != null
|
||||
? DescriptorRenderer.TEXT.renderType(inferredType)
|
||||
: JetPsiUtil.getNullableText(property.getTypeRef());
|
||||
|
||||
property = (JetProperty) property.replace(
|
||||
JetPsiFactory.createProperty(project, property.getName(), typeStr, property.isVar())
|
||||
);
|
||||
|
||||
if (inferredType != null) {
|
||||
ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(property.getTypeRef()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull JetFile file) {
|
||||
unfoldProperty(property, file, IF_EXPRESSION_PROPERTY_UNFOLDER);
|
||||
}
|
||||
|
||||
public static void unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull JetFile file) {
|
||||
unfoldProperty(property, file, WHEN_EXPRESSION_PROPERTY_UNFOLDER);
|
||||
}
|
||||
|
||||
public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
|
||||
Project project = returnExpression.getProject();
|
||||
JetIfExpression ifExpression = (JetIfExpression)returnExpression.getReturnedExpression();
|
||||
JetIfExpression ifExpression = (JetIfExpression) returnExpression.getReturnedExpression();
|
||||
|
||||
assertNotNull(ifExpression);
|
||||
|
||||
@@ -137,7 +243,7 @@ public class BranchedUnfoldingUtils {
|
||||
|
||||
public static void unfoldReturnToWhen(@NotNull JetReturnExpression returnExpression) {
|
||||
Project project = returnExpression.getProject();
|
||||
JetWhenExpression whenExpression = (JetWhenExpression)returnExpression.getReturnedExpression();
|
||||
JetWhenExpression whenExpression = (JetWhenExpression) returnExpression.getReturnedExpression();
|
||||
|
||||
assertNotNull(whenExpression);
|
||||
|
||||
|
||||
+13
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetProperty;
|
||||
import org.jetbrains.jet.lang.psi.JetReturnExpression;
|
||||
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.core.Transformer;
|
||||
|
||||
@@ -31,6 +32,12 @@ public enum UnfoldableKind implements Transformer {
|
||||
BranchedUnfoldingUtils.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, file);
|
||||
}
|
||||
},
|
||||
RETURN_TO_IF("unfold.return.to.if") {
|
||||
@Override
|
||||
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
||||
@@ -43,6 +50,12 @@ public enum UnfoldableKind implements Transformer {
|
||||
BranchedUnfoldingUtils.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, file);
|
||||
}
|
||||
},
|
||||
RETURN_TO_WHEN("unfold.return.to.when") {
|
||||
@Override
|
||||
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
|
||||
|
||||
+12
@@ -42,12 +42,24 @@ public abstract class UnfoldBranchedExpressionIntention extends AbstractCodeTran
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnfoldPropertyToIfIntention extends UnfoldBranchedExpressionIntention {
|
||||
public UnfoldPropertyToIfIntention() {
|
||||
super(UnfoldableKind.PROPERTY_TO_IF);
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnfoldAssignmentToWhenIntention extends UnfoldBranchedExpressionIntention {
|
||||
public UnfoldAssignmentToWhenIntention() {
|
||||
super(UnfoldableKind.ASSIGNMENT_TO_WHEN);
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnfoldPropertyToWhenIntention extends UnfoldBranchedExpressionIntention {
|
||||
public UnfoldPropertyToWhenIntention() {
|
||||
super(UnfoldableKind.PROPERTY_TO_WHEN);
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnfoldReturnToIfIntention extends UnfoldBranchedExpressionIntention {
|
||||
public UnfoldReturnToIfIntention() {
|
||||
super(UnfoldableKind.RETURN_TO_IF);
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
fun test(n: Int): String? {
|
||||
val res = <caret>if (n == 1) {
|
||||
if (3 > 2) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"???"
|
||||
}
|
||||
} else if (n == 2) {
|
||||
println("***")
|
||||
null
|
||||
} else {
|
||||
println("***")
|
||||
"too many"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
fun test(n: Int): String? {
|
||||
val res:<caret> String?
|
||||
if (n == 1) {
|
||||
res = if (3 > 2) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"???"
|
||||
}
|
||||
} else res = if (n == 2) {
|
||||
println("***")
|
||||
null
|
||||
} else {
|
||||
println("***")
|
||||
"too many"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
fun test(n: Int): String? {
|
||||
var res = <caret>if (n == 1) {
|
||||
if (3 > 2) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"???"
|
||||
}
|
||||
} else if (n == 2) {
|
||||
println("***")
|
||||
null
|
||||
} else {
|
||||
println("***")
|
||||
"too many"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
fun test(n: Int): String? {
|
||||
var res:<caret> String?
|
||||
if (n == 1) {
|
||||
res = if (3 > 2) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"???"
|
||||
}
|
||||
} else res = if (n == 2) {
|
||||
println("***")
|
||||
null
|
||||
} else {
|
||||
println("***")
|
||||
"too many"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
val<caret> x = if (false) "0" else "1"
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
var<caret> x = if (false) "0" else "1"
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun test(n: Int): String {
|
||||
val <caret>res = if (n == 1) "one" else "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
val <caret>res: String
|
||||
if (n == 1) res = "one" else res = "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun test(n: Int): String {
|
||||
var <caret>res = if (n == 1) "one" else "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
var <caret>res: String
|
||||
if (n == 1) res = "one" else res = "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun test(n: Int): String {
|
||||
val res<caret> = if (n == 1) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"two"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fun test(n: Int): String {
|
||||
val res<caret>: String
|
||||
if (n == 1) {
|
||||
println("***")
|
||||
res = "one"
|
||||
} else {
|
||||
println("***")
|
||||
res = "two"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun test(n: Int): String {
|
||||
var res<caret> = if (n == 1) {
|
||||
println("***")
|
||||
"one"
|
||||
} else {
|
||||
println("***")
|
||||
"two"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fun test(n: Int): String {
|
||||
var res<caret>: String
|
||||
if (n == 1) {
|
||||
println("***")
|
||||
res = "one"
|
||||
} else {
|
||||
println("***")
|
||||
res = "two"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun test(n: Int): String {
|
||||
val <caret>res: jet.String = if (n == 1) "one" else "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
val <caret>res: jet.String
|
||||
if (n == 1) res = "one" else res = "two"
|
||||
|
||||
return res
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: false
|
||||
val n = 10
|
||||
|
||||
val res<caret> = when(n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> null
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: false
|
||||
val n = 10
|
||||
|
||||
var res<caret> = when(n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> null
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun test(n: Int): String? {
|
||||
val res<caret> = when(n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fun test(n: Int): String? {
|
||||
val res<caret>: String?
|
||||
when(n) {
|
||||
1 -> res = "one"
|
||||
2 -> res = "two"
|
||||
else -> res = null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun test(n: Int): String? {
|
||||
var res<caret> = when(n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fun test(n: Int): String? {
|
||||
var res<caret>: String?
|
||||
when(n) {
|
||||
1 -> res = "one"
|
||||
2 -> res = "two"
|
||||
else -> res = null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
fun test(n: Int): String {
|
||||
val res<caret> = when (n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
"one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
"two"
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
fun test(n: Int): String {
|
||||
val res<caret>: String
|
||||
when (n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
res = "one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
res = "two"
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
fun test(n: Int): String {
|
||||
var res<caret> = when (n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
"one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
"two"
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
fun test(n: Int): String {
|
||||
var res<caret>: String
|
||||
when (n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
res = "one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
res = "two"
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun test(n: Int): String? {
|
||||
val res<caret>: jet.String? = when(n) {
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fun test(n: Int): String? {
|
||||
val res<caret>: jet.String?
|
||||
when(n) {
|
||||
1 -> res = "one"
|
||||
2 -> res = "two"
|
||||
else -> res = null
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+8
@@ -54,6 +54,14 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldPropertyToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToIfIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldPropertyToWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldReturnToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToIfIntention());
|
||||
}
|
||||
|
||||
+99
-1
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTran
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@InnerTestClasses({CodeTransformationsTestGenerated.IfToAssignment.class, CodeTransformationsTestGenerated.IfToReturn.class, CodeTransformationsTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationsTestGenerated.WhenToAssignment.class, CodeTransformationsTestGenerated.WhenToReturn.class, CodeTransformationsTestGenerated.AssignmentToIf.class, CodeTransformationsTestGenerated.AssignmentToWhen.class, CodeTransformationsTestGenerated.ReturnToIf.class, CodeTransformationsTestGenerated.ReturnToWhen.class, CodeTransformationsTestGenerated.IfToWhen.class, CodeTransformationsTestGenerated.WhenToIf.class, CodeTransformationsTestGenerated.Flatten.class, CodeTransformationsTestGenerated.IntroduceSubject.class, CodeTransformationsTestGenerated.EliminateSubject.class})
|
||||
@InnerTestClasses({CodeTransformationsTestGenerated.IfToAssignment.class, CodeTransformationsTestGenerated.IfToReturn.class, CodeTransformationsTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationsTestGenerated.WhenToAssignment.class, CodeTransformationsTestGenerated.WhenToReturn.class, CodeTransformationsTestGenerated.AssignmentToIf.class, CodeTransformationsTestGenerated.AssignmentToWhen.class, CodeTransformationsTestGenerated.PropertyToIf.class, CodeTransformationsTestGenerated.PropertyToWhen.class, CodeTransformationsTestGenerated.ReturnToIf.class, CodeTransformationsTestGenerated.ReturnToWhen.class, CodeTransformationsTestGenerated.IfToWhen.class, CodeTransformationsTestGenerated.WhenToIf.class, CodeTransformationsTestGenerated.Flatten.class, CodeTransformationsTestGenerated.IntroduceSubject.class, CodeTransformationsTestGenerated.EliminateSubject.class})
|
||||
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding/ifToAssignment")
|
||||
public static class IfToAssignment extends AbstractCodeTransformationTest {
|
||||
@@ -263,6 +263,102 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf")
|
||||
public static class PropertyToIf extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInPropertyToIf() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("nestedIfs.kt")
|
||||
public void testNestedIfs() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nestedIfs2.kt")
|
||||
public void testNestedIfs2() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty.kt")
|
||||
public void testNonLocalProperty() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nonLocalProperty.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty2.kt")
|
||||
public void testNonLocalProperty2() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nonLocalProperty2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleIf.kt")
|
||||
public void testSimpleIf() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleIf2.kt")
|
||||
public void testSimpleIf2() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleIfWithBlocks.kt")
|
||||
public void testSimpleIfWithBlocks() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleIfWithBlocks2.kt")
|
||||
public void testSimpleIfWithBlocks2() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleIfWithType.kt")
|
||||
public void testSimpleIfWithType() throws Exception {
|
||||
doTestUnfoldPropertyToIf("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithType.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen")
|
||||
public static class PropertyToWhen extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInPropertyToWhen() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty.kt")
|
||||
public void testNonLocalProperty() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/nonLocalProperty.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty2.kt")
|
||||
public void testNonLocalProperty2() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/nonLocalProperty2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleWhen.kt")
|
||||
public void testSimpleWhen() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleWhen2.kt")
|
||||
public void testSimpleWhen2() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleWhenWithBlocks.kt")
|
||||
public void testSimpleWhenWithBlocks() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleWhenWithBlocks2.kt")
|
||||
public void testSimpleWhenWithBlocks2() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleWhenWithType.kt")
|
||||
public void testSimpleWhenWithType() throws Exception {
|
||||
doTestUnfoldPropertyToWhen("idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithType.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding/returnToIf")
|
||||
public static class ReturnToIf extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInReturnToIf() throws Exception {
|
||||
@@ -553,6 +649,8 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
||||
suite.addTestSuite(WhenToReturn.class);
|
||||
suite.addTestSuite(AssignmentToIf.class);
|
||||
suite.addTestSuite(AssignmentToWhen.class);
|
||||
suite.addTestSuite(PropertyToIf.class);
|
||||
suite.addTestSuite(PropertyToWhen.class);
|
||||
suite.addTestSuite(ReturnToIf.class);
|
||||
suite.addTestSuite(ReturnToWhen.class);
|
||||
suite.addTestSuite(IfToWhen.class);
|
||||
|
||||
Reference in New Issue
Block a user