if-then --> elvis: run write action manually to avoid AWT events inside
This commit is contained in:
+15
-10
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntent
|
|||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
||||||
import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression
|
import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression
|
||||||
import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver
|
import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver
|
||||||
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
@@ -114,6 +115,8 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun startInWriteAction() = false
|
||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||||
val condition = element.condition as KtOperationExpression
|
val condition = element.condition as KtOperationExpression
|
||||||
|
|
||||||
@@ -142,20 +145,22 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
it.typeReference!!)
|
it.typeReference!!)
|
||||||
}
|
}
|
||||||
val checkedExpression = condition.checkedExpression()!!
|
val checkedExpression = condition.checkedExpression()!!
|
||||||
val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
|
val elvis = runWriteAction {
|
||||||
if (condition is KtIsExpression) newReceiver!! else left
|
val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
|
||||||
}
|
if (condition is KtIsExpression) newReceiver!! else left
|
||||||
else {
|
|
||||||
if (condition is KtIsExpression) {
|
|
||||||
(left as KtDotQualifiedExpression).replaceFirstReceiver(
|
|
||||||
factory, newReceiver!!, safeAccess = true)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
left.insertSafeCalls(factory)
|
if (condition is KtIsExpression) {
|
||||||
|
(left as KtDotQualifiedExpression).replaceFirstReceiver(
|
||||||
|
factory, newReceiver!!, safeAccess = true)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
left.insertSafeCalls(factory)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
val newExpr = element.replaced(factory.createExpressionByPattern("$0 ?: $1", replacedLeft, right))
|
||||||
|
KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression
|
||||||
}
|
}
|
||||||
val newExpr = element.replaced(factory.createExpressionByPattern("$0 ?: $1", replacedLeft, right))
|
|
||||||
val elvis = KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression
|
|
||||||
|
|
||||||
if (editor != null) {
|
if (editor != null) {
|
||||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||||
|
|||||||
Reference in New Issue
Block a user