From 832b4fb8d82105e87fd877e72b8c9a833dff8e3f Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 9 May 2015 11:24:06 +0300 Subject: [PATCH] Refactored FoldBranchedExpressionIntention removing common subclass and FoldableKind --- .../kotlin/idea/JetBundle.properties | 8 --- .../BranchedFoldingUtils.kt | 26 ++----- .../branchedTransformations/FoldableKind.java | 69 ------------------- .../FoldBranchedExpressionIntention.kt | 62 +++++++++++------ 4 files changed, 47 insertions(+), 118 deletions(-) delete mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/FoldableKind.java diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index 9e5a8ec556b..cc996b01e77 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -212,16 +212,8 @@ surround.with.cannot.perform.action=Cannot perform Surround With action to the c remove.variable.family.name=Remove variable remove.variable.action=Remove variable ''{0}'' kotlin.code.transformations=Kotlin Code Transformations -fold.if.to.assignment=Replace 'if' expression with assignment -fold.if.to.assignment.family=Replace 'if' Expression with Assignment -fold.if.to.return=Replace 'if' expression with return -fold.if.to.return.family=Replace 'if' Expression with Return fold.if.to.call=Replace 'if' expression with method call fold.if.to.call.family=Replace 'if' Expression with Method Call -fold.when.to.assignment=Replace 'when' expression with assignment -fold.when.to.assignment.family=Replace 'when' Expression with Assignment -fold.when.to.return=Replace 'when' expression with return -fold.when.to.return.family=Replace 'when' Expression with Return 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 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt index 4f3d0973483..f4b64702f7f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt @@ -55,7 +55,7 @@ public object BranchedFoldingUtils { return a1.getLeft()?.getText() == a2.getLeft()?.getText() && a1.getOperationToken() == a2.getOperationToken() } - private fun checkFoldableIfExpressionWithAssignments(ifExpression: JetIfExpression): Boolean { + public fun checkFoldableIfExpressionWithAssignments(ifExpression: JetIfExpression): Boolean { val thenBranch = ifExpression.getThen() val elseBranch = ifExpression.getElse() @@ -67,7 +67,7 @@ public object BranchedFoldingUtils { return checkAssignmentsMatch(thenAssignment, elseAssignment) } - private fun checkFoldableWhenExpressionWithAssignments(whenExpression: JetWhenExpression): Boolean { + public fun checkFoldableWhenExpressionWithAssignments(whenExpression: JetWhenExpression): Boolean { if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false val entries = whenExpression.getEntries() @@ -90,11 +90,11 @@ public object BranchedFoldingUtils { return true } - private fun checkFoldableIfExpressionWithReturns(ifExpression: JetIfExpression): Boolean { + public fun checkFoldableIfExpressionWithReturns(ifExpression: JetIfExpression): Boolean { return getFoldableBranchedReturn(ifExpression.getThen()) != null && getFoldableBranchedReturn(ifExpression.getElse()) != null } - private fun checkFoldableWhenExpressionWithReturns(whenExpression: JetWhenExpression): Boolean { + public fun checkFoldableWhenExpressionWithReturns(whenExpression: JetWhenExpression): Boolean { if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false val entries = whenExpression.getEntries() @@ -108,7 +108,7 @@ public object BranchedFoldingUtils { return true } - private fun checkFoldableIfExpressionWithAsymmetricReturns(ifExpression: JetIfExpression): Boolean { + public fun checkFoldableIfExpressionWithAsymmetricReturns(ifExpression: JetIfExpression): Boolean { if (getFoldableBranchedReturn(ifExpression.getThen()) == null || ifExpression.getElse() != null) { return false } @@ -117,22 +117,6 @@ public object BranchedFoldingUtils { return (nextElement is JetExpression) && getFoldableBranchedReturn(nextElement) != null } - public fun getFoldableExpressionKind(root: JetExpression?): FoldableKind? { - if (root is JetIfExpression) { - - if (checkFoldableIfExpressionWithAssignments(root)) return FoldableKind.IF_TO_ASSIGNMENT - if (checkFoldableIfExpressionWithReturns(root)) return FoldableKind.IF_TO_RETURN - if (checkFoldableIfExpressionWithAsymmetricReturns(root)) return FoldableKind.IF_TO_RETURN_ASYMMETRICALLY - } - else if (root is JetWhenExpression) { - - if (checkFoldableWhenExpressionWithAssignments(root)) return FoldableKind.WHEN_TO_ASSIGNMENT - if (checkFoldableWhenExpressionWithReturns(root)) return FoldableKind.WHEN_TO_RETURN - } - - return null - } - public fun foldIfExpressionWithAssignments(ifExpression: JetIfExpression) { var thenAssignment = getFoldableBranchedAssignment(ifExpression.getThen()!!)!! diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/FoldableKind.java b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/FoldableKind.java deleted file mode 100644 index 9a7750511fc..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/FoldableKind.java +++ /dev/null @@ -1,69 +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.kotlin.psi.JetFile; -import org.jetbrains.kotlin.psi.JetIfExpression; -import org.jetbrains.kotlin.psi.JetWhenExpression; - -public enum FoldableKind implements Transformer { - IF_TO_ASSIGNMENT("fold.if.to.assignment") { - @Override - public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) { - BranchedFoldingUtils.INSTANCE$.foldIfExpressionWithAssignments((JetIfExpression) element); - } - }, - IF_TO_RETURN("fold.if.to.return") { - @Override - public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) { - BranchedFoldingUtils.INSTANCE$.foldIfExpressionWithReturns((JetIfExpression) element); - } - }, - IF_TO_RETURN_ASYMMETRICALLY("fold.if.to.return") { - @Override - public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) { - BranchedFoldingUtils.INSTANCE$.foldIfExpressionWithAsymmetricReturns((JetIfExpression) element); - } - }, - WHEN_TO_ASSIGNMENT("fold.when.to.assignment") { - @Override - public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) { - BranchedFoldingUtils.INSTANCE$.foldWhenExpressionWithAssignments((JetWhenExpression) element); - } - }, - WHEN_TO_RETURN("fold.when.to.return") { - @Override - public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) { - BranchedFoldingUtils.INSTANCE$.foldWhenExpressionWithReturns((JetWhenExpression) element); - } - }; - - private final String key; - - private FoldableKind(String key) { - this.key = key; - } - - @NotNull - @Override - public String getKey() { - return key; - } -} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldBranchedExpressionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldBranchedExpressionIntention.kt index 2c17c0cfa03..bdf54be1e81 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldBranchedExpressionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldBranchedExpressionIntention.kt @@ -19,35 +19,57 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.FoldableKind import org.jetbrains.kotlin.psi.JetExpression import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetIfExpression import org.jetbrains.kotlin.psi.JetWhenExpression -public open class FoldBranchedExpressionIntention( - val kind: FoldableKind, elementType: Class -) : JetSelfTargetingOffsetIndependentIntention(kind.getKey(), elementType) { - override fun isApplicableTo(element: T): Boolean = BranchedFoldingUtils.getFoldableExpressionKind(element) == kind +public class FoldIfToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'if' expression with assignment") { + override fun isApplicableTo(element: JetIfExpression): Boolean { + return BranchedFoldingUtils.checkFoldableIfExpressionWithAssignments(element) + } - override fun applyTo(element: T, editor: Editor) { - val file = element.getContainingFile() - if (file is JetFile) { - kind.transform(element, editor, file) - } + override fun applyTo(element: JetIfExpression, editor: Editor) { + BranchedFoldingUtils.foldIfExpressionWithAssignments(element) } } + +public class FoldIfToReturnAsymmetricallyIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'if' expression with return") { + override fun isApplicableTo(element: JetIfExpression): Boolean { + return BranchedFoldingUtils.checkFoldableIfExpressionWithAsymmetricReturns(element) + } -public class FoldIfToAssignmentIntention : FoldBranchedExpressionIntention(FoldableKind.IF_TO_ASSIGNMENT, javaClass()) + override fun applyTo(element: JetIfExpression, editor: Editor) { + BranchedFoldingUtils.foldIfExpressionWithAsymmetricReturns(element) + } +} -public class FoldIfToReturnAsymmetricallyIntention : FoldBranchedExpressionIntention( - FoldableKind.IF_TO_RETURN_ASYMMETRICALLY, javaClass() -) +public class FoldIfToReturnIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'if' expression with return") { + override fun isApplicableTo(element: JetIfExpression): Boolean { + return BranchedFoldingUtils.checkFoldableIfExpressionWithReturns(element) + } + + override fun applyTo(element: JetIfExpression, editor: Editor) { + BranchedFoldingUtils.foldIfExpressionWithReturns(element) + } +} -public class FoldIfToReturnIntention : FoldBranchedExpressionIntention(FoldableKind.IF_TO_RETURN, javaClass()) +public class FoldWhenToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'when' expression with assignment") { + override fun isApplicableTo(element: JetWhenExpression): Boolean { + return BranchedFoldingUtils.checkFoldableWhenExpressionWithAssignments(element) + } + + override fun applyTo(element: JetWhenExpression, editor: Editor) { + BranchedFoldingUtils.foldWhenExpressionWithAssignments(element) + } +} -public class FoldWhenToAssignmentIntention : FoldBranchedExpressionIntention( - FoldableKind.WHEN_TO_ASSIGNMENT, javaClass() -) - -public class FoldWhenToReturnIntention : FoldBranchedExpressionIntention(FoldableKind.WHEN_TO_RETURN, javaClass()) +public class FoldWhenToReturnIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'when' expression with return") { + override fun isApplicableTo(element: JetWhenExpression): Boolean { + return BranchedFoldingUtils.checkFoldableWhenExpressionWithReturns(element) + } + + override fun applyTo(element: JetWhenExpression, editor: Editor) { + BranchedFoldingUtils.foldWhenExpressionWithReturns(element) + } +} \ No newline at end of file