diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt index fd6cad4b22d..ad69000d226 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyClassBodyIntention.kt @@ -20,8 +20,11 @@ import com.intellij.codeInspection.CleanupLocalInspectionTool import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection +import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtClassBody import org.jetbrains.kotlin.psi.KtObjectDeclaration +import org.jetbrains.kotlin.psi.KtSecondaryConstructor +import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType class RemoveEmptyClassBodyInspection : IntentionBasedInspection(RemoveEmptyClassBodyIntention::class), CleanupLocalInspectionTool { @@ -36,6 +39,11 @@ class RemoveEmptyClassBodyIntention : SelfTargetingOffsetIndependentIntention()?.let { if (it.isObjectLiteral()) return false } + + element.getStrictParentOfType()?.let { + if (!it.isTopLevel() && it.getNextSiblingIgnoringWhitespaceAndComments() is KtSecondaryConstructor) return false + } + return element.text.replace("{", "").replace("}", "").isBlank() } } \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt new file mode 100644 index 00000000000..3678eb8971b --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt @@ -0,0 +1,10 @@ +class A(val x: String) { + + class C {} + + fun foo() { + } + + constructor(x: String, y: Int) : this(x) { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt.after b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt.after new file mode 100644 index 00000000000..296f460e9f2 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt.after @@ -0,0 +1,10 @@ +class A(val x: String) { + + class C + + fun foo() { + } + + constructor(x: String, y: Int) : this(x) { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt new file mode 100644 index 00000000000..c0f74f63229 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt @@ -0,0 +1,9 @@ +class A(val x: String) { + + class C {} + + val foo = 1 + + constructor(x: String, y: Int) : this(x) { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt.after b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt.after new file mode 100644 index 00000000000..195793b6aae --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt.after @@ -0,0 +1,9 @@ +class A(val x: String) { + + class C + + val foo = 1 + + constructor(x: String, y: Int) : this(x) { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor.kt b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor.kt new file mode 100644 index 00000000000..5bcbbee0b00 --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +class A(val x: String) { + + class C {} + + constructor(x: String, y: Int) : this(x) { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor2.kt b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor2.kt new file mode 100644 index 00000000000..b3d49e42aae --- /dev/null +++ b/idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor2.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false + +class A(val x: String) { + + class C {} + + // comments + + constructor(x: String, y: Int) : this(x) { + } +} \ 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 6e3f2cbfe64..088a5645565 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10936,6 +10936,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("nestedClassFollowedByFunction.kt") + public void testNestedClassFollowedByFunction() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByFunction.kt"); + doTest(fileName); + } + + @TestMetadata("nestedClassFollowedByMember.kt") + public void testNestedClassFollowedByMember() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedByMember.kt"); + doTest(fileName); + } + + @TestMetadata("nestedClassFollowedBySecondaryConstructor.kt") + public void testNestedClassFollowedBySecondaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor.kt"); + doTest(fileName); + } + + @TestMetadata("nestedClassFollowedBySecondaryConstructor2.kt") + public void testNestedClassFollowedBySecondaryConstructor2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedClassFollowedBySecondaryConstructor2.kt"); + doTest(fileName); + } + @TestMetadata("noneEmptyClass.kt") public void testNoneEmptyClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/noneEmptyClass.kt");