KT-4574 New Intention: Split If - converts if statement containing one or more conjunction operators into two nested if statements

This commit is contained in:
gavinkeusch
2014-03-31 14:09:17 +04:00
committed by Pavel V. Talanov
parent 7c94e8a9af
commit a6a1bb2b7c
33 changed files with 400 additions and 2 deletions
@@ -383,6 +383,7 @@ fun main(args: Array<String>) {
model("intentions/convertNegatedBooleanSequence", testMethod="doTestConvertNegatedBooleanSequence")
model("intentions/convertNegatedExpressionWithDemorgansLaw", testMethod = "doTestConvertNegatedExpressionWithDemorgansLaw")
model("intentions/swapBinaryExpression", testMethod = "doTestSwapBinaryExpression")
model("intentions/splitIf", testMethod = "doTestSplitIf")
}
testClass(javaClass<AbstractJetInspectionTest>()) {
@@ -0,0 +1,2 @@
if (a)
if (b) return 1
@@ -0,0 +1 @@
if (a <spot>&&</spot> b) return 1
@@ -0,0 +1,6 @@
<html>
<body>
This intention converts <b><font color="#000080">if</font></b> statement containing conjuction operation in it its condition
into two nested <b><font color="#000080">if</font></b> statements with simplified conditions. <br> <br>
</body>
</html>
+5
View File
@@ -640,6 +640,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.SplitIfIntention</className>
<category>Kotlin</category>
</intentionAction>
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
</extensions>
@@ -285,7 +285,8 @@ convert.negated.boolean.sequence=Replace negated sequence with DeMorgan equivale
convert.negated.boolean.sequence.family=Replace negated sequence with DeMorgan equivalent
convert.negated.expression.with.demorgans.law=Convert negated expression to DeMorgan's equivalent
convert.negated.expression.with.demorgans.law.family=Convert negated expression to DeMorgan's equivalent
split.if=Split into 2 if's
split.if.family=Split If
property.is.implemented.too.many=Has implementations
property.is.overridden.too.many=Is overridden in subclasses
@@ -0,0 +1,84 @@
/*
* Copyright 2010-2014 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.intentions
import org.jetbrains.jet.lang.psi.JetIfExpression
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetBinaryExpression
import org.jetbrains.jet.lang.psi.JetPsiFactory
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.JetExpression
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
public class SplitIfIntention : JetSelfTargetingIntention<JetSimpleNameExpression>("split.if", javaClass()) {
override fun isApplicableTo(element: JetSimpleNameExpression): Boolean {
val operator = element.getReferencedNameElementType()
if (operator != JetTokens.ANDAND && operator != JetTokens.OROR) return false
if (element.getParent() !is JetBinaryExpression) return false
var expression = element.getParent() as JetBinaryExpression
if (expression.getRight() == null || expression.getLeft() == null) return false
while (expression.getParent() is JetBinaryExpression) {
expression = expression.getParent() as JetBinaryExpression
if (operator == JetTokens.ANDAND && expression.getOperationToken() != JetTokens.ANDAND) return false
if (operator == JetTokens.OROR && expression.getOperationToken() != JetTokens.OROR) return false
}
if (expression.getParent()?.getParent() !is JetIfExpression) return false
val ifExpression = expression.getParent()?.getParent() as JetIfExpression
if (ifExpression.getCondition() == null) return false
if (!PsiTreeUtil.isAncestor(ifExpression.getCondition(), element, false)) return false
if (ifExpression.getThen() == null) return false
return true
}
override fun applyTo(element: JetSimpleNameExpression, editor: Editor) {
val ifExpression = element.getParentByType(javaClass<JetIfExpression>())
val expression = element.getParent() as JetBinaryExpression
val rightExpression = getRight(expression, ifExpression!!.getCondition() as JetExpression)
val leftExpression = expression.getLeft()
val elseExpression = ifExpression.getElse()
val thenExpression = ifExpression.getThen()
if (element.getReferencedNameElementType() == JetTokens.ANDAND) {
ifExpression.replace(JetPsiFactory.createIf(element.getProject(), leftExpression,
JetPsiFactory.wrapInABlock(JetPsiFactory.createIf(element.getProject(), rightExpression, thenExpression,
elseExpression)), elseExpression))
}
else {
ifExpression.replace(JetPsiFactory.createIf(element.getProject(), leftExpression, thenExpression,
JetPsiFactory.createIf(element.getProject(), rightExpression, thenExpression, elseExpression)))
}
}
fun getRight(element: JetBinaryExpression, condition: JetExpression): JetExpression {
//gets the textOffset of the right side of the JetBinaryExpression in context to condition
val startOffset = element.getRight()!!.getTextOffset() - condition.getTextOffset()
val rightString = condition.getText()!![startOffset, condition.getTextLength()].toString()
return JetPsiFactory.createExpression(element.getProject(), rightString)
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
val c = true
if (a <caret>&& b || c) {
println("test")
}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
when (<caret>a && b) {
println("test")
}
}
@@ -0,0 +1,7 @@
fun foo() {
val a = true
val b = false
if (a <caret>&& b) {
println("test")
}
}
@@ -0,0 +1,9 @@
fun foo() {
val a = true
val b = false
if (a) {
if (b) {
println("test")
}
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
fun test(a: Boolean, b: Boolean): Boolean { return false }
if (test(a &<caret>& b)) {
println("test")
}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
if (a) {
val v = a <caret>&& b
}
}
@@ -0,0 +1,8 @@
fun foo() {
val a = true
val b = false
val c = true
if (a <caret>|| b && c) {
println("test")
}
}
@@ -0,0 +1,10 @@
fun foo() {
val a = true
val b = false
val c = true
if (a) {
println("test")
} else if (b && c) {
println("test")
}
}
@@ -0,0 +1,8 @@
fun foo() {
val a = true
val b = false
val c = true
if (a <caret>&& b && c) {
println("test")
}
}
@@ -0,0 +1,10 @@
fun foo() {
val a = true
val b = false
val c = true
if (a) {
if (b && c) {
println("test")
}
}
}
@@ -0,0 +1,8 @@
fun foo() {
val a = true
val b = false
val c = true
if (a && b &<caret>& c) {
println("test")
}
}
@@ -0,0 +1,10 @@
fun foo() {
val a = true
val b = false
val c = true
if (a && b) {
if (c) {
println("test")
}
}
}
@@ -0,0 +1,9 @@
fun foo() {
val a = true
val b = false
if (a <caret>&& b) {
println("test")
} else {
println("test2")
}
}
@@ -0,0 +1,13 @@
fun foo() {
val a = true
val b = false
if (a) {
if (b) {
println("test")
} else {
println("test2")
}
} else {
println("test2")
}
}
@@ -0,0 +1,7 @@
fun foo() {
fun test(): Boolean { return false }
val a = true
if (test() <caret>&& a) {
println("test")
}
}
@@ -0,0 +1,9 @@
fun foo() {
fun test(): Boolean { return false }
val a = true
if (test()) {
if (a) {
println("test")
}
}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
if (!(a <caret>&& b)) {
println("test")
}
}
@@ -0,0 +1,7 @@
fun foo() {
val a = true
val b = false
if (a <caret>&& !b) {
println("test")
}
}
@@ -0,0 +1,9 @@
fun foo() {
val a = true
val b = false
if (a) {
if (!b) {
println("test")
}
}
}
@@ -0,0 +1,7 @@
fun foo() {
val a = true
val b = false
if (a <caret>|| b) {
println("test")
}
}
@@ -0,0 +1,9 @@
fun foo() {
val a = true
val b = false
if (a) {
println("test")
} else if (b) {
println("test")
}
}
@@ -0,0 +1,9 @@
fun foo() {
val a = true
val b = false
if (a <caret>|| b) {
println("test")
} else {
println("test2")
}
}
@@ -0,0 +1,11 @@
fun foo() {
val a = true
val b = false
if (a) {
println("test")
} else if (b) {
println("test")
} else {
println("test2")
}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
fun foo() {
val a = true
val b = false
if (<caret>a && b) {
println("test")
}
}
@@ -208,6 +208,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
doTestIntention(path, new SimplifyNegatedBinaryExpressionIntention());
}
public void doTestSplitIf(@NotNull String path) throws Exception {
doTestIntention(path, new SplitIfIntention());
}
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
configureByFile(path);
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@InnerTestClasses({CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class})
@InnerTestClasses({CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class})
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/intentions/branched/elvisToIfThen")
public static class ElvisToIfThen extends AbstractCodeTransformationTest {
@@ -3009,6 +3009,89 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
}
@TestMetadata("idea/testData/intentions/splitIf")
public static class SplitIf extends AbstractCodeTransformationTest {
public void testAllFilesPresentInSplitIf() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/splitIf"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("splitIfAndOr.kt")
public void testSplitIfAndOr() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfAndOr.kt");
}
@TestMetadata("splitIfNotIf.kt")
public void testSplitIfNotIf() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfNotIf.kt");
}
@TestMetadata("splitIfOneAND.kt")
public void testSplitIfOneAND() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfOneAND.kt");
}
@TestMetadata("splitIfOperatorAsFunctionParam.kt")
public void testSplitIfOperatorAsFunctionParam() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfOperatorAsFunctionParam.kt");
}
@TestMetadata("splitIfOperatorOutsideIf.kt")
public void testSplitIfOperatorOutsideIf() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfOperatorOutsideIf.kt");
}
@TestMetadata("splitIfOrAnd.kt")
public void testSplitIfOrAnd() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfOrAnd.kt");
}
@TestMetadata("splitIfTwoOperatorsFirst.kt")
public void testSplitIfTwoOperatorsFirst() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfTwoOperatorsFirst.kt");
}
@TestMetadata("splitIfTwoOperatorsSecond.kt")
public void testSplitIfTwoOperatorsSecond() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfTwoOperatorsSecond.kt");
}
@TestMetadata("splitIfWithElse.kt")
public void testSplitIfWithElse() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithElse.kt");
}
@TestMetadata("splitIfWithFunction.kt")
public void testSplitIfWithFunction() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithFunction.kt");
}
@TestMetadata("splitIfWithNotOperator.kt")
public void testSplitIfWithNotOperator() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithNotOperator.kt");
}
@TestMetadata("splitIfWithNotOperatorGood.kt")
public void testSplitIfWithNotOperatorGood() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithNotOperatorGood.kt");
}
@TestMetadata("splitIfWithOR.kt")
public void testSplitIfWithOR() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithOR.kt");
}
@TestMetadata("splitIfWithORElse.kt")
public void testSplitIfWithORElse() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWithORElse.kt");
}
@TestMetadata("splitIfWrongCaretLocation.kt")
public void testSplitIfWrongCaretLocation() throws Exception {
doTestSplitIf("idea/testData/intentions/splitIf/splitIfWrongCaretLocation.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationTestGenerated");
suite.addTestSuite(ElvisToIfThen.class);
@@ -3055,6 +3138,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
suite.addTestSuite(ConvertNegatedBooleanSequence.class);
suite.addTestSuite(ConvertNegatedExpressionWithDemorgansLaw.class);
suite.addTestSuite(SwapBinaryExpression.class);
suite.addTestSuite(SplitIf.class);
return suite;
}
}