diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/BranchedUnfoldingUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/BranchedUnfoldingUtils.java index 61ca1c961bf..a3fccc42a96 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/BranchedUnfoldingUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/BranchedUnfoldingUtils.java @@ -22,13 +22,7 @@ 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; +import org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations.DeclarationUtils; public class BranchedUnfoldingUtils { private BranchedUnfoldingUtils() { @@ -133,91 +127,14 @@ 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()); + public static void unfoldPropertyToIf(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) { + JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file); + unfoldAssignmentToIf(assignment, editor); } - protected interface PropertyUnfolder { - void processInitializer(@NotNull T newInitializer, @NotNull JetExpression propertyRef, @NotNull Project project); - } - - protected static final PropertyUnfolder IF_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder() { - @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 WHEN_EXPRESSION_PROPERTY_UNFOLDER = new PropertyUnfolder() { - @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 void unfoldProperty( - @NotNull JetProperty property, @NotNull JetFile file, PropertyUnfolder 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 unfoldPropertyToWhen(@NotNull JetProperty property, @NotNull JetFile file, @NotNull Editor editor) { + JetBinaryExpression assignment = DeclarationUtils.splitPropertyDeclaration(property, file); + unfoldAssignmentToWhen(assignment, editor); } public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) { diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldableKind.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldableKind.java index 4cc9162e237..bb14756bafb 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldableKind.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldableKind.java @@ -35,7 +35,7 @@ public enum UnfoldableKind implements Transformer { PROPERTY_TO_IF("unfold.property.to.if") { @Override public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) { - BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, file); + BranchedUnfoldingUtils.unfoldPropertyToIf((JetProperty) element, file, editor); } }, RETURN_TO_IF("unfold.return.to.if") { @@ -53,7 +53,7 @@ public enum UnfoldableKind implements Transformer { PROPERTY_TO_WHEN("unfold.property.to.when") { @Override public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) { - BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, file); + BranchedUnfoldingUtils.unfoldPropertyToWhen((JetProperty) element, file, editor); } }, RETURN_TO_WHEN("unfold.return.to.when") { diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs.kt.after index c0344100f46..5b1471d1878 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String? { - val res: String? - if (n == 1) { + val res: String? + if (n == 1) { res = if (3 > 2) { println("***") "one" diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs2.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs2.kt.after index f560c0b982b..a8fb5588cc6 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs2.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/nestedIfs2.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String? { - var res: String? - if (n == 1) { + var res: String? + if (n == 1) { res = if (3 > 2) { println("***") "one" diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf.kt.after index 7025c616a42..ee420221776 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - val res: String - if (n == 1) res = "one" else res = "two" + val res: String + if (n == 1) res = "one" else res = "two" return res } \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf2.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf2.kt.after index 7e2a0315bd6..43088f87ac5 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf2.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIf2.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - var res: String - if (n == 1) res = "one" else res = "two" + var res: String + if (n == 1) res = "one" else res = "two" return res } \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks.kt.after index d1471a6142e..953ab6c8863 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - val res: String - if (n == 1) { + val res: String + if (n == 1) { println("***") res = "one" } else { diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks2.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks2.kt.after index c16a32371c6..705e0ce8b89 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks2.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithBlocks2.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - var res: String - if (n == 1) { + var res: String + if (n == 1) { println("***") res = "one" } else { diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithType.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithType.kt.after index 3460d3bda79..b9b19d16ac9 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithType.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToIf/simpleIfWithType.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - val res: jet.String - if (n == 1) res = "one" else res = "two" + val res: jet.String + if (n == 1) res = "one" else res = "two" return res } \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen.kt.after index d0be88b8b7f..c7e7082f1ca 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String? { - val res: String? - when(n) { + val res: String? + when(n) { 1 -> res = "one" 2 -> res = "two" else -> res = null diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen2.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen2.kt.after index a0dd0c4c2c8..b4971e0feae 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen2.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhen2.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String? { - var res: String? - when(n) { + var res: String? + when(n) { 1 -> res = "one" 2 -> res = "two" else -> res = null diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks.kt.after index b900cde5fc2..b16b90b3c81 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - val res: String - when (n) { + val res: String + when (n) { 1 -> { println("***") res = "one" diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks2.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks2.kt.after index 4487e97042d..74f9623e053 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks2.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithBlocks2.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String { - var res: String - when (n) { + var res: String + when (n) { 1 -> { println("***") res = "one" diff --git a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithType.kt.after b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithType.kt.after index 7bf0f8006d3..994e41451e5 100644 --- a/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithType.kt.after +++ b/idea/testData/codeInsight/codeTransformations/branched/unfolding/propertyToWhen/simpleWhenWithType.kt.after @@ -1,6 +1,6 @@ fun test(n: Int): String? { - val res: jet.String? - when(n) { + val res: jet.String? + when(n) { 1 -> res = "one" 2 -> res = "two" else -> res = null