diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt index bddb0d6c4b7..63811619a04 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt @@ -13,18 +13,21 @@ import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.containingClass import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter import org.jetbrains.kotlin.resolve.AnnotationChecker import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode - class MovePropertyToClassBodyIntention : SelfTargetingIntention( KtParameter::class.java, KotlinBundle.lazyMessage("move.to.class.body") ) { - override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean = - element.isPropertyParameter() && (element.ownerFunction as KtPrimaryConstructor).isNotContainedInAnnotation() + override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean { + if (!element.isPropertyParameter()) return false + val containingClass = element.containingClass() ?: return false + return !containingClass.isAnnotation() && !containingClass.isData() + } override fun applyTo(element: KtParameter, editor: Editor?) { val parentClass = PsiTreeUtil.getParentOfType(element, KtClass::class.java) ?: return @@ -94,6 +97,4 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention( nextSibling?.delete() // ':' symbol after use site delete() } - - private fun KtPrimaryConstructor.isNotContainedInAnnotation() = !getContainingClassOrObject().isAnnotation() } diff --git a/idea/testData/intentions/movePropertyToClassBody/annotationClass.kt b/idea/testData/intentions/movePropertyToClassBody/annotationClass.kt new file mode 100644 index 00000000000..d00fdbbc327 --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/annotationClass.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +annotation class TestClass(val text: String) \ No newline at end of file diff --git a/idea/testData/intentions/movePropertyToClassBody/dataClass.kt b/idea/testData/intentions/movePropertyToClassBody/dataClass.kt new file mode 100644 index 00000000000..ef4dd9f43a1 --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/dataClass.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +data class TestClass(val text: String) \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 6f50258ed22..01f736f99a9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -12510,11 +12510,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/movePropertyToClassBody"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("annotationClass.kt") + public void testAnnotationClass() throws Exception { + runTest("idea/testData/intentions/movePropertyToClassBody/annotationClass.kt"); + } + @TestMetadata("annotationWithUseSite.kt") public void testAnnotationWithUseSite() throws Exception { runTest("idea/testData/intentions/movePropertyToClassBody/annotationWithUseSite.kt"); } + @TestMetadata("dataClass.kt") + public void testDataClass() throws Exception { + runTest("idea/testData/intentions/movePropertyToClassBody/dataClass.kt"); + } + @TestMetadata("location1.kt") public void testLocation1() throws Exception { runTest("idea/testData/intentions/movePropertyToClassBody/location1.kt");