Fix secondary constructor deletion in "sealed class -> object"

This commit is contained in:
Mikhail Glukhikh
2018-08-07 15:26:18 +03:00
parent 0fb8dd2f75
commit 87d81a75f3
4 changed files with 28 additions and 2 deletions
@@ -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) {
@@ -0,0 +1,10 @@
// FIX: Convert sealed sub-class to object
// WITH_RUNTIME
sealed class Sealed
<caret>class SubSealed : Sealed() {
constructor() {
println("init")
}
}
@@ -0,0 +1,10 @@
// FIX: Convert sealed sub-class to object
// WITH_RUNTIME
sealed class Sealed
object SubSealed : Sealed() {
init {
println("init")
}
}
@@ -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");