ElvisToIfThenIntention - smaller range + minor code improvements

This commit is contained in:
Valentin Kipyatkov
2015-05-09 10:12:07 +03:00
parent 3b7bb81141
commit b9486846e1
6 changed files with 15 additions and 12 deletions
@@ -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
@@ -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<JetBinaryExpression>("elvis.to.if.then", javaClass()) {
override fun isApplicableTo(element: JetBinaryExpression): Boolean =
element.getOperationToken() == JetTokens.ELVIS
public class ElvisToIfThenIntention : JetSelfTargetingRangeIntention<JetBinaryExpression>(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()
@@ -6,5 +6,5 @@ fun bar() {
}
fun main(args: Array<String>) {
foo() ?: <caret>bar()
foo() ?:<caret> bar()
}
@@ -6,5 +6,5 @@ fun bar() {
}
fun main(args: Array<String>) {
(foo()) ?: <caret>bar()
(foo()) ?:<caret> bar()
}
@@ -1,5 +1,5 @@
fun main(args: Array<String>) {
val foo: String? = "foo"
var bar = "bar"
foo ?: <caret>bar
foo ?:<caret> bar
}
@@ -2,5 +2,5 @@ fun bar(): String = "bar"
fun main(args: Array<String>) {
val foo: String? = "foo"
(foo) ?: <caret>bar()
(foo) ?:<caret> bar()
}