KTIJ-31409 [kotlin] Handle UNRESOLVED_REFERENCE_WRONG_RECEIVER diagnostic in K2 Mode auto-import implementation

`ImportQuickFixFactories` is responsible for the "on the fly" auto-import,
and `KotlinDiagnosticHighlightVisitor` is responsible for providing the
auto-import options in the regular Alt+Enter menu

^KTIJ-31409 Fixed

GitOrigin-RevId: c1257fba60379026336e696b20d62d38e61738e8
This commit is contained in:
Roman Golyshev
2024-09-26 14:06:54 +00:00
committed by intellij-monorepo-bot
parent aac8c933a6
commit 67e86f75f7
11 changed files with 88 additions and 0 deletions
@@ -582,6 +582,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
override val importOnTheFlyList: KotlinQuickFixesList = KtQuickFixesListBuilder.registerPsiQuickFix {
registerFactory(ImportQuickFixFactories.unresolvedReferenceFactory)
registerFactory(ImportQuickFixFactories.unresolvedReferenceWrongReceiverFactory)
registerFactory(ImportQuickFixFactories.invisibleReferenceFactory)
}
}
@@ -16,4 +16,11 @@ object ImportQuickFixFactories {
val unresolvedReferenceFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.UnresolvedReference ->
ImportQuickFixProvider.getFixes(diagnostic.psi)
}
/**
* See KDoc for [unresolvedReferenceFactory].
*/
val unresolvedReferenceWrongReceiverFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.UnresolvedReferenceWrongReceiver ->
ImportQuickFixProvider.getFixes(diagnostic.psi)
}
}
@@ -999,6 +999,11 @@ public abstract class HighLevelQuickFixMultiFileTestGenerated extends AbstractHi
public void testWithSmartCastQualifier() throws Exception {
runTest("../../../idea/tests/testData/quickfix/autoImports/withSmartCastQualifier.before.Main.kt");
}
@TestMetadata("wrongReceiverType.test")
public void testWrongReceiverType() throws Exception {
runTest("../../../idea/tests/testData/quickfix/autoImports/wrongReceiverType.test");
}
}
}
@@ -184,4 +184,9 @@ public class K2AutoImportTestGenerated extends AbstractK2AutoImportTest {
public void testWithoutAutoImport() throws Exception {
runTest("../../idea/tests/testData/editor/autoImport/withoutAutoImport/");
}
@TestMetadata("wrongReceiverType")
public void testWrongReceiverType() throws Exception {
runTest("../../idea/tests/testData/editor/autoImport/wrongReceiverType/");
}
}
@@ -159,6 +159,7 @@ class KotlinDiagnosticHighlightVisitor : HighlightVisitor {
if (
diagnostic is KaFirDiagnostic.UnresolvedReference ||
diagnostic is KaFirDiagnostic.UnresolvedReferenceWrongReceiver ||
diagnostic is KaFirDiagnostic.UnresolvedImport
) {
psiElement.registerKotlinUnresolvedReferenceKind(KotlinUnresolvedReferenceKind.Regular)
@@ -167,6 +168,7 @@ class KotlinDiagnosticHighlightVisitor : HighlightVisitor {
if (
diagnostic is KaFirDiagnostic.UnresolvedImport ||
diagnostic is KaFirDiagnostic.UnresolvedReference ||
diagnostic is KaFirDiagnostic.UnresolvedReferenceWrongReceiver ||
diagnostic is KaFirDiagnostic.DelegateSpecialFunctionMissing ||
diagnostic is KaFirDiagnostic.DelegateSpecialFunctionNoneApplicable
) {
@@ -184,4 +184,9 @@ public class K1AutoImportTestGenerated extends AbstractK1AutoImportTest {
public void testWithoutAutoImport() throws Exception {
runTest("testData/editor/autoImport/withoutAutoImport/");
}
@TestMetadata("wrongReceiverType")
public void testWrongReceiverType() throws Exception {
runTest("testData/editor/autoImport/wrongReceiverType/");
}
}
@@ -1111,6 +1111,11 @@ public abstract class QuickFixMultiFileTestGenerated extends AbstractQuickFixMul
public void testWithSmartCastQualifier() throws Exception {
runTest("testData/quickfix/autoImports/withSmartCastQualifier.before.Main.kt");
}
@TestMetadata("wrongReceiverType.test")
public void testWrongReceiverType() throws Exception {
runTest("testData/quickfix/autoImports/wrongReceiverType.test");
}
}
}
@@ -0,0 +1,5 @@
package dependency
class FromDependency
fun FromDependency.extension() {}
@@ -0,0 +1,11 @@
package main
import dependency.FromDependency
class UnrelatedClass
fun UnrelatedClass.extension()
fun test(f: FromDependency) {
f.extension() // UNRESOLVED_REFERENCE_WRONG_RECEIVER
}
@@ -0,0 +1,12 @@
package main
import dependency.FromDependency
import dependency.extension
class UnrelatedClass
fun UnrelatedClass.extension()
fun test(f: FromDependency) {
f.extension() // UNRESOLVED_REFERENCE_WRONG_RECEIVER
}
@@ -0,0 +1,30 @@
// FILE: first.before.kt
// "Import extension function 'String.extension'" "true"
// ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: <br>public fun Int.extension(): Unit defined in test in file first.before.kt
package test
fun Int.extension() {}
fun test(str: String) {
str.<caret>extension()
}
// FILE: second.kt
package dependency
fun String.extension() {}
// FILE: first.after.kt
// "Import extension function 'String.extension'" "true"
// ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: <br>public fun Int.extension(): Unit defined in test in file first.before.kt
package test
import dependency.extension
fun Int.extension() {}
fun test(str: String) {
str.<caret>extension()
}