Fix ConvertAssertToIfWithThrowIntention
to not invoke ShortenReferences on file created by JetPsiFactory #KT-5476 Fixed
This commit is contained in:
+28
-14
@@ -29,6 +29,8 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
|
|||||||
import org.jetbrains.jet.lang.psi.JetIfExpression
|
import org.jetbrains.jet.lang.psi.JetIfExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
|
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
|
||||||
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getResolvedCall
|
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getResolvedCall
|
||||||
|
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||||
|
import org.jetbrains.jet.lang.psi.JetThrowExpression
|
||||||
|
|
||||||
public class ConvertAssertToIfWithThrowIntention : JetSelfTargetingIntention<JetCallExpression>(
|
public class ConvertAssertToIfWithThrowIntention : JetSelfTargetingIntention<JetCallExpression>(
|
||||||
"convert.assert.to.if.with.throw", javaClass()) {
|
"convert.assert.to.if.with.throw", javaClass()) {
|
||||||
@@ -57,6 +59,13 @@ public class ConvertAssertToIfWithThrowIntention : JetSelfTargetingIntention<Jet
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
||||||
|
val replacementIfExpression = createReplacementExpression(element) ?: return
|
||||||
|
val replaced = replaceExpression(element, replacementIfExpression)
|
||||||
|
ShortenReferences.process(replaced.getThen()!!)
|
||||||
|
simplifyConditionIfPossible(editor, replaced)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createReplacementExpression(element: JetCallExpression): JetIfExpression? {
|
||||||
val args = element.getValueArguments()
|
val args = element.getValueArguments()
|
||||||
val condition = args[0]?.getArgumentExpression()
|
val condition = args[0]?.getArgumentExpression()
|
||||||
val lambdas = element.getFunctionLiteralArguments()
|
val lambdas = element.getFunctionLiteralArguments()
|
||||||
@@ -73,7 +82,7 @@ public class ConvertAssertToIfWithThrowIntention : JetSelfTargetingIntention<Jet
|
|||||||
psiFactory.createExpression("\"Assertion failed\"")
|
psiFactory.createExpression("\"Assertion failed\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (condition == null || messageExpr == null) return
|
if (condition == null || messageExpr == null) return null
|
||||||
|
|
||||||
val message =
|
val message =
|
||||||
if (messageIsAFunction && messageExpr is JetCallableReferenceExpression) {
|
if (messageIsAFunction && messageExpr is JetCallableReferenceExpression) {
|
||||||
@@ -86,25 +95,30 @@ public class ConvertAssertToIfWithThrowIntention : JetSelfTargetingIntention<Jet
|
|||||||
"${messageExpr.getText()}"
|
"${messageExpr.getText()}"
|
||||||
}
|
}
|
||||||
|
|
||||||
val assertTypeRef = psiFactory.createType("java.lang.AssertionError")
|
val text = "if (!true) { throw java.lang.AssertionError(${message}) }"
|
||||||
ShortenReferences.process(assertTypeRef)
|
|
||||||
|
|
||||||
val text = "if (!true) { throw ${assertTypeRef.getText()}(${message}) }"
|
|
||||||
val ifExpression = psiFactory.createExpression(text) as JetIfExpression
|
val ifExpression = psiFactory.createExpression(text) as JetIfExpression
|
||||||
|
|
||||||
val ifCondition = ifExpression.getCondition() as JetPrefixExpression
|
val ifCondition = ifExpression.getCondition() as JetPrefixExpression
|
||||||
ifCondition.getBaseExpression()?.replace(condition)
|
ifCondition.getBaseExpression()?.replace(condition)
|
||||||
|
return ifExpression
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun simplifyConditionIfPossible(editor: Editor, replaced: JetIfExpression) {
|
||||||
|
val condition = replaced.getCondition() as JetPrefixExpression
|
||||||
val simplifier = SimplifyNegatedBinaryExpressionIntention()
|
val simplifier = SimplifyNegatedBinaryExpressionIntention()
|
||||||
if (simplifier.isApplicableTo(ifCondition)) {
|
if (simplifier.isApplicableTo(condition)) {
|
||||||
simplifier.applyTo(ifCondition, editor)
|
simplifier.applyTo(condition, editor)
|
||||||
}
|
|
||||||
|
|
||||||
val parent = element.getParent()
|
|
||||||
if (parent is JetDotQualifiedExpression) {
|
|
||||||
parent.replace(ifExpression)
|
|
||||||
} else {
|
|
||||||
element.replace(ifExpression)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun replaceExpression(original: JetCallExpression, replacement: JetIfExpression): JetIfExpression {
|
||||||
|
val parent = original.getParent()
|
||||||
|
val result = if (parent is JetDotQualifiedExpression) {
|
||||||
|
parent.replace(replacement)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
original.replace(replacement)
|
||||||
|
}
|
||||||
|
return result as JetIfExpression
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
// WITH_RUNTIME
|
package a
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
<caret>assert(true, "text")
|
<caret>assert(true, "text")
|
||||||
}
|
}
|
||||||
|
|
||||||
class AssertionError
|
class AssertionError
|
||||||
|
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
// WITH_RUNTIME
|
package a
|
||||||
|
|
||||||
|
import java.lang
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
if (!true) {
|
if (!true) {
|
||||||
throw lang.AssertionError("text")
|
throw lang.AssertionError("text")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AssertionError
|
class AssertionError
|
||||||
|
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|||||||
Reference in New Issue
Block a user