diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt index 814ee9e4481..6606d8e5aa6 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt @@ -99,10 +99,21 @@ abstract class SelfTargetingIntention( final override fun invoke(project: Project, editor: Editor?, file: PsiFile) { editor ?: return val target = getTarget(editor, file) ?: return - if (!FileModificationService.getInstance().preparePsiElementForWrite(target)) return + if (!preparePsiElementForWriteIfNeeded(target)) return applyTo(target, editor) } + /** + * If [startInWriteAction] returns true, that means that the platform already called `preparePsiElementForWrite` + * for us (we do not want to call it again because it will throw if the intention is used with Intention Preview). + * + * Otherwise we have to call it ourselves (see javadoc for [getElementToMakeWritable]). + */ + private fun preparePsiElementForWriteIfNeeded(target: TElement): Boolean { + if (startInWriteAction()) return true + return FileModificationService.getInstance().preparePsiElementForWrite(target) + } + override fun startInWriteAction() = true override fun toString(): String = text