IDEA plugin: fixed crashing ifExpressionToElvis intention when applying to if expression which is part of another expression.
#EA-57307 fixed
This commit is contained in:
+8
-1
@@ -31,6 +31,8 @@ import org.jetbrains.jet.plugin.intentions.branchedTransformations.inlineLeftSid
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.jet.plugin.intentions.branchedTransformations.throwsNullPointerExceptionWithNoArguments
|
||||
import org.jetbrains.jet.lang.psi.JetThrowExpression
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
|
||||
public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>("if.then.to.elvis", javaClass()) {
|
||||
|
||||
@@ -73,7 +75,12 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
|
||||
}
|
||||
|
||||
val resultingExprString = "${left.getText()} ?: ${right.getText()}"
|
||||
val elvis = element.replace(resultingExprString) as JetBinaryExpression
|
||||
val resultingExpression = JetPsiUtil.deparenthesize(element.replace(resultingExprString) as? JetExpression)
|
||||
|
||||
assert(resultingExpression is JetBinaryExpression,
|
||||
"Unexpected expression type: ${resultingExpression?.javaClass}, expected JetBinaryExpression, element = '${element.getText()}'")
|
||||
|
||||
val elvis= resultingExpression as JetBinaryExpression
|
||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
fun maybeFoo(): String? {
|
||||
return "foo"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val foo = maybeFoo()
|
||||
val bar = "bar"
|
||||
val x = "baz" + 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 = "baz" + (maybeFoo() ?: bar)
|
||||
}
|
||||
@@ -47,6 +47,14 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'.</problem_class>
|
||||
<description>Replace 'if' expression with elvis expression</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>ifAsPartOfExpression.kt</file>
|
||||
<line>8</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/src/ifAsPartOfExpression.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'.</problem_class>
|
||||
<description>Replace 'if' expression with elvis expression</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>ifAndElseNotInBlocks.kt</file>
|
||||
<line>8</line>
|
||||
|
||||
@@ -413,6 +413,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
doTestIfThenToElvis("idea/testData/intentions/branched/ifThenToElvis/ifAsExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("ifAsPartOfExpression.kt")
|
||||
public void testIfAsPartOfExpression() throws Exception {
|
||||
doTestIfThenToElvis("idea/testData/intentions/branched/ifThenToElvis/ifAsPartOfExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("lhsEqualsNull.kt")
|
||||
public void testLhsEqualsNull() throws Exception {
|
||||
doTestIfThenToElvis("idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt");
|
||||
|
||||
Reference in New Issue
Block a user