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
This commit is contained in:
@@ -39,7 +39,8 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(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 {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class TestClass(val <caret>text: String) {
|
||||
|
||||
fun foo() {
|
||||
|
||||
}
|
||||
|
||||
val p1 = 0
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class TestClass(text: String) {
|
||||
|
||||
fun foo() {
|
||||
|
||||
}
|
||||
|
||||
val text = text
|
||||
val p1 = 0
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class TestClass(val <caret>text: String) {
|
||||
|
||||
val p1 = 0
|
||||
|
||||
fun foo() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class TestClass(text: String) {
|
||||
|
||||
val text = text
|
||||
val p1 = 0
|
||||
|
||||
fun foo() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user