From 1f548875c66461f382660e1c03be4795eb829dde Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Oct 2014 18:26:16 +0400 Subject: [PATCH] Hopefully fixed KT-4339 Auto-import suggestion doesn't go away for a long time #KT-4339 Fixed --- .../jet/utils/CachedValueProperty.kt | 34 +++++++++++++++++++ .../jet/plugin/quickfix/AutoImportFix.kt | 20 +++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 compiler/util/src/org/jetbrains/jet/utils/CachedValueProperty.kt diff --git a/compiler/util/src/org/jetbrains/jet/utils/CachedValueProperty.kt b/compiler/util/src/org/jetbrains/jet/utils/CachedValueProperty.kt new file mode 100644 index 00000000000..a901f53acb4 --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/CachedValueProperty.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.utils + +import kotlin.properties.ReadOnlyProperty + +public class CachedValueProperty(private val calculator: () -> TValue, + private val timestampCalculator: () -> TTimestamp) : ReadOnlyProperty { + private var value: TValue? = null + private var timestamp: TTimestamp? = null + + public override fun get(thisRef: Any?, desc: PropertyMetadata): TValue { + val currentTimestamp = timestampCalculator() + if (value == null || timestamp != currentTimestamp) { + value = calculator() + timestamp = currentTimestamp + } + return value!! + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt index d75a267f99f..a9b659307f4 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.kt @@ -53,6 +53,9 @@ import org.jetbrains.jet.plugin.search.searchScopeForSourceElementDependencies import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.jet.lang.descriptors.Visibilities +import org.jetbrains.jet.lang.diagnostics.Errors +import com.intellij.psi.util.PsiModificationTracker +import org.jetbrains.jet.utils.CachedValueProperty /** * Check possibility and perform fix for unresolved references. @@ -60,7 +63,9 @@ import org.jetbrains.jet.lang.descriptors.Visibilities public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction(element), HighPriorityAction { private val module = ModuleUtilCore.findModuleForPsiElement(element) - private val suggestions: Collection = computeSuggestions(element) + private val suggestions: Collection + by CachedValueProperty({ computeSuggestions(element) }, + { PsiModificationTracker.SERVICE.getInstance(element.getProject()).getModificationCount() }) override fun showHint(editor: Editor): Boolean { if (suggestions.isEmpty()) return false @@ -84,7 +89,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction.isAvailable(project, editor, file) && !suggestions.isEmpty() + = super.isAvailable(project, editor, file) && !suggestions.isEmpty() override fun invoke(project: Project, editor: Editor?, file: JetFile?) { CommandProcessor.getInstance().runUndoTransparentAction { @@ -99,6 +104,8 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction { + if (!element.isValid()) return listOf() + val file = element.getContainingFile() as? JetFile ?: return listOf() var referenceName = element.getReferencedName() @@ -114,7 +121,12 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction? {