diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index b7a89555d19..b7d38404a83 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -348,6 +348,7 @@ fun main(args: Array) { testClass(javaClass()) { model("intentions/branched/elvisToIfThen", testMethod = "doTestElvisToIfThen") + model("intentions/branched/ifThenToElvis", testMethod = "doTestIfThenToElvis") model("intentions/branched/folding/ifToAssignment", testMethod = "doTestFoldIfToAssignment") model("intentions/branched/folding/ifToReturn", testMethod = "doTestFoldIfToReturn") model("intentions/branched/folding/ifToReturnAsymmetrically", testMethod = "doTestFoldIfToReturnAsymmetrically") diff --git a/idea/resources/intentionDescriptions/IfThenToElvisIntention/after.kt.template b/idea/resources/intentionDescriptions/IfThenToElvisIntention/after.kt.template new file mode 100644 index 00000000000..a37f275650c --- /dev/null +++ b/idea/resources/intentionDescriptions/IfThenToElvisIntention/after.kt.template @@ -0,0 +1 @@ +val result = maybeSomething ?: somethingElse diff --git a/idea/resources/intentionDescriptions/IfThenToElvisIntention/before.kt.template b/idea/resources/intentionDescriptions/IfThenToElvisIntention/before.kt.template new file mode 100644 index 00000000000..6c64bfdc26d --- /dev/null +++ b/idea/resources/intentionDescriptions/IfThenToElvisIntention/before.kt.template @@ -0,0 +1 @@ +val result = if (maybeSomething != null) maybeSomething else somethingElse diff --git a/idea/resources/intentionDescriptions/IfThenToElvisIntention/description.html b/idea/resources/intentionDescriptions/IfThenToElvisIntention/description.html new file mode 100644 index 00000000000..ebe74dfee54 --- /dev/null +++ b/idea/resources/intentionDescriptions/IfThenToElvisIntention/description.html @@ -0,0 +1,5 @@ + + + Converts an if expression to an expression using an elvis operator (if applicable) + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index a257d9c6cfd..055b91cc3bb 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -479,6 +479,11 @@ Kotlin + + org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfThenToElvisIntention + Kotlin + + org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfToWhenIntention Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 5ea6e171518..088f1b16a62 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -221,6 +221,12 @@ unfold.call.to.when=Replace method call with 'when' expression unfold.call.to.when.family=Replace Method Call with 'when' Expression elvis.to.if.then=Replace elvis expression with 'if' expression elvis.to.if.then.family=Replace Elvis Expression With 'if' Expression +if.then.to.elvis=Replace 'if' expression with elvis expression +if.then.to.elvis.family=Replace 'if' Expression With Elvis Expression +safe.access.to.if.then=Replace safe access expression with 'if' expression +safe.access.to.if.then.family=Replace Safe Access Expression With 'if' Expression +if.then.to.safe.access=Replace 'if' expression with safe access expression +if.then.to.safe.access.family=Replace 'if' Expression with Safe Access Expression if.to.when=Replace 'if' with 'when' if.to.when.family=Replace 'if' with 'when' when.to.if=Replace 'when' with 'if' diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt new file mode 100644 index 00000000000..28287cf530e --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt @@ -0,0 +1,70 @@ +/* + * 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.branchedTransformations.intentions + +import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention +import com.intellij.openapi.editor.Editor +import org.jetbrains.jet.lang.psi.JetIfExpression +import org.jetbrains.jet.lang.psi.JetBinaryExpression +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.plugin.intentions.branchedTransformations.extractExpressionIfSingle +import org.jetbrains.jet.plugin.intentions.branchedTransformations.evaluatesTo +import org.jetbrains.jet.plugin.intentions.branchedTransformations.comparesNonNullToNull +import org.jetbrains.jet.plugin.intentions.branchedTransformations.getNonNullExpression +import org.jetbrains.jet.plugin.intentions.branchedTransformations.isNotNullExpression +import org.jetbrains.jet.plugin.intentions.branchedTransformations.replace +import org.jetbrains.jet.plugin.intentions.branchedTransformations.inlineLeftSideIfApplicableWithPrompt +import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStableVariable + +public class IfThenToElvisIntention : JetSelfTargetingIntention("if.then.to.elvis", javaClass()) { + + override fun isApplicableTo(element: JetIfExpression): Boolean { + val condition = element.getCondition() + val thenClause = element.getThen() + val elseClause = element.getElse() + if (thenClause == null || elseClause == null || condition !is JetBinaryExpression || !condition.comparesNonNullToNull()) return false + + val expression = condition.getNonNullExpression() + if (expression == null || !expression.isStableVariable()) return false + + return when (condition.getOperationToken()) { + JetTokens.EQEQ -> thenClause.isNotNullExpression() && elseClause.evaluatesTo(expression) + JetTokens.EXCLEQ -> elseClause.isNotNullExpression() && thenClause.evaluatesTo(expression) + else -> false + } + } + + override fun applyTo(element: JetIfExpression, editor: Editor) { + val condition = element.getCondition() as JetBinaryExpression + + val thenClause = checkNotNull(element.getThen(), "The then clause cannot be null") + val elseClause = checkNotNull(element.getElse(), "The else clause cannot be null") + val thenExpression = checkNotNull(thenClause.extractExpressionIfSingle(), "Then clause must contain expression") + val elseExpression = checkNotNull(elseClause.extractExpressionIfSingle(), "Else clause must contain expression") + + val (left, right) = + when(condition.getOperationToken()) { + JetTokens.EQEQ -> Pair(elseExpression, thenExpression) + JetTokens.EXCLEQ -> Pair(thenExpression, elseExpression) + else -> throw IllegalStateException("Operation token must be either null or not null") + } + + val resultingExprString = "${left.getText()} ?: ${right.getText()}" + val elvis = element.replace(resultingExprString) as JetBinaryExpression + elvis.inlineLeftSideIfApplicableWithPrompt(editor) + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/blockHasMoreThanOneStatement.kt b/idea/testData/intentions/branched/ifThenToElvis/blockHasMoreThanOneStatement.kt new file mode 100644 index 00000000000..ab7ac0691b5 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/blockHasMoreThanOneStatement.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = null + val bar = "bar" + + if (foo != null) { + print ("Hello") + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/blockUsesDifferentVar.kt b/idea/testData/intentions/branched/ifThenToElvis/blockUsesDifferentVar.kt new file mode 100644 index 00000000000..72f13d277af --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/blockUsesDifferentVar.kt @@ -0,0 +1,14 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo = null + val a = "a" + val b = "b" + + if (foo != null) { + a + } + else { + b + } + +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/conditionComparesNullWithNull.kt b/idea/testData/intentions/branched/ifThenToElvis/conditionComparesNullWithNull.kt new file mode 100644 index 00000000000..e9474fc929e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/conditionComparesNullWithNull.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + if (null == null) { + foo + } + else { + null + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/conditionInvalidBinaryExp.kt b/idea/testData/intentions/branched/ifThenToElvis/conditionInvalidBinaryExp.kt new file mode 100644 index 00000000000..e200570107f --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/conditionInvalidBinaryExp.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo = null + val bar = "bar" + if (foo > null) { + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/conditionNotBinaryExpr.kt b/idea/testData/intentions/branched/ifThenToElvis/conditionNotBinaryExpr.kt new file mode 100644 index 00000000000..e3bf07517ba --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/conditionNotBinaryExpr.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: Int? = 4 + val bar = 3 + if (foo * 10) { + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt new file mode 100644 index 00000000000..ef406ee9f1d --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt @@ -0,0 +1,15 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + print(foo) + val bar = "bar" + if (foo != null) { + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt.after b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt.after new file mode 100644 index 00000000000..67640f4aa79 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt.after @@ -0,0 +1,10 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + print(foo) + val bar = "bar" + foo ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt new file mode 100644 index 00000000000..f0b173fe240 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt @@ -0,0 +1,13 @@ +fun maybeFoo(): String? { + return "foo" +} + +val x = maybeFoo() + +fun main(args: Array) { + if (x != null) { + x + } else { + "abc" + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt.after b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt.after new file mode 100644 index 00000000000..503c71c0e50 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueOutsideOfScope.kt.after @@ -0,0 +1,9 @@ +fun maybeFoo(): String? { + return "foo" +} + +val x = maybeFoo() + +fun main(args: Array) { + x ?: "abc" +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/emptyCondition.kt b/idea/testData/intentions/branched/ifThenToElvis/emptyCondition.kt new file mode 100644 index 00000000000..3258b218fcf --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/emptyCondition.kt @@ -0,0 +1,12 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo = "foo" + val bar = "foo" + if () { + foo + } + else { + bar + } + +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/emptyElseBlock.kt b/idea/testData/intentions/branched/ifThenToElvis/emptyElseBlock.kt new file mode 100644 index 00000000000..1864b505085 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/emptyElseBlock.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = null + if (foo != null) { + foo + } + else { + + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/emptyThenBlock.kt b/idea/testData/intentions/branched/ifThenToElvis/emptyThenBlock.kt new file mode 100644 index 00000000000..0efaf8748bf --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/emptyThenBlock.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + val bar = "bar" + if (foo == null) { + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt new file mode 100644 index 00000000000..3abb5e0aada --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt @@ -0,0 +1,14 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + val bar = "bar" + if (foo != null) { + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt.after b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt.after new file mode 100644 index 00000000000..e6ad9d02f33 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseBothInBlocks.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt new file mode 100644 index 00000000000..87331d8e55e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt @@ -0,0 +1,12 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + val bar = "bar" + if (foo != null) + foo + else + bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt.after b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt.after new file mode 100644 index 00000000000..e6ad9d02f33 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAndElseNotInBlocks.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt b/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt new file mode 100644 index 00000000000..db0aa16b81e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt @@ -0,0 +1,14 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + val bar = "bar" + val x = if (foo == null) { + bar + } + else { + foo + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt.after b/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt.after new file mode 100644 index 00000000000..3447e328547 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + val x = maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt b/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt new file mode 100644 index 00000000000..03ff3d21e81 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt @@ -0,0 +1,12 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + val bar = "bar" + if (foo == null) + bar + else + foo +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt.after b/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt.after new file mode 100644 index 00000000000..e6ad9d02f33 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt b/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt new file mode 100644 index 00000000000..87331d8e55e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt @@ -0,0 +1,12 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val foo = maybeFoo() + val bar = "bar" + if (foo != null) + foo + else + bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt.after b/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt.after new file mode 100644 index 00000000000..e6ad9d02f33 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/lhsNotEqualsNull.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/missingElseClause.kt b/idea/testData/intentions/branched/ifThenToElvis/missingElseClause.kt new file mode 100644 index 00000000000..3c6d197adb6 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/missingElseClause.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + if (foo == null) { + null + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/missingThenClause.kt b/idea/testData/intentions/branched/ifThenToElvis/missingThenClause.kt new file mode 100644 index 00000000000..d4a46e007d9 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/missingThenClause.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + val bar = "bar" + if (foo != null) + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/noCondition.kt b/idea/testData/intentions/branched/ifThenToElvis/noCondition.kt new file mode 100644 index 00000000000..4328f8a0770 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/noCondition.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + val bar = "bar" + if { + foo + } else bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/noNullInCondition.kt b/idea/testData/intentions/branched/ifThenToElvis/noNullInCondition.kt new file mode 100644 index 00000000000..1fe611daf90 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/noNullInCondition.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo = "foo" + val bar = "bar" + if (foo == bar) { + foo + } + else { + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/notApplicableForFunction.kt b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForFunction.kt new file mode 100644 index 00000000000..44f4a4f982b --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForFunction.kt @@ -0,0 +1,11 @@ +//IS_APPLICABLE: false +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + if (maybeFoo() == null) + maybeFoo() + else + "bar" +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/notApplicableForLocalVar.kt b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForLocalVar.kt new file mode 100644 index 00000000000..a9d3a6f648f --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/notApplicableForLocalVar.kt @@ -0,0 +1,12 @@ +//IS_APPLICABLE: false +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + var foo = maybeFoo() + if (foo == null) + "bar" + else + foo +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/nullBranchAlsoNull.kt b/idea/testData/intentions/branched/ifThenToElvis/nullBranchAlsoNull.kt new file mode 100644 index 00000000000..11da5e88dfe --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/nullBranchAlsoNull.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = "foo" + if (foo == null) { + null + } + else { + foo + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt b/idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt new file mode 100644 index 00000000000..a4a45b7123b --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo: String? = null + val bar = "bar" + + if (foo != null) { + foo + } + else { + print ("Hello") + bar + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt b/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt new file mode 100644 index 00000000000..70dea85cfb5 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt @@ -0,0 +1,12 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + val foo = maybeFoo() + if (null == foo) + bar + else + foo +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt.after b/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt.after new file mode 100644 index 00000000000..e6ad9d02f33 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/rhsEqualsNull.kt.after @@ -0,0 +1,8 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun main(args: Array) { + val bar = "bar" + maybeFoo() ?: bar +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt b/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt new file mode 100644 index 00000000000..e9049ecda30 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt @@ -0,0 +1,13 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun bar(): String = "bar" + +fun main(args: Array) { + val foo = maybeFoo() + if (null != foo) + foo + else + bar() +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt.after b/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt.after new file mode 100644 index 00000000000..d80191145ca --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/rhsNotEqualsNull.kt.after @@ -0,0 +1,9 @@ +fun maybeFoo(): String? { + return "foo" +} + +fun bar(): String = "bar" + +fun main(args: Array) { + maybeFoo() ?: bar() +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/thenAndElseBothNull.kt b/idea/testData/intentions/branched/ifThenToElvis/thenAndElseBothNull.kt new file mode 100644 index 00000000000..3bc679de523 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/thenAndElseBothNull.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun main(args: Array) { + val foo = null + if (foo == null) { + null + } + else { + null + } +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt b/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt new file mode 100644 index 00000000000..03462f4a1fb --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt @@ -0,0 +1,8 @@ +class F(a: Int?) { + val b = a + val c = if (b != null) b else 2 +} + +fun main(args: Array) { + F(1).c +} diff --git a/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt.after b/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt.after new file mode 100644 index 00000000000..26a3cd363b2 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt.after @@ -0,0 +1,8 @@ +class F(a: Int?) { + val b = a + val c = b ?: 2 +} + +fun main(args: Array) { + F(1).c +} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index 47ea545d8b3..a660ca3f1c4 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -35,6 +35,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes doTestIntention(path, new ElvisToIfThenIntention()); } + public void doTestIfThenToElvis(@NotNull String path) throws Exception { + doTestIntention(path, new IfThenToElvisIntention()); + } + public void doTestFoldIfToAssignment(@NotNull String path) throws Exception { doTestIntention(path, new FoldIfToAssignmentIntention()); }