Refactored UnfoldBranchedExpressionIntention's getting rid of UnfoldableKind

This commit is contained in:
Valentin Kipyatkov
2015-05-11 16:09:56 +03:00
parent 2ea2be01cd
commit 3743e9f508
5 changed files with 60 additions and 183 deletions
@@ -216,20 +216,8 @@ fold.if.to.call=Replace 'if' expression with method call
fold.if.to.call.family=Replace 'if' Expression with Method Call
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
unfold.call.to.when.family=Replace Method Call with 'when' Expression
if.then.to.double.bang=Replace 'if' expression with '!!' expression
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
public object BranchedUnfoldingUtils {
@@ -27,44 +26,6 @@ public object BranchedUnfoldingUtils {
return JetPsiUtil.getOutermostLastBlockElement(expression, JetPsiUtil.ANY_JET_ELEMENT) as JetExpression
}
public fun getUnfoldableExpressionKind(root: JetExpression?): UnfoldableKind? {
if (root == null) return null
when (root) {
is JetBinaryExpression -> {
if (root.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return null
if (root.getLeft() == null) return null
val rhs = root.getRight()
if (rhs is JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF
if (rhs is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(rhs)) {
return UnfoldableKind.ASSIGNMENT_TO_WHEN
}
}
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
}
}
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?) {
@@ -1,28 +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;
public interface Transformer {
@NotNull
String getKey();
void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file);
}
@@ -1,76 +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.JetBinaryExpression;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.JetProperty;
import org.jetbrains.kotlin.psi.JetReturnExpression;
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.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.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.INSTANCE$.unfoldReturnToIf((JetReturnExpression) element);
}
},
ASSIGNMENT_TO_WHEN("unfold.assignment.to.when") {
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, JetFile file) {
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.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.INSTANCE$.unfoldReturnToWhen((JetReturnExpression) element);
}
};
private final String key;
private UnfoldableKind(String key) {
this.key = key;
}
@NotNull
@Override
public String getKey() {
return key;
}
}
@@ -19,42 +19,74 @@ 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.BranchedUnfoldingUtils
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.UnfoldableKind
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
public open class UnfoldBranchedExpressionIntention<T: JetExpression>(
val kind: UnfoldableKind, elementType: Class<T>
) : JetSelfTargetingOffsetIndependentIntention<T>(kind.getKey(), elementType) {
override fun isApplicableTo(element: T): Boolean = BranchedUnfoldingUtils.getUnfoldableExpressionKind(element) == kind
public class UnfoldAssignmentToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'if' expression") {
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false
if (element.getLeft() == null) return false
return element.getRight() is JetIfExpression
}
override fun applyTo(element: T, editor: Editor) {
val file = element.getContainingFile()
if (file is JetFile) {
kind.transform(element, editor, file)
}
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldAssignmentToIf(element, editor)
}
}
public class UnfoldAssignmentToIfIntention : UnfoldBranchedExpressionIntention<JetBinaryExpression>(
UnfoldableKind.ASSIGNMENT_TO_IF, javaClass()
)
public class UnfoldPropertyToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>(javaClass(), "Replace property initializer with 'if' expression") {
override fun isApplicableTo(element: JetProperty): Boolean {
if (!element.isLocal()) return false
return element.getInitializer() is JetIfExpression
}
public class UnfoldPropertyToIfIntention : UnfoldBranchedExpressionIntention<JetProperty>(
UnfoldableKind.PROPERTY_TO_IF, javaClass()
)
override fun applyTo(element: JetProperty, editor: Editor) {
BranchedUnfoldingUtils.unfoldPropertyToIf(element, editor)
}
}
public class UnfoldAssignmentToWhenIntention : UnfoldBranchedExpressionIntention<JetBinaryExpression>(
UnfoldableKind.ASSIGNMENT_TO_WHEN, javaClass()
)
public class UnfoldAssignmentToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'when' expression" ) {
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false
if (element.getLeft() == null) return false
val right = element.getRight()
return right is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(right)
}
public class UnfoldPropertyToWhenIntention : UnfoldBranchedExpressionIntention<JetProperty>(
UnfoldableKind.PROPERTY_TO_WHEN, javaClass()
)
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldAssignmentToWhen(element, editor)
}
}
public class UnfoldReturnToIfIntention : UnfoldBranchedExpressionIntention<JetReturnExpression>(
UnfoldableKind.RETURN_TO_IF, javaClass()
)
public class UnfoldPropertyToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>(javaClass(), "Replace property initializer with 'when' expression") {
override fun isApplicableTo(element: JetProperty): Boolean {
if (!element.isLocal()) return false
val initializer = element.getInitializer()
return initializer is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)
}
public class UnfoldReturnToWhenIntention : UnfoldBranchedExpressionIntention<JetReturnExpression>(
UnfoldableKind.RETURN_TO_WHEN, javaClass()
)
override fun applyTo(element: JetProperty, editor: Editor) {
BranchedUnfoldingUtils.unfoldPropertyToWhen(element, editor)
}
}
public class UnfoldReturnToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetReturnExpression>(javaClass(), "Replace return with 'if' expression") {
override fun isApplicableTo(element: JetReturnExpression): Boolean {
return element.getReturnedExpression() is JetIfExpression
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldReturnToIf(element)
}
}
public class UnfoldReturnToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetReturnExpression>(javaClass(), "Replace return with 'when' expression") {
override fun isApplicableTo(element: JetReturnExpression): Boolean {
val expr = element.getReturnedExpression()
return expr is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(expr)
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldReturnToWhen(element)
}
}