if-then --> elvis: run write action manually to avoid AWT events inside

This commit is contained in:
Mikhail Glukhikh
2017-01-31 20:36:08 +03:00
parent 9594147c55
commit 834c3f5d62
@@ -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,6 +145,7 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
it.typeReference!!) it.typeReference!!)
} }
val checkedExpression = condition.checkedExpression()!! val checkedExpression = condition.checkedExpression()!!
val elvis = runWriteAction {
val replacedLeft = if (left.evaluatesTo(checkedExpression)) { val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
if (condition is KtIsExpression) newReceiver!! else left if (condition is KtIsExpression) newReceiver!! else left
} }
@@ -155,7 +159,8 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
} }
} }
val newExpr = element.replaced(factory.createExpressionByPattern("$0 ?: $1", replacedLeft, right)) val newExpr = element.replaced(factory.createExpressionByPattern("$0 ?: $1", replacedLeft, right))
val elvis = KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression KtPsiUtil.deparenthesize(newExpr) as KtBinaryExpression
}
if (editor != null) { if (editor != null) {
elvis.inlineLeftSideIfApplicableWithPrompt(editor) elvis.inlineLeftSideIfApplicableWithPrompt(editor)