From 5777b23955ea4f9ad9b5933414cf8b94c9090489 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 19 Dec 2014 01:41:35 +0300 Subject: [PATCH] Avoid recounting auto-import suggesting for outdated quick-fix objects --- .../jet/plugin/quickfix/AutoImportFix.kt | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt index 6e01bcc42f8..97c0e076a48 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt @@ -53,42 +53,49 @@ import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.jet.lang.diagnostics.Errors import com.intellij.psi.util.PsiModificationTracker -import org.jetbrains.jet.utils.CachedValueProperty import org.jetbrains.jet.plugin.completion.isVisible +import org.jetbrains.jet.utils.CachedValueProperty /** * Check possibility and perform fix for unresolved references. */ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction(element), HighPriorityAction { private val module = ModuleUtilCore.findModuleForPsiElement(element) + private val modificationCountOnCreate = PsiModificationTracker.SERVICE.getInstance(element.getProject()).getModificationCount() - private val suggestions: Collection - by CachedValueProperty({ computeSuggestions(element) }, - { PsiModificationTracker.SERVICE.getInstance(element.getProject()).getModificationCount() }) + volatile private var anySuggestionFound: Boolean? = null + + private val suggestions: Collection by CachedValueProperty( + { + val fqnames = computeSuggestions(element) + anySuggestionFound = !fqnames.isEmpty() + fqnames + }, + { PsiModificationTracker.SERVICE.getInstance(element.getProject()).getModificationCount() }) override fun showHint(editor: Editor): Boolean { - if (suggestions.isEmpty()) return false - val project = editor.getProject() ?: return false + if (!element.isValid() || isOutdated()) return false if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) return false + if (suggestions.isEmpty()) return false + if (!ApplicationManager.getApplication()!!.isUnitTestMode()) { val hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.first().asString()) + val project = editor.getProject() ?: return false HintManager.getInstance().showQuestionHint(editor, hintText, element.getTextOffset(), element.getTextRange()!!.getEndOffset(), createAction(project, editor)) } return true } - override fun getText() - = JetBundle.message("import.fix") + override fun getText() = JetBundle.message("import.fix") - override fun getFamilyName() - = JetBundle.message("import.fix") + override fun getFamilyName() = JetBundle.message("import.fix") override fun isAvailable(project: Project, editor: Editor, file: PsiFile) - = super.isAvailable(project, editor, file) && !suggestions.isEmpty() + = (super.isAvailable(project, editor, file)) && (anySuggestionFound ?: !suggestions.isEmpty()) override fun invoke(project: Project, editor: Editor?, file: JetFile?) { CommandProcessor.getInstance().runUndoTransparentAction { @@ -96,11 +103,11 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction { if (!element.isValid()) return listOf() @@ -129,7 +136,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction