Replace fold/unfold intentions with a set of specialized ones

This commit is contained in:
Alexey Sedunov
2013-04-16 14:33:58 +04:00
parent 9e441b0525
commit 64e578297c
113 changed files with 972 additions and 437 deletions
@@ -305,8 +305,15 @@ public class GenerateTests {
"idea/tests/",
"CodeTransformationsTestGenerated",
AbstractCodeTransformationTest.class,
testModel("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression", "doTestIfStatementWithAssignmentsToExpression"),
testModel("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement", "doTestAssignmentWithIfExpressionToStatement")
testModel("idea/testData/codeInsight/codeTransformations/branched/folding/ifToAssignment", "doTestFoldIfToAssignment"),
testModel("idea/testData/codeInsight/codeTransformations/branched/folding/ifToReturn", "doTestFoldIfToReturn"),
testModel("idea/testData/codeInsight/codeTransformations/branched/folding/ifToReturnAsymmetrically", "doTestFoldIfToReturnAsymmetrically"),
testModel("idea/testData/codeInsight/codeTransformations/branched/folding/whenToAssignment", "doTestFoldWhenToAssignment"),
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/returnToIf", "doTestUnfoldReturnToIf"),
testModel("idea/testData/codeInsight/codeTransformations/branched/unfolding/returnToWhen", "doTestUnfoldReturnToWhen")
);
generateTest(
@@ -1,7 +0,0 @@
res = if (n == 1) {
println("***")
"one"
} else {
println("***")
"two"
}
@@ -1,7 +0,0 @@
if (n == 1) {
println("***")
res = "one"
} else {
println("***")
res = "two"
}
@@ -1,6 +0,0 @@
<html>
<body>
This intention converts branched (if/when) expression where each branch is terminated with assignment/return/method call into single
assignment/return/method call with branched expression as argument
</body>
</html>
@@ -0,0 +1,5 @@
res = if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
if (ok) {
res = "ok"
} else {
res = "failed"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'if' expression where each branch is terminated with assignment into a single assignment with 'if' expression as a right-hand side
</body>
</html>
@@ -0,0 +1,3 @@
return if (ok) {
"ok"
} else "failed"
@@ -0,0 +1,4 @@
if (ok) {
return "ok"
}
return "failed"
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts single-branch 'if' expression immediately followed by 'return' into a single 'return' with 'if' expression as an argument
</body>
</html>
@@ -0,0 +1,5 @@
return if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
if (ok) {
return "ok"
} else {
return "failed"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'if' expression where each branch is terminated with 'return' into a single 'return' with 'if' expression as a right-hand side
</body>
</html>
@@ -0,0 +1,5 @@
res = when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
when (n) {
1 -> res = "one"
2 -> res = "two"
else -> res = "many"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'when' expression where each branch is terminated with assignment into a single assignment with 'when' expression as right-hand side
</body>
</html>
@@ -0,0 +1,5 @@
return when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
when (n) {
1 -> return "one"
2 -> return "two"
else -> return "many"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'when' expression where each branch is terminated with 'return' into a single 'return' with 'when' expression as a right-hand side
</body>
</html>
@@ -0,0 +1,5 @@
if (ok) {
res = "ok"
} else {
res = "failed"
}
@@ -0,0 +1,5 @@
res = if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts assignment with 'if' right-hand side to 'if' expression where each branch is terminated with assignment
</body>
</html>
@@ -0,0 +1,5 @@
when (n) {
1 -> res = "one"
2 -> res = "two"
else -> res = "many"
}
@@ -0,0 +1,5 @@
res = when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts assignment with 'when' right-hand side to 'when' expression where each branch is terminated with assignment
</body>
</html>
@@ -1,7 +0,0 @@
if (n == 1) {
println("***")
res = "one"
} else {
println("***")
res = "two"
}
@@ -1,7 +0,0 @@
res = if (n == 1) {
println("***")
"one"
} else {
println("***")
"two"
}
@@ -1,6 +0,0 @@
<html>
<body>
This intention converts assignment/return/method call with branched (if/when) expression as argument
to branched expression where each branch is terminated with assignment/return/method call
</body>
</html>
@@ -0,0 +1,5 @@
if (ok) {
return "ok"
} else {
return "failed"
}
@@ -0,0 +1,5 @@
return if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'return' with 'if' expression as a result to 'if' expression where each branch is terminated with 'return'
</body>
</html>
@@ -0,0 +1,5 @@
when (n) {
1 -> return "one"
2 -> return "two"
else -> return "many"
}
@@ -0,0 +1,5 @@
return when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts 'return' with 'when' expression as a result to 'when' expression where each branch is terminated with 'return'
</body>
</html>
+37 -2
View File
@@ -292,12 +292,47 @@
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldBranchedExpressionIntention</className>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.FoldIfToAssignmentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldBranchedExpressionIntention</className>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.FoldIfToReturnAsymmetricallyIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.FoldIfToReturnIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.FoldWhenToAssignmentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.FoldWhenToReturnIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldAssignmentToIfIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldAssignmentToWhenIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldReturnToIfIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.UnfoldReturnToWhenIntention</className>
<category>Kotlin</category>
</intentionAction>
@@ -152,6 +152,30 @@ 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
unfold.assignment.to.if.family=Replace Assignment 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.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' Expressiontransform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
transform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
@@ -16,6 +16,7 @@
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
import com.google.common.base.Predicate;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -25,25 +26,38 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.core.Transformer;
public class FoldBranchedExpressionIntention extends BaseIntentionAction {
public FoldBranchedExpressionIntention() {
setText(JetBundle.message("fold.branched.expression"));
public abstract class AbstractCodeTransformationIntention<T extends Transformer> extends BaseIntentionAction {
private final T transformer;
private final Predicate<PsiElement> filter;
protected AbstractCodeTransformationIntention(@NotNull T transformer, @NotNull Predicate<PsiElement> filter) {
this.transformer = transformer;
this.filter = filter;
setText(JetBundle.message(transformer.getKey()));
}
@Nullable
private PsiElement getTarget(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, filter, false);
}
protected final T getTransformer() {
return transformer;
}
protected final Predicate<PsiElement> getFilter() {
return filter;
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("fold.branched.expression.family");
}
@Nullable
private static JetExpression getTarget(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return (JetExpression)JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, BranchedFoldingUtils.FOLDABLE_EXPRESSION, false);
return JetBundle.message(transformer.getKey() + ".family");
}
@Override
@@ -53,10 +67,10 @@ public class FoldBranchedExpressionIntention extends BaseIntentionAction {
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) throws IncorrectOperationException {
JetExpression target = getTarget(editor, file);
PsiElement target = getTarget(editor, file);
assert target != null;
assert target != null : "Intention is not applicable";
BranchedFoldingUtils.foldExpression(target);
transformer.transform(target);
}
}
@@ -138,26 +138,27 @@ public class BranchedFoldingUtils {
return (nextElement instanceof JetExpression) && checkAndGetFoldableBranchedReturn((JetExpression)nextElement) != null;
}
public static boolean checkFoldableExpression(@Nullable JetExpression root) {
@Nullable
public static FoldableKind getFoldableExpressionKind(@Nullable JetExpression root) {
if (root instanceof JetIfExpression) {
JetIfExpression ifExpression = (JetIfExpression)root;
return checkFoldableIfExpressionWithAssignments(ifExpression) ||
checkFoldableIfExpressionWithReturns(ifExpression) ||
checkFoldableIfExpressionWithAsymmetricReturns(ifExpression) ;
}
if (root instanceof JetWhenExpression) {
if (checkFoldableIfExpressionWithAssignments(ifExpression)) return FoldableKind.IF_TO_ASSIGNMENT;
if (checkFoldableIfExpressionWithReturns(ifExpression)) return FoldableKind.IF_TO_RETURN;
if (checkFoldableIfExpressionWithAsymmetricReturns(ifExpression)) return FoldableKind.IF_TO_RETURN_ASYMMETRICALLY;
} else if (root instanceof JetWhenExpression) {
JetWhenExpression whenExpression = (JetWhenExpression)root;
return checkFoldableWhenExpressionWithAssignments(whenExpression) ||
checkFoldableWhenExpressionWithReturns(whenExpression);
if (checkFoldableWhenExpressionWithAssignments(whenExpression)) return FoldableKind.WHEN_TO_ASSIGNMENT;
if (checkFoldableWhenExpressionWithReturns(whenExpression)) return FoldableKind.WHEN_TO_RETURN;
}
return false;
return null;
}
public static final String FOLD_WITHOUT_CHECK = "Expression must be checked before folding";
private static void foldIfExpressionWithAssignments(JetIfExpression ifExpression) {
public static void foldIfExpressionWithAssignments(JetIfExpression ifExpression) {
Project project = ifExpression.getProject();
JetBinaryExpression thenAssignment = checkAndGetFoldableBranchedAssignment(ifExpression.getThen());
@@ -189,7 +190,7 @@ public class BranchedFoldingUtils {
elseAssignment.replace(elseRhs);
}
private static void foldIfExpressionWithReturns(JetIfExpression ifExpression) {
public static void foldIfExpressionWithReturns(JetIfExpression ifExpression) {
Project project = ifExpression.getProject();
JetReturnExpression returnExpr = (JetReturnExpression)ifExpression.replace(JetPsiFactory.createReturn(project, ifExpression));
@@ -213,7 +214,7 @@ public class BranchedFoldingUtils {
elseReturn.replace(elseExpr);
}
private static void foldIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
public static void foldIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
Project project = ifExpression.getProject();
JetExpression condition = ifExpression.getCondition();
@@ -254,7 +255,7 @@ public class BranchedFoldingUtils {
elseReturn.replace(elseExpr);
}
private static void foldWhenExpressionWithAssignments(JetWhenExpression whenExpression) {
public static void foldWhenExpressionWithAssignments(JetWhenExpression whenExpression) {
Project project = whenExpression.getProject();
assert !whenExpression.getEntries().isEmpty() : FOLD_WITHOUT_CHECK;
@@ -285,7 +286,7 @@ public class BranchedFoldingUtils {
}
}
private static void foldWhenExpressionWithReturns(JetWhenExpression whenExpression) {
public static void foldWhenExpressionWithReturns(JetWhenExpression whenExpression) {
Project project = whenExpression.getProject();
assert !whenExpression.getEntries().isEmpty() : FOLD_WITHOUT_CHECK;
@@ -307,33 +308,4 @@ public class BranchedFoldingUtils {
currReturn.replace(currExpr);
}
}
public static void foldExpression(@Nullable JetExpression root) {
if (root instanceof JetIfExpression) {
JetIfExpression ifExpression = (JetIfExpression)root;
if (checkFoldableIfExpressionWithAssignments(ifExpression)) {
foldIfExpressionWithAssignments(ifExpression);
} else if (checkFoldableIfExpressionWithReturns(ifExpression)) {
foldIfExpressionWithReturns(ifExpression);
} else if (checkFoldableIfExpressionWithAsymmetricReturns(ifExpression)) {
foldIfExpressionWithAsymmetricReturns(ifExpression);
}
}
if (root instanceof JetWhenExpression) {
JetWhenExpression whenExpression = (JetWhenExpression)root;
if (checkFoldableWhenExpressionWithAssignments(whenExpression)) {
foldWhenExpressionWithAssignments(whenExpression);
} else if (checkFoldableWhenExpressionWithReturns(whenExpression)) {
foldWhenExpressionWithReturns(whenExpression);
}
}
}
public static final Predicate<PsiElement> FOLDABLE_EXPRESSION = new Predicate<PsiElement>() {
@Override
public boolean apply(@Nullable PsiElement input) {
return (input instanceof JetExpression) && checkFoldableExpression((JetExpression)input);
}
};
}
@@ -27,31 +27,36 @@ public class BranchedUnfoldingUtils {
private BranchedUnfoldingUtils() {
}
private static boolean checkUnfoldableAssignment(@NotNull JetExpression expression) {
if (!JetPsiUtil.isAssignment(expression)) return false;
JetBinaryExpression assignment = (JetBinaryExpression)expression;
return assignment.getLeft() instanceof JetSimpleNameExpression && JetPsiUtil.isBranchedExpression(assignment.getRight());
}
private static boolean checkUnfoldableReturn(@NotNull JetExpression expression) {
if (!(expression instanceof JetReturnExpression)) return false;
JetReturnExpression returnExpression = (JetReturnExpression)expression;
return JetPsiUtil.isBranchedExpression(returnExpression.getReturnedExpression());
}
public static boolean checkUnfoldableExpression(@NotNull JetExpression root) {
return checkUnfoldableAssignment(root) || checkUnfoldableReturn(root);
}
private static JetExpression getOutermostLastBlockElement(@Nullable JetExpression expression) {
return (JetExpression) JetPsiUtil.getOutermostLastBlockElement(expression, JetPsiUtil.ANY_JET_ELEMENT);
}
@Nullable
public static UnfoldableKind getUnfoldableExpressionKind(@Nullable JetExpression root) {
if (root == null) return null;
if (JetPsiUtil.isAssignment(root)) {
JetBinaryExpression assignment = (JetBinaryExpression)root;
JetExpression lhs = assignment.getLeft();
JetExpression rhs = assignment.getRight();
if (!(lhs instanceof JetSimpleNameExpression)) return null;
if (rhs instanceof JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF;
if (rhs instanceof JetWhenExpression) return UnfoldableKind.ASSIGNMENT_TO_WHEN;
} else if (root instanceof JetReturnExpression) {
JetExpression resultExpr = ((JetReturnExpression)root).getReturnedExpression();
if (resultExpr instanceof JetIfExpression) return UnfoldableKind.RETURN_TO_IF;
if (resultExpr instanceof JetWhenExpression) return UnfoldableKind.RETURN_TO_WHEN;
}
return null;
}
public static final String UNFOLD_WITHOUT_CHECK = "Expression must be checked before unfolding";
private static void unfoldAssignmentToIf(@NotNull JetBinaryExpression assignment) {
public static void unfoldAssignmentToIf(@NotNull JetBinaryExpression assignment) {
Project project = assignment.getProject();
String op = assignment.getOperationReference().getText();
String lhsText = assignment.getLeft().getText();
@@ -71,7 +76,7 @@ public class BranchedUnfoldingUtils {
elseExpr.replace(JetPsiFactory.createBinaryExpression(project, JetPsiFactory.createExpression(project, lhsText), op, elseExpr));
}
private static void unfoldAssignmentToWhen(@NotNull JetBinaryExpression assignment) {
public static void unfoldAssignmentToWhen(@NotNull JetBinaryExpression assignment) {
Project project = assignment.getProject();
String op = assignment.getOperationReference().getText();
JetExpression lhs = (JetExpression)assignment.getLeft().copy();
@@ -90,7 +95,7 @@ public class BranchedUnfoldingUtils {
}
}
private static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
public static void unfoldReturnToIf(@NotNull JetReturnExpression returnExpression) {
Project project = returnExpression.getProject();
JetIfExpression ifExpression = (JetIfExpression)returnExpression.getReturnedExpression();
@@ -108,7 +113,7 @@ public class BranchedUnfoldingUtils {
elseExpr.replace(JetPsiFactory.createReturn(project, elseExpr));
}
private static void unfoldReturnToWhen(@NotNull JetReturnExpression returnExpression) {
public static void unfoldReturnToWhen(@NotNull JetReturnExpression returnExpression) {
Project project = returnExpression.getProject();
JetWhenExpression whenExpression = (JetWhenExpression)returnExpression.getReturnedExpression();
@@ -124,29 +129,4 @@ public class BranchedUnfoldingUtils {
currExpr.replace(JetPsiFactory.createReturn(project, currExpr));
}
}
public static void unfoldExpression(@NotNull JetExpression root) {
if (checkUnfoldableAssignment(root)) {
JetBinaryExpression assignment = (JetBinaryExpression)root;
if (assignment.getRight() instanceof JetIfExpression) {
unfoldAssignmentToIf(assignment);
} else if (assignment.getRight() instanceof JetWhenExpression) {
unfoldAssignmentToWhen(assignment);
}
} else if (checkUnfoldableReturn(root)) {
JetReturnExpression returnExpression = (JetReturnExpression)root;
if (returnExpression.getReturnedExpression() instanceof JetIfExpression) {
unfoldReturnToIf(returnExpression);
} else if (returnExpression.getReturnedExpression() instanceof JetWhenExpression) {
unfoldReturnToWhen(returnExpression);
}
}
}
public static final Predicate<PsiElement> UNFOLDABLE_EXPRESSION = new Predicate<PsiElement>() {
@Override
public boolean apply(@Nullable PsiElement input) {
return (input instanceof JetExpression) && checkUnfoldableExpression((JetExpression)input);
}
};
}
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance TO 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,
* TOOUT 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetIfExpression;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.core.Transformer;
public enum FoldableKind implements Transformer {
IF_TO_ASSIGNMENT("fold.if.to.assignment") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedFoldingUtils.foldIfExpressionWithAssignments((JetIfExpression) element);
}
},
IF_TO_RETURN("fold.if.to.return") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedFoldingUtils.foldIfExpressionWithReturns((JetIfExpression) element);
}
},
IF_TO_RETURN_ASYMMETRICALLY("fold.if.to.return") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedFoldingUtils.foldIfExpressionWithAsymmetricReturns((JetIfExpression) element);
}
},
WHEN_TO_ASSIGNMENT("fold.when.to.assignment") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedFoldingUtils.foldWhenExpressionWithAssignments((JetWhenExpression) element);
}
},
WHEN_TO_RETURN("fold.when.to.return") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedFoldingUtils.foldWhenExpressionWithReturns((JetWhenExpression) element);
}
};
private final String key;
private FoldableKind(String key) {
this.key = key;
}
@NotNull
@Override
public String getKey() {
return key;
}
}
@@ -1,62 +0,0 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.plugin.JetBundle;
public class UnfoldBranchedExpressionIntention extends BaseIntentionAction {
public UnfoldBranchedExpressionIntention() {
setText(JetBundle.message("unfold.branched.expression"));
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("unfold.branched.expression.family");
}
@Nullable
private static JetExpression getTarget(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return (JetExpression)JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, BranchedUnfoldingUtils.UNFOLDABLE_EXPRESSION, false);
}
@Override
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
return getTarget(editor, file) != null;
}
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) throws IncorrectOperationException {
JetExpression target = getTarget(editor, file);
assert target != null;
BranchedUnfoldingUtils.unfoldExpression(target);
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
import org.jetbrains.jet.lang.psi.JetReturnExpression;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.core.Transformer;
public enum UnfoldableKind implements Transformer {
ASSIGNMENT_TO_IF("unfold.assignment.to.if") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedUnfoldingUtils.unfoldAssignmentToIf((JetBinaryExpression) element);
}
},
RETURN_TO_IF("unfold.return.to.if") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedUnfoldingUtils.unfoldReturnToIf((JetReturnExpression) element);
}
},
ASSIGNMENT_TO_WHEN("unfold.assignment.to.when") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedUnfoldingUtils.unfoldAssignmentToWhen((JetBinaryExpression) element);
}
},
RETURN_TO_WHEN("unfold.return.to.when") {
@Override
public void transform(@NotNull PsiElement element) {
BranchedUnfoldingUtils.unfoldReturnToWhen((JetReturnExpression) element);
}
};
private final String key;
private UnfoldableKind(String key) {
this.key = key;
}
@NotNull
@Override
public String getKey() {
return key;
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.core;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
public interface Transformer {
public @NotNull String getKey();
public void transform(@NotNull PsiElement element);
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.BranchedFoldingUtils;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public abstract class FoldBranchedExpressionIntention extends AbstractCodeTransformationIntention<FoldableKind> {
protected FoldBranchedExpressionIntention(@NotNull final FoldableKind foldableKind) {
super(
foldableKind,
new Predicate<PsiElement>() {
@Override
public boolean apply(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedFoldingUtils.getFoldableExpressionKind((JetExpression) input) == foldableKind;
}
}
);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public class FoldIfToAssignmentIntention extends FoldBranchedExpressionIntention {
public FoldIfToAssignmentIntention() {
super(FoldableKind.IF_TO_ASSIGNMENT);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public class FoldIfToReturnAsymmetricallyIntention extends FoldBranchedExpressionIntention {
public FoldIfToReturnAsymmetricallyIntention() {
super(FoldableKind.IF_TO_RETURN_ASYMMETRICALLY);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public class FoldIfToReturnIntention extends FoldBranchedExpressionIntention {
public FoldIfToReturnIntention() {
super(FoldableKind.IF_TO_RETURN);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public class FoldWhenToAssignmentIntention extends FoldBranchedExpressionIntention {
public FoldWhenToAssignmentIntention() {
super(FoldableKind.WHEN_TO_ASSIGNMENT);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldableKind;
public class FoldWhenToReturnIntention extends FoldBranchedExpressionIntention {
public FoldWhenToReturnIntention() {
super(FoldableKind.WHEN_TO_RETURN);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldableKind;
public class UnfoldAssignmentToIfIntention extends UnfoldBranchedExpressionIntention {
public UnfoldAssignmentToIfIntention() {
super(UnfoldableKind.ASSIGNMENT_TO_IF);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldableKind;
public class UnfoldAssignmentToWhenIntention extends UnfoldBranchedExpressionIntention {
public UnfoldAssignmentToWhenIntention() {
super(UnfoldableKind.ASSIGNMENT_TO_WHEN);
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.*;
public abstract class UnfoldBranchedExpressionIntention extends AbstractCodeTransformationIntention<UnfoldableKind> {
protected UnfoldBranchedExpressionIntention(@NotNull final UnfoldableKind unfoldableKind) {
super(
unfoldableKind,
new Predicate<PsiElement>() {
@Override
public boolean apply(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedUnfoldingUtils.getUnfoldableExpressionKind((JetExpression) input) == unfoldableKind;
}
}
);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldableKind;
public class UnfoldReturnToIfIntention extends UnfoldBranchedExpressionIntention {
public UnfoldReturnToIfIntention() {
super(UnfoldableKind.RETURN_TO_IF);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2013 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.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldableKind;
public class UnfoldReturnToWhenIntention extends UnfoldBranchedExpressionIntention {
public UnfoldReturnToWhenIntention() {
super(UnfoldableKind.RETURN_TO_WHEN);
}
}

Some files were not shown because too many files have changed in this diff Show More