@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntent
26
26
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
27
27
import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression
28
28
import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver
29
+ import org.jetbrains.kotlin.idea.util.application.runWriteAction
29
30
import org.jetbrains.kotlin.lexer.KtTokens
30
31
import org.jetbrains.kotlin.psi.*
31
32
import org.jetbrains.kotlin.resolve.BindingContext
@@ -114,6 +115,8 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
114
115
return this
115
116
}
116
117
118
+ override fun startInWriteAction () = false
119
+
117
120
override fun applyTo (element : KtIfExpression , editor : Editor ? ) {
118
121
val condition = element.condition as KtOperationExpression
119
122
@@ -142,20 +145,22 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
142
145
it.typeReference!! )
143
146
}
144
147
val checkedExpression = condition.checkedExpression()!!
145
- val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
146
- if (condition is KtIsExpression ) newReceiver!! else left
147
- }
148
- else {
149
- if (condition is KtIsExpression ) {
150
- (left as KtDotQualifiedExpression ).replaceFirstReceiver(
151
- factory, newReceiver!! , safeAccess = true )
148
+ val elvis = runWriteAction {
149
+ val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
150
+ if (condition is KtIsExpression ) newReceiver!! else left
152
151
}
153
152
else {
154
- left.insertSafeCalls(factory)
153
+ if (condition is KtIsExpression ) {
154
+ (left as KtDotQualifiedExpression ).replaceFirstReceiver(
155
+ factory, newReceiver!! , safeAccess = true )
156
+ }
157
+ else {
158
+ left.insertSafeCalls(factory)
159
+ }
155
160
}
161
+ val newExpr = element.replaced(factory.createExpressionByPattern(" $0 ?: $1" , replacedLeft, right))
162
+ KtPsiUtil .deparenthesize(newExpr) as KtBinaryExpression
156
163
}
157
- val newExpr = element.replaced(factory.createExpressionByPattern(" $0 ?: $1" , replacedLeft, right))
158
- val elvis = KtPsiUtil .deparenthesize(newExpr) as KtBinaryExpression
159
164
160
165
if (editor != null ) {
161
166
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
0 commit comments