[kotlin] Port ConvertClassToKClassFix to K2

^KTIJ-29487

GitOrigin-RevId: 9388d765fddc4c64076002ef2a01b59dcfbcb4e6
This commit is contained in:
Andrey Cherkasov
2024-09-07 12:51:43 +00:00
committed by intellij-monorepo-bot
parent ede36ff711
commit 61e15459c5
14 changed files with 194 additions and 6 deletions
@@ -0,0 +1,58 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.codeinsight.fixes
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory
import org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtProperty
internal object ConvertClassToKClassFixFactories {
val ArgumentTypeMismatchFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ArgumentTypeMismatch ->
listOfNotNull(createFixIfAvailable(diagnostic.psi, diagnostic.expectedType))
}
val ReturnTypeMismatchFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ReturnTypeMismatch ->
listOfNotNull(createFixIfAvailable(diagnostic.psi, diagnostic.expectedType))
}
val InitializerTypeMismatchFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.InitializerTypeMismatch ->
listOfNotNull(createFixIfAvailable((diagnostic.psi as? KtProperty)?.initializer, diagnostic.expectedType))
}
val AssignmentTypeMismatchFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.AssignmentTypeMismatch ->
listOfNotNull(createFixIfAvailable(diagnostic.psi, diagnostic.expectedType))
}
context(KaSession)
private fun createFixIfAvailable(element: PsiElement?, expectedType: KaType): ConvertClassToKClassFix? {
val dotQualifiedExpression = element as? KtDotQualifiedExpression ?: return null
if (!expectedType.isKClass()) return null
val expressionType = dotQualifiedExpression.expressionType ?: return null
if (!expressionType.isJClass()) return null
val children = dotQualifiedExpression.children
if (children.size != 2) return null
val firstChild = children.first() as? KtExpression ?: return null
val firstChildType = firstChild.expressionType ?: return null
if (!firstChildType.isSubtypeOf(expectedType)) return null
return ConvertClassToKClassFix(dotQualifiedExpression)
}
context(KaSession)
private fun KaType.isKClass(): Boolean = isClassType(StandardClassIds.KClass)
context(KaSession)
private fun KaType.isJClass(): Boolean = isClassType(ClassId.fromString("java/lang/Class"))
}
@@ -145,6 +145,10 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
registerFactory(ExtensionPropertyWithBackingFieldFixFactories.convertToGetterFixFactory)
registerFactory(OverrideDeprecationFixFactories.copyDeprecatedAnnotationFixFactory)
registerFactory(TypeVarianceConflictErrorFixFactories.addUnsafeVarianceAnnotationFixFactory)
registerFactory(ConvertClassToKClassFixFactories.ArgumentTypeMismatchFixFactory)
registerFactory(ConvertClassToKClassFixFactories.ReturnTypeMismatchFixFactory)
registerFactory(ConvertClassToKClassFixFactories.InitializerTypeMismatchFixFactory)
registerFactory(ConvertClassToKClassFixFactories.AssignmentTypeMismatchFixFactory)
}
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -10813,7 +10813,44 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch")
public static class TypeInferenceExpectedTypeMismatch extends AbstractHighLevelQuickFixTest {
@java.lang.Override
@org.jetbrains.annotations.NotNull
public final KotlinPluginMode getPluginMode() {
return KotlinPluginMode.K2;
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("argument.kt")
public void testArgument() throws Exception {
runTest("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch/argument.kt");
}
@TestMetadata("assignment.kt")
public void testAssignment() throws Exception {
runTest("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch/assignment.kt");
}
@TestMetadata("initializer.kt")
public void testInitializer() throws Exception {
runTest("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch/initializer.kt");
}
@TestMetadata("notKClass.kt")
public void testNotKClass() throws Exception {
runTest("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch/notKClass.kt");
}
@TestMetadata("return.kt")
public void testReturn() throws Exception {
runTest("../../../idea/tests/testData/quickfix/typeInferenceExpectedTypeMismatch/return.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/typeOfAnnotationMember")
@@ -17646,9 +17646,29 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("convertClassToKClass1Runtime.kt")
public void testConvertClassToKClass1Runtime() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/convertClassToKClass1Runtime.kt");
@TestMetadata("argument.kt")
public void testArgument() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/argument.kt");
}
@TestMetadata("assignment.kt")
public void testAssignment() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/assignment.kt");
}
@TestMetadata("initializer.kt")
public void testInitializer() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/initializer.kt");
}
@TestMetadata("notKClass.kt")
public void testNotKClass() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/notKClass.kt");
}
@TestMetadata("return.kt")
public void testReturn() throws Exception {
runTest("testData/quickfix/typeInferenceExpectedTypeMismatch/return.kt");
}
}
@@ -8,4 +8,5 @@ class Foo
fun bar(kc: kotlin.reflect.KClass<Foo>) {
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -8,4 +8,5 @@ class Foo
fun bar(kc: kotlin.reflect.KClass<Foo>) {
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,10 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo() {
val clazz: kotlin.reflect.KClass<Foo>
clazz = Foo::class.java<caret>
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,10 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo() {
val clazz: kotlin.reflect.KClass<Foo>
clazz = Foo::class
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,9 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo() {
val clazz: kotlin.reflect.KClass<Foo> = Foo::class.java<caret>
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,9 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo() {
val clazz: kotlin.reflect.KClass<Foo> = Foo::class
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,11 @@
// "Remove '.java'" "false"
// WITH_STDLIB
// ERROR: Type mismatch: inferred type is Class<Foo> but String was expected
fun foo() {
bar(Foo::class.java<caret>)
}
class Foo
fun bar(s: String) {
}
@@ -0,0 +1,9 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo(): kotlin.reflect.KClass<Foo> {
return Foo::class.java<caret>
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -0,0 +1,9 @@
// "Remove '.java'" "true"
// WITH_STDLIB
fun foo(): kotlin.reflect.KClass<Foo> {
return Foo::class
}
class Foo
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertClassToKClassFix
@@ -193,7 +193,7 @@ internal fun MutableTWorkspace.generateK2FixTests() {
model("$idea/quickfix/suspiciousCollectionReassignment", pattern = pattern, isIgnored = true)
model("$idea/quickfix/tooLongCharLiteralToString", pattern = pattern)
model("$idea/quickfix/typeImports", pattern = pattern)
model("$idea/quickfix/typeInferenceExpectedTypeMismatch", pattern = pattern, isIgnored = true)
model("$idea/quickfix/typeInferenceExpectedTypeMismatch", pattern = pattern)
model("$idea/quickfix/typeOfAnnotationMember", pattern = pattern)
model("$idea/quickfix/typeParameters", pattern = pattern)
model("$idea/quickfix/typeProjection", pattern = pattern, isIgnored = true)