From 8a73a1da2c9bea79a53a8b8049649ec4037e3424 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Mon, 31 Oct 2016 23:31:06 +0100 Subject: [PATCH] Implement Intention + Inspection to remove empty secondary constructor body #KT-14326 Fixed --- .../after.kt.template | 1 + .../before.kt.template | 3 ++ .../description.html | 5 +++ idea/src/META-INF/plugin.xml | 13 ++++++ ...eEmptySecondaryConstructorBodyIntention.kt | 42 +++++++++++++++++++ .../.intention | 1 + .../comment.kt | 7 ++++ .../simple.kt | 4 ++ .../simple.kt.after | 3 ++ .../intentions/IntentionTestGenerated.java | 21 ++++++++++ 10 files changed, 100 insertions(+) create mode 100644 idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/description.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptySecondaryConstructorBodyIntention.kt create mode 100644 idea/testData/intentions/removeEmptySecondaryConstructorBody/.intention create mode 100644 idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt create mode 100644 idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt create mode 100644 idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt.after diff --git a/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/after.kt.template new file mode 100644 index 00000000000..51d5bb19a7f --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/after.kt.template @@ -0,0 +1 @@ +constructor(a: Int) \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/before.kt.template new file mode 100644 index 00000000000..f24050c61c9 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/before.kt.template @@ -0,0 +1,3 @@ +constructor(a: Int) { + +} diff --git a/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/description.html b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/description.html new file mode 100644 index 00000000000..0de8b59c8ac --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptySecondaryConstructorBodyIntention/description.html @@ -0,0 +1,5 @@ + + +This intention removes empty bodies of secondary constructors + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 9f6b25e94a8..4700869f3cf 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1420,6 +1420,11 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.RemoveEmptySecondaryConstructorBodyIntention + Kotlin + + + + diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptySecondaryConstructorBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptySecondaryConstructorBodyIntention.kt new file mode 100644 index 00000000000..1eb8e497777 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptySecondaryConstructorBodyIntention.kt @@ -0,0 +1,42 @@ +/* + * 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.KtBlockExpression +import org.jetbrains.kotlin.psi.KtSecondaryConstructor + +class RemoveEmptySecondaryConstructorBodyInspection : IntentionBasedInspection(RemoveEmptySecondaryConstructorBodyIntention::class), CleanupLocalInspectionTool { + override val problemHighlightType: ProblemHighlightType + get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL +} + +class RemoveEmptySecondaryConstructorBodyIntention() : SelfTargetingOffsetIndependentIntention(KtBlockExpression::class.java, "Remove empty constructor body") { + + override fun applyTo(element: KtBlockExpression, editor: Editor?) = element.delete() + + override fun isApplicableTo(element: KtBlockExpression): Boolean { + if(element.parent !is KtSecondaryConstructor) return false + if(element.statements.isNotEmpty()) return false + + return element.text.replace("{", "").replace("}", "").isBlank() + } + +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptySecondaryConstructorBody/.intention b/idea/testData/intentions/removeEmptySecondaryConstructorBody/.intention new file mode 100644 index 00000000000..ba6a03627e9 --- /dev/null +++ b/idea/testData/intentions/removeEmptySecondaryConstructorBody/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.RemoveEmptySecondaryConstructorBodyIntention \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt b/idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt new file mode 100644 index 00000000000..70a209d657e --- /dev/null +++ b/idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +class Foo() { + constructor(a: Int) : this() { + //comment + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt b/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt new file mode 100644 index 00000000000..a2cf627a538 --- /dev/null +++ b/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt @@ -0,0 +1,4 @@ +class Foo() { + constructor(a: Int) : this() { + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt.after b/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt.after new file mode 100644 index 00000000000..521a809f445 --- /dev/null +++ b/idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt.after @@ -0,0 +1,3 @@ +class Foo() { + constructor(a: Int) : this() +} \ 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 bf9cf902ebb..ec5cf3a908b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10843,6 +10843,27 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveEmptySecondaryConstructorBody extends AbstractIntentionTest { + public void testAllFilesPresentInRemoveEmptySecondaryConstructorBody() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptySecondaryConstructorBody"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("comment.kt") + public void testComment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/removeExplicitLambdaParameterTypes") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)