From 9504488acb9a6c1ad09d9068ccf43059907d80c5 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Thu, 13 May 2021 18:25:23 +0300 Subject: [PATCH] FIR IDE: Do not show hint from ImportQuickFix if the PSI have changed Without this check, the import hint will be available even after the quickfix is applied; this happens because the element to which the quickfix is attached is not invalidated by the quickfix execution Since the quickfix is still considered as available some time after the import have been added, the hint is also generated. And the hint stays even after the quickfix itself is discarded `isOutdated` function prevents this --- .../kotlin/idea/quickfix/fixes/ImportQuickFix.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ImportQuickFix.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ImportQuickFix.kt index 0ebe5e2b1a3..e1fa04b2d4c 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ImportQuickFix.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ImportQuickFix.kt @@ -16,6 +16,7 @@ import com.intellij.openapi.ui.popup.JBPopupFactory import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.util.PsiModificationTracker import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.caches.project.getModuleInfo import org.jetbrains.kotlin.idea.fir.api.fixes.diagnosticFixFactory @@ -79,6 +80,21 @@ internal class ImportQuickFix( return true } + private val modificationCountOnCreate: Long = PsiModificationTracker.SERVICE.getInstance(element.project).modificationCount + + /** + * This is a safe-guard against showing hint after the quickfix have been applied. + * + * Inspired by the org.jetbrains.kotlin.idea.quickfix.ImportFixBase.isOutdated + */ + private fun isOutdated(project: Project): Boolean { + return modificationCountOnCreate != PsiModificationTracker.SERVICE.getInstance(project).modificationCount + } + + override fun isAvailableImpl(project: Project, editor: Editor?, file: PsiFile): Boolean { + return super.isAvailableImpl(project, editor, file) && !isOutdated(project) + } + private class ImportQuestionAction( private val project: Project, private val editor: Editor,