Primary constructor to secondary: always try to build constructor body #KT-14475 Fixed
This commit is contained in:
+21
-21
@@ -89,32 +89,32 @@ class ConvertPrimaryConstructorToSecondaryIntention : SelfTargetingIntention<KtP
|
||||
superTypeEntry.replace(factory.createSuperTypeEntry(superTypeEntry.typeReference!!.text))
|
||||
}
|
||||
}
|
||||
if (element.valueParameters.firstOrNull { it.hasValOrVar() } != null || initializerMap.isNotEmpty()) {
|
||||
val valueParameterInitializers = element.valueParameters.filter { it.hasValOrVar() }.joinToString(separator = "\n") {
|
||||
val valueParameterInitializers = element.valueParameters.filter { it.hasValOrVar() }.joinToString(separator = "\n") {
|
||||
val name = it.name!!
|
||||
"this.$name = $name"
|
||||
}
|
||||
val classBodyInitializers = klass.declarations.filter {
|
||||
(it is KtProperty && initializerMap[it] != null) || it is KtAnonymousInitializer
|
||||
}.joinToString(separator = "\n") {
|
||||
if (it is KtProperty) {
|
||||
val name = it.name!!
|
||||
"this.$name = $name"
|
||||
}
|
||||
val classBodyInitializers = klass.declarations.filter {
|
||||
(it is KtProperty && initializerMap[it] != null) || it is KtAnonymousInitializer
|
||||
}.joinToString(separator = "\n") {
|
||||
if (it is KtProperty) {
|
||||
val name = it.name!!
|
||||
val text = initializerMap[it]
|
||||
if (text != null) {
|
||||
"${THIS_KEYWORD.value}.$name = $text"
|
||||
}
|
||||
else {
|
||||
""
|
||||
}
|
||||
val text = initializerMap[it]
|
||||
if (text != null) {
|
||||
"${THIS_KEYWORD.value}.$name = $text"
|
||||
}
|
||||
else {
|
||||
((it as KtAnonymousInitializer).body as? KtBlockExpression)?.statements?.joinToString(separator = "\n") {
|
||||
it.text
|
||||
} ?: ""
|
||||
""
|
||||
}
|
||||
}
|
||||
blockBody(listOf(valueParameterInitializers, classBodyInitializers)
|
||||
.filter(String::isNotEmpty).joinToString(separator = "\n"))
|
||||
else {
|
||||
((it as KtAnonymousInitializer).body as? KtBlockExpression)?.statements?.joinToString(separator = "\n") {
|
||||
it.text
|
||||
} ?: ""
|
||||
}
|
||||
}
|
||||
val allInitializers = listOf(valueParameterInitializers, classBodyInitializers).filter(String::isNotEmpty)
|
||||
if (allInitializers.isNotEmpty()) {
|
||||
blockBody(allInitializers.joinToString(separator = "\n"))
|
||||
}
|
||||
}.asString()
|
||||
)
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fun println(arg: Int) = arg
|
||||
|
||||
class My<caret>() {
|
||||
val x = 1
|
||||
|
||||
val y = 2
|
||||
|
||||
init {
|
||||
println(x)
|
||||
println(y)
|
||||
}
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
fun println(arg: Int) = arg
|
||||
|
||||
class My {
|
||||
constructor() {
|
||||
println(x)
|
||||
println(y)
|
||||
}
|
||||
|
||||
val x: Int = 1
|
||||
|
||||
val y: Int = 2
|
||||
|
||||
}
|
||||
@@ -4537,6 +4537,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("initWithoutAssignments.kt")
|
||||
public void testInitWithoutAssignments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPrimaryConstructorToSecondary/initWithoutAssignments.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("paramsAndProperties.kt")
|
||||
public void testParamsAndProperties() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPrimaryConstructorToSecondary/paramsAndProperties.kt");
|
||||
|
||||
Reference in New Issue
Block a user