Refactored FoldBranchedExpressionIntention removing common subclass and FoldableKind
This commit is contained in:
@@ -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
|
||||
|
||||
+5
-21
@@ -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()!!)!!
|
||||
|
||||
|
||||
-69
@@ -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;
|
||||
}
|
||||
}
|
||||
+42
-20
@@ -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<T: JetExpression>(
|
||||
val kind: FoldableKind, elementType: Class<T>
|
||||
) : JetSelfTargetingOffsetIndependentIntention<T>(kind.getKey(), elementType) {
|
||||
override fun isApplicableTo(element: T): Boolean = BranchedFoldingUtils.getFoldableExpressionKind(element) == kind
|
||||
public class FoldIfToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(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<JetIfExpression>(javaClass(), "Replace 'if' expression with return") {
|
||||
override fun isApplicableTo(element: JetIfExpression): Boolean {
|
||||
return BranchedFoldingUtils.checkFoldableIfExpressionWithAsymmetricReturns(element)
|
||||
}
|
||||
|
||||
public class FoldIfToAssignmentIntention : FoldBranchedExpressionIntention<JetIfExpression>(FoldableKind.IF_TO_ASSIGNMENT, javaClass())
|
||||
override fun applyTo(element: JetIfExpression, editor: Editor) {
|
||||
BranchedFoldingUtils.foldIfExpressionWithAsymmetricReturns(element)
|
||||
}
|
||||
}
|
||||
|
||||
public class FoldIfToReturnAsymmetricallyIntention : FoldBranchedExpressionIntention<JetIfExpression>(
|
||||
FoldableKind.IF_TO_RETURN_ASYMMETRICALLY, javaClass()
|
||||
)
|
||||
public class FoldIfToReturnIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(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<JetIfExpression>(FoldableKind.IF_TO_RETURN, javaClass())
|
||||
public class FoldWhenToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>(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<JetWhenExpression>(
|
||||
FoldableKind.WHEN_TO_ASSIGNMENT, javaClass()
|
||||
)
|
||||
|
||||
public class FoldWhenToReturnIntention : FoldBranchedExpressionIntention<JetWhenExpression>(FoldableKind.WHEN_TO_RETURN, javaClass())
|
||||
public class FoldWhenToReturnIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>(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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user