Add Inspection to remove empty primary constructor #KT-14521 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
ba7f60040a
commit
495dd3641c
+1
@@ -0,0 +1 @@
|
||||
class Foo
|
||||
+1
@@ -0,0 +1 @@
|
||||
class Foo<spot>()</spot>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention removes an empty primary constructor when it would be implicitly available anyway
|
||||
</body>
|
||||
</html>
|
||||
@@ -1415,6 +1415,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||
displayName="Object literal can be converted to lambda"
|
||||
groupName="Kotlin"
|
||||
@@ -1889,6 +1894,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorInspection"
|
||||
displayName="Remove empty primary constructor"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+42
@@ -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.KtPrimaryConstructor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||
|
||||
class RemoveEmptyPrimaryConstructorInspection : IntentionBasedInspection<KtPrimaryConstructor>(RemoveEmptyPrimaryConstructorIntention::class), CleanupLocalInspectionTool {
|
||||
override val problemHighlightType: ProblemHighlightType
|
||||
get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||
}
|
||||
|
||||
class RemoveEmptyPrimaryConstructorIntention : SelfTargetingOffsetIndependentIntention<KtPrimaryConstructor>(KtPrimaryConstructor::class.java, "Remove empty primary constructor") {
|
||||
|
||||
override fun applyTo(element: KtPrimaryConstructor, editor: Editor?) = element.delete()
|
||||
|
||||
override fun isApplicableTo(element: KtPrimaryConstructor) = when {
|
||||
element.valueParameters.isNotEmpty() -> false
|
||||
element.annotations.isNotEmpty() -> false
|
||||
element.modifierList?.text?.isBlank() == false -> false
|
||||
element.containingClass()?.getSecondaryConstructors()?.isNotEmpty() == true -> false
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
annotation class Ann
|
||||
|
||||
class Foo @Ann constructor()<caret>
|
||||
@@ -0,0 +1 @@
|
||||
class Foo constructor()<caret>
|
||||
@@ -0,0 +1 @@
|
||||
class Foo <caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Foo private constructor()<caret>
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Foo()<caret> {
|
||||
constructor(a: Int): this()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
class Foo()<caret>
|
||||
@@ -0,0 +1 @@
|
||||
class Foo<caret>
|
||||
@@ -10804,6 +10804,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveEmptyPrimaryConstructor extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInRemoveEmptyPrimaryConstructor() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyPrimaryConstructor"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("annotation.kt")
|
||||
public void testAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/annotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("keyword.kt")
|
||||
public void testKeyword() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/keyword.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("modifier.kt")
|
||||
public void testModifier() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/modifier.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("secondary.kt")
|
||||
public void testSecondary() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/secondary.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/removeExplicitLambdaParameterTypes")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user