KT-14241 Add Intention + Inspection to remove empty class body (#968)

This commit is contained in:
Yoshinori Isogai
2016-10-11 02:24:39 +09:00
committed by Dmitry Jemerov
parent 4236a4dd07
commit afe8244183
15 changed files with 117 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports and offers to remove an empty class body
</body>
</html>
@@ -0,0 +1,3 @@
class Foo <spot>{
}</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes an empty class body.
</body>
</html>
+14
View File
@@ -1390,6 +1390,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveEmptyClassBodyIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -1847,6 +1852,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveEmptyClassBodyInspection"
displayName="Replace empty class body"
groupName="Kotlin"
enabledByDefault="true"
cleanupTool="true"
level="INFO"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
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.KtClassBody
class RemoveEmptyClassBodyInspection : IntentionBasedInspection<KtClassBody>(RemoveEmptyClassBodyIntention::class), CleanupLocalInspectionTool {
override val problemHighlightType: ProblemHighlightType
get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
}
class RemoveEmptyClassBodyIntention : SelfTargetingOffsetIndependentIntention<KtClassBody>(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()
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveEmptyClassBodyIntention
@@ -0,0 +1,4 @@
class Foo() {<caret>
}
@@ -0,0 +1 @@
class Foo()
@@ -0,0 +1,4 @@
data class Foo(val bar: Int) {<caret>
}
@@ -0,0 +1 @@
data class Foo(val bar: Int)
@@ -0,0 +1,4 @@
interface Foo {<caret>
}
@@ -0,0 +1 @@
interface Foo
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Foo() {<caret>
// comment
}
@@ -10251,6 +10251,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeEmptyClassBody")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveEmptyClassBody extends AbstractIntentionTest {
public void testAllFilesPresentInRemoveEmptyClassBody() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyClassBody"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("emptyClass.kt")
public void testEmptyClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/emptyClass.kt");
doTest(fileName);
}
@TestMetadata("emptyDataClass.kt")
public void testEmptyDataClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/emptyDataClass.kt");
doTest(fileName);
}
@TestMetadata("emptyInterface.kt")
public void testEmptyInterface() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/emptyInterface.kt");
doTest(fileName);
}
@TestMetadata("noneEmptyClass.kt")
public void testNoneEmptyClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/noneEmptyClass.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)