From 87d81a75f3a239c2a3c0a45a6a8360dcbc8ada03 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 7 Aug 2018 15:26:18 +0300 Subject: [PATCH] Fix secondary constructor deletion in "sealed class -> object" --- .../ConvertSealedSubClassToObjectFix.kt | 5 +++-- .../convertSubClassWithSecondaryConstructor.kt | 10 ++++++++++ .../convertSubClassWithSecondaryConstructor.kt.after | 10 ++++++++++ .../idea/inspections/LocalInspectionTestGenerated.java | 5 +++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/sealedSubClassToObject/ConvertSealedSubClassToObjectFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/sealedSubClassToObject/ConvertSealedSubClassToObjectFix.kt index dcd7cbb5522..0015f9d092b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/sealedSubClassToObject/ConvertSealedSubClassToObjectFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/sealedSubClassToObject/ConvertSealedSubClassToObjectFix.kt @@ -14,6 +14,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.impl.source.tree.JavaElementType import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.idea.intentions.ConvertSecondaryConstructorToPrimaryIntention import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtPsiFactory @@ -47,9 +48,9 @@ class ConvertSealedSubClassToObjectFix : LocalQuickFix { } private fun KtClass.changeToObject(factory: KtPsiFactory) { - getClassOrInterfaceKeyword()?.replace(factory.createExpression(KtTokens.OBJECT_KEYWORD.value)) - secondaryConstructors.forEach { delete() } + secondaryConstructors.forEach { ConvertSecondaryConstructorToPrimaryIntention().applyTo(it, null) } primaryConstructor?.delete() + getClassOrInterfaceKeyword()?.replace(factory.createExpression(KtTokens.OBJECT_KEYWORD.value)) } private fun KtClass.transformToObject(factory: KtPsiFactory) { diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt new file mode 100644 index 00000000000..ea8a765fbca --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt @@ -0,0 +1,10 @@ +// FIX: Convert sealed sub-class to object +// WITH_RUNTIME + +sealed class Sealed + +class SubSealed : Sealed() { + constructor() { + println("init") + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt.after b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt.after new file mode 100644 index 00000000000..2ff1425460f --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt.after @@ -0,0 +1,10 @@ +// FIX: Convert sealed sub-class to object +// WITH_RUNTIME + +sealed class Sealed + +object SubSealed : Sealed() { + init { + println("init") + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 8494f27981d..42c62dda192 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1696,6 +1696,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithParentheses.kt"); } + @TestMetadata("convertSubClassWithSecondaryConstructor.kt") + public void testConvertSubClassWithSecondaryConstructor() throws Exception { + runTest("idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithSecondaryConstructor.kt"); + } + @TestMetadata("convertSubClassWithoutParentheses.kt") public void testConvertSubClassWithoutParentheses() throws Exception { runTest("idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithoutParentheses.kt");