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
This commit is contained in:
Roman Golyshev
2020-05-21 06:33:15 +03:00
parent b89878a509
commit a83b0bef96
@@ -99,10 +99,21 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
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