Hopefully fixed KT-4339 Auto-import suggestion doesn't go away for a long time

#KT-4339 Fixed
This commit is contained in:
Valentin Kipyatkov
2014-10-21 18:26:16 +04:00
parent 762dc31e98
commit 1f548875c6
2 changed files with 51 additions and 3 deletions
@@ -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<TValue : Any, TTimestamp : Any>(private val calculator: () -> TValue,
private val timestampCalculator: () -> TTimestamp) : ReadOnlyProperty<Any?, TValue> {
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!!
}
}
@@ -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<JetSimpleNameExpression>(element), HighPriorityAction {
private val module = ModuleUtilCore.findModuleForPsiElement(element)
private val suggestions: Collection<FqName> = computeSuggestions(element)
private val suggestions: Collection<FqName>
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<Jet
= JetBundle.message("import.fix")
override fun isAvailable(project: Project, editor: Editor, file: PsiFile)
= super< JetHintAction>.isAvailable(project, editor, file) && !suggestions.isEmpty()
= super<JetHintAction>.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<Jet
= JetAddImportAction(project, editor, element, suggestions)
private fun computeSuggestions(element: JetSimpleNameExpression): Collection<FqName> {
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<Jet
val searchScope = searchScopeForSourceElementDependencies(file) ?: return listOf()
val resolutionScope = resolveSession.resolveToElement(element)[BindingContext.RESOLUTION_SCOPE, element] ?: return listOf()
val bindingContext = resolveSession.resolveToElement(element)
val diagnostics = bindingContext.getDiagnostics().forElement(element)
if (!diagnostics.any { it.getFactory() in ERRORS }) return listOf()
val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return listOf()
val containingDescriptor = resolutionScope.getContainingDeclaration()
fun isVisible(descriptor: DeclarationDescriptor): Boolean {
@@ -192,6 +204,8 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
}
class object {
private val ERRORS = setOf(Errors.UNRESOLVED_REFERENCE, Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER)
public fun createFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): JetIntentionAction<JetSimpleNameExpression>? {