From 84498513f6c08613ab5d1c8f843c13151a1e544d Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 31 Jul 2019 17:11:47 +0300 Subject: [PATCH] Do not allow optimize imports for uncommitted document Optimize imports may be called after executing command, and document can be uncommitted after command is finished. In that case we will get ISE: Attempt to modify PSI for non-committed Document #EA-209693 fixed --- .../kotlin/idea/imports/KotlinImportOptimizer.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt b/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt index 270c3d707ec..2b718461754 100644 --- a/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt @@ -20,6 +20,7 @@ import com.intellij.lang.ImportOptimizer import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.progress.ProgressIndicatorProvider import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import org.jetbrains.kotlin.descriptors.* @@ -60,9 +61,23 @@ class KotlinImportOptimizer : ImportOptimizer { } } + // The same as com.intellij.pom.core.impl.PomModelImpl.isDocumentUncommitted + // Which is checked in com.intellij.pom.core.impl.PomModelImpl.startTransaction + private val KtFile.isDocumentUncommitted: Boolean + get() { + val documentManager = PsiDocumentManager.getInstance(project) + val cachedDocument = documentManager.getCachedDocument(this) + return cachedDocument != null && documentManager.isUncommited(cachedDocument) + } + private fun prepareImports(file: KtFile): OptimizeInformation? { ApplicationManager.getApplication().assertReadAccessAllowed() + // Optimize imports may be called after command + // And document can be uncommitted after running that command + // In that case we will get ISE: Attempt to modify PSI for non-committed Document! + if (file.isDocumentUncommitted) return null + val moduleInfo = file.getNullableModuleInfo() if (moduleInfo !is ModuleSourceInfo && moduleInfo !is ScriptModuleInfo) return null