Skip to content

Commit 24476fc

Browse files
committed
Fix MoveProperty intention: better property placement
Property is placed now before first property, or at the beginning of class body if no properties found. #KT-18044 Fixed
1 parent ca124d1 commit 24476fc

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

idea/src/org/jetbrains/kotlin/idea/intentions/MovePropertyToClassBodyIntention.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(KtP
3939
val propertyDeclaration = KtPsiFactory(element)
4040
.createProperty("${element.valOrVarKeyword?.text} ${element.name} = ${element.name}")
4141

42-
parentClass.addDeclaration(propertyDeclaration).apply {
42+
val firstProperty = parentClass.getProperties().firstOrNull()
43+
parentClass.addDeclarationBefore(propertyDeclaration, firstProperty).apply {
4344
val propertyModifierList = element.modifierList?.copy() as? KtModifierList
4445
propertyModifierList?.let { modifierList?.replace(it) ?: addBefore(it, firstChild) }
4546
modifierList?.annotationEntries?.forEach {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class TestClass(val <caret>text: String) {
2+
3+
fun foo() {
4+
5+
}
6+
7+
val p1 = 0
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class TestClass(text: String) {
2+
3+
fun foo() {
4+
5+
}
6+
7+
val text = text
8+
val p1 = 0
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class TestClass(val <caret>text: String) {
2+
3+
val p1 = 0
4+
5+
fun foo() {
6+
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class TestClass(text: String) {
2+
3+
val text = text
4+
val p1 = 0
5+
6+
fun foo() {
7+
8+
}
9+
}

idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11573,6 +11573,18 @@ public void testAnnotationWithUseSite() throws Exception {
1157311573
doTest(fileName);
1157411574
}
1157511575

11576+
@TestMetadata("location1.kt")
11577+
public void testLocation1() throws Exception {
11578+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/location1.kt");
11579+
doTest(fileName);
11580+
}
11581+
11582+
@TestMetadata("location2.kt")
11583+
public void testLocation2() throws Exception {
11584+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/location2.kt");
11585+
doTest(fileName);
11586+
}
11587+
1157611588
@TestMetadata("parameterAnnotation.kt")
1157711589
public void testParameterAnnotation() throws Exception {
1157811590
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/movePropertyToClassBody/parameterAnnotation.kt");

0 commit comments

Comments
 (0)