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
@@ -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>? {