KT-9326 IDE tries to resolve name in qualified this to any class in project/classpath

#KT-9326 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-10-02 19:22:55 +03:00
parent dda9476793
commit b03a297ed0
4 changed files with 56 additions and 24 deletions
@@ -121,30 +121,48 @@ public sealed class CallTypeAndReceiver<TReceiver : JetElement?, TCallType : Cal
return CallTypeAndReceiver.TYPE(receiverExpression) return CallTypeAndReceiver.TYPE(receiverExpression)
} }
if (receiverExpression == null) { when (expression) {
return CallTypeAndReceiver.DEFAULT is JetOperationReferenceExpression -> {
} if (receiverExpression == null) {
return UNKNOWN // incomplete code
}
return when (parent) {
is JetBinaryExpression -> {
//TODO: operator vs infix
CallTypeAndReceiver.INFIX(receiverExpression)
}
return when (parent) { is JetUnaryExpression -> CallTypeAndReceiver.UNARY(receiverExpression)
is JetBinaryExpression -> CallTypeAndReceiver.INFIX(receiverExpression)
is JetCallExpression -> { else -> error("Unknown parent for JetOperationReferenceExpression: $parent")
if ((parent.parent as JetQualifiedExpression).operationSign == JetTokens.SAFE_ACCESS) }
CallTypeAndReceiver.SAFE(receiverExpression)
else
CallTypeAndReceiver.DOT(receiverExpression)
} }
is JetQualifiedExpression -> { is JetNameReferenceExpression -> {
if (parent.operationSign == JetTokens.SAFE_ACCESS) if (receiverExpression == null) {
CallTypeAndReceiver.SAFE(receiverExpression) return CallTypeAndReceiver.DEFAULT
else }
CallTypeAndReceiver.DOT(receiverExpression)
return when (parent) {
is JetCallExpression -> {
if ((parent.parent as JetQualifiedExpression).operationSign == JetTokens.SAFE_ACCESS)
CallTypeAndReceiver.SAFE(receiverExpression)
else
CallTypeAndReceiver.DOT(receiverExpression)
}
is JetQualifiedExpression -> {
if (parent.operationSign == JetTokens.SAFE_ACCESS)
CallTypeAndReceiver.SAFE(receiverExpression)
else
CallTypeAndReceiver.DOT(receiverExpression)
}
else -> error("Unknown parent for JetNameReferenceExpression with receiver: $parent")
}
} }
is JetUnaryExpression -> CallTypeAndReceiver.UNARY(receiverExpression) else -> return UNKNOWN
else -> error("Unknown parent for expression with receiver: $parent")
} }
} }
} }
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
import org.jetbrains.kotlin.idea.core.getResolutionScope import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.idea.core.isVisible import org.jetbrains.kotlin.idea.core.isVisible
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
import org.jetbrains.kotlin.idea.util.CallType
import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver
import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetPsiUtil import org.jetbrains.kotlin.psi.JetPsiUtil
@@ -116,9 +117,12 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
private val ERRORS = setOf(Errors.UNRESOLVED_REFERENCE, Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER) private val ERRORS = setOf(Errors.UNRESOLVED_REFERENCE, Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER)
public fun computeSuggestions(element: JetSimpleNameExpression): Collection<DeclarationDescriptor> { public fun computeSuggestions(element: JetSimpleNameExpression): Collection<DeclarationDescriptor> {
if (!element.isValid()) return listOf() if (!element.isValid()) return emptyList()
val file = element.getContainingFile() as? JetFile ?: return listOf() val file = element.getContainingFile() as? JetFile ?: return emptyList()
val callType = CallTypeAndReceiver.detect(element).callType
if (callType is CallType.UNKNOWN) return emptyList()
var referenceName = element.getReferencedName() var referenceName = element.getReferencedName()
if (element.getIdentifier() == null) { if (element.getIdentifier() == null) {
@@ -127,20 +131,18 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
referenceName = conventionName.asString() referenceName = conventionName.asString()
} }
} }
if (referenceName.isEmpty()) return listOf() if (referenceName.isEmpty()) return emptyList()
val searchScope = getResolveScope(file) val searchScope = getResolveScope(file)
val bindingContext = element.analyze(BodyResolveMode.PARTIAL) val bindingContext = element.analyze(BodyResolveMode.PARTIAL)
val diagnostics = bindingContext.getDiagnostics().forElement(element) val diagnostics = bindingContext.getDiagnostics().forElement(element)
if (!diagnostics.any { it.getFactory() in ERRORS }) return listOf() if (!diagnostics.any { it.getFactory() in ERRORS }) return emptyList()
val resolutionScope = element.getResolutionScope(bindingContext, file.getResolutionFacade()) val resolutionScope = element.getResolutionScope(bindingContext, file.getResolutionFacade())
val containingDescriptor = resolutionScope.ownerDescriptor val containingDescriptor = resolutionScope.ownerDescriptor
val callType = CallTypeAndReceiver.detect(element).callType
fun isVisible(descriptor: DeclarationDescriptor): Boolean { fun isVisible(descriptor: DeclarationDescriptor): Boolean {
if (!callType.descriptorKindFilter.accepts(descriptor)) return false if (!callType.descriptorKindFilter.accepts(descriptor)) return false
+6
View File
@@ -0,0 +1,6 @@
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
// ERROR: Unresolved reference: @String
fun refer() {
val v1 = this@String<caret>
}
@@ -478,6 +478,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("notForThisLabel.kt")
public void testNotForThisLabel() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/notForThisLabel.kt");
doTest(fileName);
}
@TestMetadata("sAMConstructorFromLambda.kt") @TestMetadata("sAMConstructorFromLambda.kt")
public void testSAMConstructorFromLambda() throws Exception { public void testSAMConstructorFromLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/sAMConstructorFromLambda.kt"); String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/sAMConstructorFromLambda.kt");