KT-14890 Fix the problem with RemoveEmptyClassBodyIntention for nested class followed by a secondary constructor

#KT-14890 Fixed
This commit is contained in:
shiraji
2016-11-28 06:48:31 +09:00
committed by Mikhail Glukhikh
parent b7f27b86fe
commit ba92dcb737
8 changed files with 90 additions and 0 deletions
@@ -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<KtClassBody>(RemoveEmptyClassBodyIntention::class), CleanupLocalInspectionTool {
@@ -36,6 +39,11 @@ class RemoveEmptyClassBodyIntention : SelfTargetingOffsetIndependentIntention<Kt
element.getStrictParentOfType<KtObjectDeclaration>()?.let {
if (it.isObjectLiteral()) return false
}
element.getStrictParentOfType<KtClass>()?.let {
if (!it.isTopLevel() && it.getNextSiblingIgnoringWhitespaceAndComments() is KtSecondaryConstructor) return false
}
return element.text.replace("{", "").replace("}", "").isBlank()
}
}
@@ -0,0 +1,10 @@
class A(val x: String) {
class C {<caret>}
fun foo() {
}
constructor(x: String, y: Int) : this(x) {
}
}
@@ -0,0 +1,10 @@
class A(val x: String) {
class C
fun foo() {
}
constructor(x: String, y: Int) : this(x) {
}
}
@@ -0,0 +1,9 @@
class A(val x: String) {
class C {<caret>}
val foo = 1
constructor(x: String, y: Int) : this(x) {
}
}
@@ -0,0 +1,9 @@
class A(val x: String) {
class C
val foo = 1
constructor(x: String, y: Int) : this(x) {
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
class A(val x: String) {
class C {<caret>}
constructor(x: String, y: Int) : this(x) {
}
}
@@ -0,0 +1,11 @@
// IS_APPLICABLE: false
class A(val x: String) {
class C {<caret>}
// comments
constructor(x: String, y: Int) : this(x) {
}
}
@@ -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");