Added elvisToIfThenIntention

This commit is contained in:
Zack Grannan
2014-03-02 17:39:17 -08:00
committed by Zalim Bashorov
parent 6b5e46da50
commit 673369affe
30 changed files with 208 additions and 0 deletions
@@ -347,6 +347,7 @@ fun main(args: Array<String>) {
}
testClass(javaClass<AbstractCodeTransformationTest>()) {
model("intentions/branched/elvisToIfThen", testMethod = "doTestElvisToIfThen")
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 = if (maybeSomething != null) maybeSomething else somethingElse
@@ -0,0 +1 @@
val result = maybeSomething ?: somethingElse
@@ -0,0 +1,5 @@
<html>
<body>
Converts an expression that uses an elvis operator to an if-then expression
</body>
</html>
+5
View File
@@ -474,6 +474,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.ElvisToIfThenIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfToWhenIntention</className>
<category>Kotlin</category>
@@ -219,6 +219,8 @@ unfold.return.to.when=Replace return with 'when' expression
unfold.return.to.when.family=Replace Return with 'when' Expression
unfold.call.to.when=Replace method call with 'when' expression
unfold.call.to.when.family=Replace Method Call with 'when' Expression
elvis.to.if.then=Replace elvis expression with 'if' expression
elvis.to.if.then.family=Replace Elvis Expression With 'if' Expression
if.to.when=Replace 'if' with 'when'
if.to.when.family=Replace 'if' with 'when'
when.to.if=Replace 'when' with 'if'
@@ -0,0 +1,45 @@
/*
* 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.lang.psi.JetBinaryExpression
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lang.psi.JetPsiUtil
import org.jetbrains.jet.plugin.intentions.branchedTransformations.convertToIfNotNullExpression
import org.jetbrains.jet.plugin.intentions.branchedTransformations.introduceValueForCondition
import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStableVariable
public class ElvisToIfThenIntention : JetSelfTargetingIntention<JetBinaryExpression>("elvis.to.if.then", javaClass()) {
override fun isApplicableTo(element: JetBinaryExpression): Boolean =
element.getOperationToken() == JetTokens.ELVIS
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
val left = checkNotNull(JetPsiUtil.deparenthesize(element.getLeft()), "Left hand side of elvis expression cannot be null")
val right = checkNotNull(JetPsiUtil.deparenthesize(element.getRight()), "Right hand side of elvis expression cannot be null")
val leftIsStable = left.isStableVariable()
val ifStatement = element.convertToIfNotNullExpression(left, left, right)
if (!leftIsStable) {
ifStatement.introduceValueForCondition(ifStatement.getThen()!!, editor)
}
}
}
@@ -0,0 +1,12 @@
fun foo(): String? {
print("foo")
return "foo"
}
fun bar() {
print("bar")
}
fun main(args: Array<String>) {
foo() ?: <caret>bar()
}
@@ -0,0 +1,13 @@
fun foo(): String? {
print("foo")
return "foo"
}
fun bar() {
print("bar")
}
fun main(args: Array<String>) {
val s = foo()
if (s != null) s else bar()
}
@@ -0,0 +1,12 @@
fun foo(): String? {
print("foo")
return "foo"
}
fun bar() {
print("bar")
}
fun main(args: Array<String>) {
(foo()) ?: <caret>bar()
}
@@ -0,0 +1,13 @@
fun foo(): String? {
print("foo")
return "foo"
}
fun bar() {
print("bar")
}
fun main(args: Array<String>) {
val s = foo()
if (s != null) s else bar()
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val foo: String? = "foo"
var bar: String = "bar"
val x = foo ?:<caret> bar
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val foo: String? = "foo"
var bar: String = "bar"
val x = if (foo != null) foo else bar
}
@@ -0,0 +1,4 @@
fun main(args: Array<String>) {
val a: String? = "A"
a ?:<caret> "bar"
}
@@ -0,0 +1,4 @@
fun main(args: Array<String>) {
val a: String? = "A"
if (a != null) a else "bar"
}
@@ -0,0 +1,4 @@
fun main(args: Array<String>) {
var a: String? = "A"
a ?:<caret> "bar"
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
var a: String? = "A"
val s = a
if (s != null) s else "bar"
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val foo: String? = "foo"
var bar = "bar"
foo ?: <caret>bar
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val foo: String? = "foo"
var bar = "bar"
if (foo != null) foo else bar
}
@@ -0,0 +1,6 @@
fun bar(): String = "bar"
fun main(args: Array<String>) {
val foo: String? = "foo"
(foo) ?: <caret>bar()
}
@@ -0,0 +1,6 @@
fun bar(): String = "bar"
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo != null) foo else bar()
}
@@ -0,0 +1,4 @@
val a: String? = "A"
fun main(args: Array<String>) {
a ?:<caret> "bar"
}
@@ -0,0 +1,4 @@
val a: String? = "A"
fun main(args: Array<String>) {
if (a != null) a else "bar"
}
@@ -0,0 +1,6 @@
val a: String?
get() = ""
fun main(args: Array<String>) {
a <caret>?: "bar"
}
@@ -0,0 +1,7 @@
val a: String?
get() = ""
fun main(args: Array<String>) {
val s = a
if (s != null) s else "bar"
}
@@ -0,0 +1,4 @@
var a: String? = "A"
fun main(args: Array<String>) {
a <caret>?: "bar"
}
@@ -0,0 +1,5 @@
var a: String? = "A"
fun main(args: Array<String>) {
val s = a
if (s != null) s else "bar"
}
@@ -0,0 +1,7 @@
var a: String?
get() = ""
set(v) {}
fun main(args: Array<String>) {
a ?:<caret> "bar"
}
@@ -0,0 +1,8 @@
var a: String?
get() = ""
set(v) {}
fun main(args: Array<String>) {
val s = a
if (s != null) s else "bar"
}
@@ -31,6 +31,10 @@ import org.junit.Assert;
import java.io.File;
public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase {
public void doTestElvisToIfThen(@NotNull String path) throws Exception {
doTestIntention(path, new ElvisToIfThenIntention());
}
public void doTestFoldIfToAssignment(@NotNull String path) throws Exception {
doTestIntention(path, new FoldIfToAssignmentIntention());
}