From 7be54108779e453c25549073c99ea41d33dc42ce Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 20 Dec 2019 13:13:25 +0900 Subject: [PATCH] Add a quick fix for NO_CONSTRUCTOR error on 'expect' annotation entry #KT-20718 Fixed --- .../idea/quickfix/AddDefaultConstructorFix.kt | 27 +++++++++++++------ .../addDefaultConstructor/expectAnnotation.kt | 8 ++++++ .../expectAnnotation.kt.after | 8 ++++++ .../expectAnnotation2.kt | 8 ++++++ .../expectAnnotation2.kt.after | 8 ++++++ .../expectAnnotation3.kt | 11 ++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 15 +++++++++++ 7 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt create mode 100644 idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt.after create mode 100644 idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt create mode 100644 idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt.after create mode 100644 idea/testData/quickfix/addDefaultConstructor/expectAnnotation3.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddDefaultConstructorFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddDefaultConstructorFix.kt index dfa2715ff4d..a7db6ac1700 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddDefaultConstructorFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddDefaultConstructorFix.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType +import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypes import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils @@ -36,14 +36,25 @@ class AddDefaultConstructorFix(expectClass: KtClass) : KotlinQuickFixAction? { - val argumentList = diagnostic.psiElement as? KtValueArgumentList ?: return null - if (argumentList.arguments.isNotEmpty()) return null - val derivedClass = argumentList.getStrictParentOfType() ?: return null - val context = derivedClass.analyze() - val baseTypeCallEntry = derivedClass.superTypeListEntries.asSequence().filterIsInstance().firstOrNull() - ?: return null - val baseClass = superTypeEntryToClass(baseTypeCallEntry, context) ?: return null + val element = diagnostic.psiElement + if (element is KtValueArgumentList && element.arguments.isNotEmpty()) return null + val parent = element.getParentOfTypes(true, KtClassOrObject::class.java, KtAnnotationEntry::class.java) ?: return null + val context by lazy { parent.analyze() } + val baseClass = when (parent) { + is KtClassOrObject -> parent.superTypeListEntries.asSequence().filterIsInstance().firstOrNull()?.let { + superTypeEntryToClass(it, context) + } + is KtAnnotationEntry -> annotationEntryToClass(parent, context) + else -> null + } ?: return null return AddDefaultConstructorFix(baseClass) } } diff --git a/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt new file mode 100644 index 00000000000..3580a776721 --- /dev/null +++ b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt @@ -0,0 +1,8 @@ +// "Add default constructor to expect class" "true" +// ENABLE_MULTIPLATFORM +// ERROR: Expected annotation class 'Foo' has no actual declaration in module light_idea_test_case for JVM + +expect annotation class Foo + +@Foo +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt.after b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt.after new file mode 100644 index 00000000000..684ac4d867b --- /dev/null +++ b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt.after @@ -0,0 +1,8 @@ +// "Add default constructor to expect class" "true" +// ENABLE_MULTIPLATFORM +// ERROR: Expected annotation class 'Foo' has no actual declaration in module light_idea_test_case for JVM + +expect annotation class Foo() + +@Foo +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt new file mode 100644 index 00000000000..1caeaaff01f --- /dev/null +++ b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt @@ -0,0 +1,8 @@ +// "Add default constructor to expect class" "true" +// ENABLE_MULTIPLATFORM +// ERROR: Expected annotation class 'Foo' has no actual declaration in module light_idea_test_case for JVM + +expect annotation class Foo + +@Foo() +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt.after b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt.after new file mode 100644 index 00000000000..efe7c37bade --- /dev/null +++ b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt.after @@ -0,0 +1,8 @@ +// "Add default constructor to expect class" "true" +// ENABLE_MULTIPLATFORM +// ERROR: Expected annotation class 'Foo' has no actual declaration in module light_idea_test_case for JVM + +expect annotation class Foo() + +@Foo() +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/addDefaultConstructor/expectAnnotation3.kt b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation3.kt new file mode 100644 index 00000000000..c3f25dace59 --- /dev/null +++ b/idea/testData/quickfix/addDefaultConstructor/expectAnnotation3.kt @@ -0,0 +1,11 @@ +// "Add default constructor to expect class" "false" +// ENABLE_MULTIPLATFORM +// ACTION: Make internal +// ACTION: Make private +// ACTION: Remove constructor call +// ERROR: Expected annotation class 'Foo' has no actual declaration in module light_idea_test_case for JVM +// ERROR: This class does not have a constructor +expect annotation class Foo + +@Foo("") +fun bar() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 992f87a35f9..db7e5251822 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -550,6 +550,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/addDefaultConstructor/expect.kt"); } + @TestMetadata("expectAnnotation.kt") + public void testExpectAnnotation() throws Exception { + runTest("idea/testData/quickfix/addDefaultConstructor/expectAnnotation.kt"); + } + + @TestMetadata("expectAnnotation2.kt") + public void testExpectAnnotation2() throws Exception { + runTest("idea/testData/quickfix/addDefaultConstructor/expectAnnotation2.kt"); + } + + @TestMetadata("expectAnnotation3.kt") + public void testExpectAnnotation3() throws Exception { + runTest("idea/testData/quickfix/addDefaultConstructor/expectAnnotation3.kt"); + } + @TestMetadata("expectInterface.kt") public void testExpectInterface() throws Exception { runTest("idea/testData/quickfix/addDefaultConstructor/expectInterface.kt");