Skip to content

Commit 5e8afd2

Browse files
committed
Do not run write actions from J2K #KT-16714 Fixed
Also #EA-97363 Fixed
1 parent f950ff4 commit 5e8afd2

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.FileModificationService
2020
import com.intellij.codeInsight.daemon.HighlightDisplayKey
2121
import com.intellij.codeInsight.intention.IntentionAction
2222
import com.intellij.codeInspection.LocalInspectionEP
23+
import com.intellij.openapi.application.ApplicationManager
2324
import com.intellij.openapi.editor.Editor
2425
import com.intellij.openapi.extensions.Extensions
2526
import com.intellij.openapi.project.Project
@@ -30,6 +31,7 @@ import com.intellij.psi.PsiElement
3031
import com.intellij.psi.PsiFile
3132
import com.intellij.psi.util.PsiTreeUtil
3233
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
34+
import org.jetbrains.kotlin.idea.util.application.runWriteAction
3335
import org.jetbrains.kotlin.psi.KtBlockExpression
3436
import org.jetbrains.kotlin.psi.KtElement
3537
import org.jetbrains.kotlin.psi.psiUtil.containsInside
@@ -56,6 +58,10 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
5658

5759
abstract fun applyTo(element: TElement, editor: Editor?)
5860

61+
protected fun <R> tryRunWriteAction(action: () -> R): R =
62+
if (ApplicationManager.getApplication().isDispatchThread) runWriteAction(action)
63+
else run(action)
64+
5965
private fun getTarget(editor: Editor, file: PsiFile): TElement? {
6066
val offset = editor.caretModel.offset
6167
val leaf1 = file.findElementAt(offset)

idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
145145
it.typeReference!!)
146146
}
147147
val checkedExpression = condition.checkedExpression()!!
148-
val elvis = runWriteAction {
148+
val elvis = tryRunWriteAction {
149149
val replacedLeft = if (left.evaluatesTo(checkedExpression)) {
150150
if (condition is KtIsExpression) newReceiver!! else left
151151
}

idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToSafeAccessIntention.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
6767
}
6868

6969
val newExpr = KtPsiFactory(element).createExpressionByPattern("$0?.$1", receiverExpression, selectorExpression) as KtSafeQualifiedExpression
70-
val safeAccessExpr = runWriteAction {
70+
val safeAccessExpr = tryRunWriteAction {
7171
element.replaced(newExpr)
7272
}
7373

0 commit comments

Comments
 (0)