From b9486846e1e1e12c5921fae236a7af486b3e1d83 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 9 May 2015 10:12:07 +0300 Subject: [PATCH] ElvisToIfThenIntention - smaller range + minor code improvements --- .../jetbrains/kotlin/idea/JetBundle.properties | 2 -- .../intentions/ElvisToIfThenIntention.kt | 17 +++++++++++------ .../branched/elvisToIfThen/callExpression.kt | 2 +- .../elvisToIfThen/callExpressionParens.kt | 2 +- .../elvisToIfThen/simpleNameExpression.kt | 2 +- .../simpleNameExpressionInParens.kt | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index cc90b3490ea..9e5a8ec556b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -243,8 +243,6 @@ unfold.call.to.when.family=Replace Method Call with 'when' Expression if.then.to.double.bang=Replace 'if' expression with '!!' expression if.then.to.double.bang.replace.exception=Replace 'if' expression with '!!' expression (will remove Exception) if.then.to.double.bang.family=Replace 'if' Expression with '!!' Expression -elvis.to.if.then=Replace elvis expression with 'if' expression -elvis.to.if.then.family=Replace Elvis Expression with 'if' 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 merge.when=Merge 'when' expressions diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt index ec2007f609a..90710ed3340 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt @@ -17,7 +17,8 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable @@ -25,13 +26,17 @@ import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.JetBinaryExpression import org.jetbrains.kotlin.psi.JetPsiUtil -public class ElvisToIfThenIntention : JetSelfTargetingOffsetIndependentIntention("elvis.to.if.then", javaClass()) { - override fun isApplicableTo(element: JetBinaryExpression): Boolean = - element.getOperationToken() == JetTokens.ELVIS +public class ElvisToIfThenIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace elvis expression with 'if' expression") { + override fun applicabilityRange(element: JetBinaryExpression): TextRange? { + return if (element.getOperationToken() == JetTokens.ELVIS && element.getLeft() != null && element.getRight() != null) + element.getOperationReference().getTextRange() + else + null + } 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 left = JetPsiUtil.safeDeparenthesize(element.getLeft()!!) + val right = JetPsiUtil.safeDeparenthesize(element.getRight()!!) val leftIsStable = left.isStableVariable() diff --git a/idea/testData/intentions/branched/elvisToIfThen/callExpression.kt b/idea/testData/intentions/branched/elvisToIfThen/callExpression.kt index ab76eac55e8..985ababc0cf 100644 --- a/idea/testData/intentions/branched/elvisToIfThen/callExpression.kt +++ b/idea/testData/intentions/branched/elvisToIfThen/callExpression.kt @@ -6,5 +6,5 @@ fun bar() { } fun main(args: Array) { - foo() ?: bar() + foo() ?: bar() } diff --git a/idea/testData/intentions/branched/elvisToIfThen/callExpressionParens.kt b/idea/testData/intentions/branched/elvisToIfThen/callExpressionParens.kt index 3e80fa0d160..2656e102679 100644 --- a/idea/testData/intentions/branched/elvisToIfThen/callExpressionParens.kt +++ b/idea/testData/intentions/branched/elvisToIfThen/callExpressionParens.kt @@ -6,5 +6,5 @@ fun bar() { } fun main(args: Array) { - (foo()) ?: bar() + (foo()) ?: bar() } diff --git a/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpression.kt b/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpression.kt index 1b19decfbe1..c648508d3d9 100644 --- a/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpression.kt +++ b/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpression.kt @@ -1,5 +1,5 @@ fun main(args: Array) { val foo: String? = "foo" var bar = "bar" - foo ?: bar + foo ?: bar } diff --git a/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpressionInParens.kt b/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpressionInParens.kt index c4ae6fc64d0..4719f261737 100644 --- a/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpressionInParens.kt +++ b/idea/testData/intentions/branched/elvisToIfThen/simpleNameExpressionInParens.kt @@ -2,5 +2,5 @@ fun bar(): String = "bar" fun main(args: Array) { val foo: String? = "foo" - (foo) ?: bar() + (foo) ?: bar() }