diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt index dba956827bd..cdfb7db4704 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt @@ -39,7 +39,8 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention(KtP val propertyDeclaration = KtPsiFactory(element) .createProperty("${element.valOrVarKeyword?.text} ${element.name} = ${element.name}") - parentClass.addDeclaration(propertyDeclaration).apply { + val firstProperty = parentClass.getProperties().firstOrNull() + parentClass.addDeclarationBefore(propertyDeclaration, firstProperty).apply { val propertyModifierList = element.modifierList?.copy() as? KtModifierList propertyModifierList?.let { modifierList?.replace(it) ?: addBefore(it, firstChild) } modifierList?.annotationEntries?.forEach { diff --git a/idea/testData/intentions/movePropertyToClassBody/location1.kt b/idea/testData/intentions/movePropertyToClassBody/location1.kt new file mode 100644 index 00000000000..7e3fd8a1f63 --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/location1.kt @@ -0,0 +1,8 @@ +class TestClass(val text: String) { + + fun foo() { + + } + + val p1 = 0 +} \ No newline at end of file diff --git a/idea/testData/intentions/movePropertyToClassBody/location1.kt.after b/idea/testData/intentions/movePropertyToClassBody/location1.kt.after new file mode 100644 index 00000000000..14483dd22ee --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/location1.kt.after @@ -0,0 +1,9 @@ +class TestClass(text: String) { + + fun foo() { + + } + + val text = text + val p1 = 0 +} \ No newline at end of file diff --git a/idea/testData/intentions/movePropertyToClassBody/location2.kt b/idea/testData/intentions/movePropertyToClassBody/location2.kt new file mode 100644 index 00000000000..96eb6c94846 --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/location2.kt @@ -0,0 +1,8 @@ +class TestClass(val text: String) { + + val p1 = 0 + + fun foo() { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/movePropertyToClassBody/location2.kt.after b/idea/testData/intentions/movePropertyToClassBody/location2.kt.after new file mode 100644 index 00000000000..bf549615bf6 --- /dev/null +++ b/idea/testData/intentions/movePropertyToClassBody/location2.kt.after @@ -0,0 +1,9 @@ +class TestClass(text: String) { + + val text = text + val p1 = 0 + + fun foo() { + + } +} \ 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 ce0d565504d..b7b275debd3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -11573,6 +11573,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("location1.kt") + public void testLocation1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/location1.kt"); + doTest(fileName); + } + + @TestMetadata("location2.kt") + public void testLocation2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/location2.kt"); + doTest(fileName); + } + @TestMetadata("parameterAnnotation.kt") public void testParameterAnnotation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/parameterAnnotation.kt");