From a83b0bef968503123c49ee79998ab64d78a359c3 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Thu, 21 May 2020 06:33:15 +0300 Subject: [PATCH] KT-38841 Do not call `preparePsiElementForWrite` if not needed - This call breaks Intention Preview for all intentions that extend `SelfTargetingIntention` - This check is actually already performed by the platform before the intention invocation (but only if `startInWriteAction` returns `true`) - There are many other intentions which redundantly use this call, but most of them implement `LocalQuickFix`, which means that they are inspection based. Currently Intention Preview does not seem to support this kind of intentions --- .../idea/intentions/SelfTargetingIntention.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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