diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt index 9d493f82fdb..fd6cad4b22d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt @@ -21,6 +21,8 @@ import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.psi.KtClassBody +import org.jetbrains.kotlin.psi.KtObjectDeclaration +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType class RemoveEmptyClassBodyInspection : IntentionBasedInspection(RemoveEmptyClassBodyIntention::class), CleanupLocalInspectionTool { override val problemHighlightType: ProblemHighlightType @@ -30,5 +32,10 @@ class RemoveEmptyClassBodyInspection : IntentionBasedInspection(Rem class RemoveEmptyClassBodyIntention : SelfTargetingOffsetIndependentIntention(KtClassBody::class.java, "Remove empty class body") { override fun applyTo(element: KtClassBody, editor: Editor?) = element.delete() - override fun isApplicableTo(element: KtClassBody) = element.text.replace("{", "").replace("}", "").isBlank() + override fun isApplicableTo(element: KtClassBody): Boolean { + element.getStrictParentOfType()?.let { + if (it.isObjectLiteral()) return false + } + return element.text.replace("{", "").replace("}", "").isBlank() + } } \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/anonymousInterfaceObject.kt b/idea/testData/intentions/removeEmptyClassBody/anonymousInterfaceObject.kt new file mode 100644 index 00000000000..a76524090ca --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/anonymousInterfaceObject.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +interface Foo + +fun test() { + val value = object : Foo {} +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/anonymousObject.kt b/idea/testData/intentions/removeEmptyClassBody/anonymousObject.kt new file mode 100644 index 00000000000..cc93c44294a --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/anonymousObject.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false + +fun test() { + object {} +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/innerClass.kt b/idea/testData/intentions/removeEmptyClassBody/innerClass.kt new file mode 100644 index 00000000000..1511b74dfbb --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/innerClass.kt @@ -0,0 +1,3 @@ +object Foo { + class Bar {} +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/innerClass.kt.after b/idea/testData/intentions/removeEmptyClassBody/innerClass.kt.after new file mode 100644 index 00000000000..c9aed29c087 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/innerClass.kt.after @@ -0,0 +1,3 @@ +object Foo { + class Bar +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedAnonymous.kt b/idea/testData/intentions/removeEmptyClassBody/nestedAnonymous.kt new file mode 100644 index 00000000000..982571d54c4 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedAnonymous.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +object Foo { + fun test() { + object {} + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/object.kt b/idea/testData/intentions/removeEmptyClassBody/object.kt new file mode 100644 index 00000000000..3aace11bb33 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/object.kt @@ -0,0 +1,4 @@ +object Foo { + + +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/object.kt.after b/idea/testData/intentions/removeEmptyClassBody/object.kt.after new file mode 100644 index 00000000000..d37c1045635 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/object.kt.after @@ -0,0 +1 @@ +object Foo \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 3e859633134..01d4458f55f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10446,6 +10446,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyClassBody"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("anonymousInterfaceObject.kt") + public void testAnonymousInterfaceObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/anonymousInterfaceObject.kt"); + doTest(fileName); + } + + @TestMetadata("anonymousObject.kt") + public void testAnonymousObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/anonymousObject.kt"); + doTest(fileName); + } + @TestMetadata("emptyClass.kt") public void testEmptyClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/emptyClass.kt"); @@ -10464,11 +10476,29 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("innerClass.kt") + public void testInnerClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/innerClass.kt"); + doTest(fileName); + } + + @TestMetadata("nestedAnonymous.kt") + public void testNestedAnonymous() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedAnonymous.kt"); + doTest(fileName); + } + @TestMetadata("noneEmptyClass.kt") public void testNoneEmptyClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/noneEmptyClass.kt"); doTest(fileName); } + + @TestMetadata("object.kt") + public void testObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/object.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall")