Skip to content

Commit 8a73a1d

Browse files
cypressiousmglukhikh
authored andcommitted
Implement Intention + Inspection to remove empty secondary constructor body #KT-14326 Fixed
1 parent 495dd36 commit 8a73a1d

File tree

10 files changed

+100
-0
lines changed

10 files changed

+100
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
constructor(a: Int)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
constructor(a: Int) <spot>{
2+
3+
}</spot>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
This intention removes empty bodies of secondary constructors
4+
</body>
5+
</html>

idea/src/META-INF/plugin.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,11 @@
14201420
<category>Kotlin</category>
14211421
</intentionAction>
14221422

1423+
<intentionAction>
1424+
<className>org.jetbrains.kotlin.idea.intentions.RemoveEmptySecondaryConstructorBodyIntention</className>
1425+
<category>Kotlin</category>
1426+
</intentionAction>
1427+
14231428
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
14241429
displayName="Object literal can be converted to lambda"
14251430
groupName="Kotlin"
@@ -1902,6 +1907,14 @@
19021907
language="kotlin"
19031908
/>
19041909

1910+
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveEmptySecondaryConstructorBodyInspection"
1911+
displayName="Remove empty constructor body"
1912+
groupName="Kotlin"
1913+
enabledByDefault="true"
1914+
level="WEAK WARNING"
1915+
language="kotlin"
1916+
/>
1917+
19051918
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
19061919

19071920
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.idea.intentions
18+
19+
import com.intellij.codeInspection.CleanupLocalInspectionTool
20+
import com.intellij.codeInspection.ProblemHighlightType
21+
import com.intellij.openapi.editor.Editor
22+
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
23+
import org.jetbrains.kotlin.psi.KtBlockExpression
24+
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
25+
26+
class RemoveEmptySecondaryConstructorBodyInspection : IntentionBasedInspection<KtBlockExpression>(RemoveEmptySecondaryConstructorBodyIntention::class), CleanupLocalInspectionTool {
27+
override val problemHighlightType: ProblemHighlightType
28+
get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
29+
}
30+
31+
class RemoveEmptySecondaryConstructorBodyIntention() : SelfTargetingOffsetIndependentIntention<KtBlockExpression>(KtBlockExpression::class.java, "Remove empty constructor body") {
32+
33+
override fun applyTo(element: KtBlockExpression, editor: Editor?) = element.delete()
34+
35+
override fun isApplicableTo(element: KtBlockExpression): Boolean {
36+
if(element.parent !is KtSecondaryConstructor) return false
37+
if(element.statements.isNotEmpty()) return false
38+
39+
return element.text.replace("{", "").replace("}", "").isBlank()
40+
}
41+
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.jetbrains.kotlin.idea.intentions.RemoveEmptySecondaryConstructorBodyIntention
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// IS_APPLICABLE: false
2+
3+
class Foo() {
4+
constructor(a: Int) : this() <caret>{
5+
//comment
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Foo() {
2+
constructor(a: Int) : this() <caret>{
3+
}
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo() {
2+
constructor(a: Int) : this()<caret>
3+
}

idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10843,6 +10843,27 @@ public void testSimple() throws Exception {
1084310843
}
1084410844
}
1084510845

10846+
@TestMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody")
10847+
@TestDataPath("$PROJECT_ROOT")
10848+
@RunWith(JUnit3RunnerWithInners.class)
10849+
public static class RemoveEmptySecondaryConstructorBody extends AbstractIntentionTest {
10850+
public void testAllFilesPresentInRemoveEmptySecondaryConstructorBody() throws Exception {
10851+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptySecondaryConstructorBody"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
10852+
}
10853+
10854+
@TestMetadata("comment.kt")
10855+
public void testComment() throws Exception {
10856+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody/comment.kt");
10857+
doTest(fileName);
10858+
}
10859+
10860+
@TestMetadata("simple.kt")
10861+
public void testSimple() throws Exception {
10862+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptySecondaryConstructorBody/simple.kt");
10863+
doTest(fileName);
10864+
}
10865+
}
10866+
1084610867
@TestMetadata("idea/testData/intentions/removeExplicitLambdaParameterTypes")
1084710868
@TestDataPath("$PROJECT_ROOT")
1084810869
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)