Added ifThenToElvisIntention
This commit is contained in:
committed by
Zalim Bashorov
parent
673369affe
commit
1ca4bb0e79
@@ -348,6 +348,7 @@ fun main(args: Array<String>) {
|
||||
|
||||
testClass(javaClass<AbstractCodeTransformationTest>()) {
|
||||
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")
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
val result = maybeSomething ?: somethingElse
|
||||
@@ -0,0 +1 @@
|
||||
val result = if (maybeSomething != null) maybeSomething else somethingElse
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Converts an if expression to an expression using an elvis operator (if applicable)
|
||||
</body>
|
||||
</html>
|
||||
@@ -479,6 +479,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfThenToElvisIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfToWhenIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -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'
|
||||
|
||||
+70
@@ -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<JetIfExpression>("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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = null
|
||||
val bar = "bar"
|
||||
|
||||
if (foo != null<caret>) {
|
||||
print ("Hello")
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo = null
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
|
||||
if (foo != null<caret>) {
|
||||
a
|
||||
}
|
||||
else {
|
||||
b
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
if (null == <caret>null) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo = null
|
||||
val bar = "bar"
|
||||
if (foo > null<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: Int? = 4
|
||||
val bar = 3
|
||||
if (foo * 10<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
print(foo)
|
||||
val bar = "bar"
|
||||
if (foo != null<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
print(foo)
|
||||
val bar = "bar"
|
||||
foo ?: bar
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
val x = maybeFoo()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (x !=<caret> null) {
|
||||
x
|
||||
} else {
|
||||
"abc"
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
val x = maybeFoo()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
x ?: "abc"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo = "foo"
|
||||
val bar = "foo"
|
||||
if (<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = null
|
||||
if (foo != null<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
val bar = "bar"
|
||||
if (foo == null<caret>) {
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
if (foo != null<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
if (foo != null<caret>)
|
||||
foo
|
||||
else
|
||||
bar
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
val x = if (foo == null<caret>) {
|
||||
bar
|
||||
}
|
||||
else {
|
||||
foo
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
val x = maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
if (foo == null<caret>)
|
||||
bar
|
||||
else
|
||||
foo
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
if (foo != null<caret>)
|
||||
foo
|
||||
else
|
||||
bar
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
if (foo == null<caret>) {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
val bar = "bar"
|
||||
if (foo != null<caret>)
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
val bar = "bar"
|
||||
if<caret> {
|
||||
foo
|
||||
} else bar
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo = "foo"
|
||||
val bar = "bar"
|
||||
if (foo == bar<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//IS_APPLICABLE: false
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (maybeFoo() == null<caret>)
|
||||
maybeFoo()
|
||||
else
|
||||
"bar"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//IS_APPLICABLE: false
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var foo = maybeFoo()
|
||||
if (foo == null<caret>)
|
||||
"bar"
|
||||
else
|
||||
foo
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = "foo"
|
||||
if (foo == null<caret>) {
|
||||
null
|
||||
}
|
||||
else {
|
||||
foo
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo: String? = null
|
||||
val bar = "bar"
|
||||
|
||||
if (foo != null<caret>) {
|
||||
foo
|
||||
}
|
||||
else {
|
||||
print ("Hello")
|
||||
bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
val foo = maybeFoo()
|
||||
if (null == foo<caret>)
|
||||
bar
|
||||
else
|
||||
foo
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar = "bar"
|
||||
maybeFoo() ?: bar
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun bar(): String = "bar"
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
if (null != foo<caret>)
|
||||
foo
|
||||
else
|
||||
bar()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun bar(): String = "bar"
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
maybeFoo() ?: bar()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun main(args: Array<String>) {
|
||||
val foo = null
|
||||
if (foo == null<caret>) {
|
||||
null
|
||||
}
|
||||
else {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class F(a: Int?) {
|
||||
val b = a
|
||||
val c = if (b !=<caret> null) b else 2
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
F(1).c
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class F(a: Int?) {
|
||||
val b = a
|
||||
val c = b ?: 2
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
F(1).c
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user