KT-9418 Suggest name for new declaration basing on unresolved names in code

#KT-9418 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-01-15 13:27:53 +03:00
parent 83ca4b84f9
commit 28331bca9f
23 changed files with 614 additions and 116 deletions
@@ -28,6 +28,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.colors.CodeInsightColors
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.MultiRangeReference
import com.intellij.psi.PsiElement
@@ -44,10 +45,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
import org.jetbrains.kotlin.idea.quickfix.QuickFixes
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.psi.KtCodeFragment
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import org.jetbrains.kotlin.utils.singletonOrEmptyList
@@ -81,6 +79,12 @@ open class KotlinPsiChecker : Annotator, HighlightRangeExtension {
fun annotateElement(element: PsiElement, holder: AnnotationHolder, diagnostics: Diagnostics) {
val diagnosticsForElement = diagnostics.forElement(element)
if (element is KtNameReferenceExpression) {
val unresolved = diagnostics.any { it.factory == Errors.UNRESOLVED_REFERENCE }
element.putUserData(UNRESOLVED_KEY, if (unresolved) Unit else null)
}
if (diagnosticsForElement.isEmpty()) return
if (ProjectRootsUtil.isInProjectSource(element) || element.containingFile is KtCodeFragment) {
@@ -98,6 +102,10 @@ open class KotlinPsiChecker : Annotator, HighlightRangeExtension {
fun createQuickFixes(diagnostic: Diagnostic): Collection<IntentionAction> =
createQuickFixes(diagnostic.singletonOrEmptyList())[diagnostic]
private val UNRESOLVED_KEY = Key<Unit>("KotlinPsiChecker.UNRESOLVED_KEY")
fun wasUnresolved(element: KtNameReferenceExpression) = element.getUserData(UNRESOLVED_KEY) != null
}
}