Remove unnecessary constructor keyword intention (KT-29143)

#KT-29143 Fixed
This commit is contained in:
Felix Guo
2019-01-17 01:28:58 +03:00
committed by Nikolay Krasko
parent c10eadfa77
commit 185d0c6165
24 changed files with 145 additions and 0 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.addRemoveModifier.addModifier
@@ -36,6 +37,13 @@ class KtPrimaryConstructor : KtConstructor<KtPrimaryConstructor> {
return getConstructorKeyword() ?: addBefore(KtPsiFactory(this).createConstructorKeyword(), valueParameterList!!)
}
fun removeRedundantConstructorKeywordAndSpace() {
getConstructorKeyword()?.delete()
if (prevSibling is PsiWhiteSpace) {
prevSibling.delete()
}
}
override fun addModifier(modifier: KtModifierKeywordToken) {
val modifierList = modifierList
if (modifierList != null) {
@@ -1394,6 +1394,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveConstructorKeywordIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPrimaryConstructorToSecondaryIntention</className>
<category>Kotlin</category>
@@ -0,0 +1 @@
class Foo(x: Int, y: Int)
@@ -0,0 +1 @@
class Foo constructor(x: Int, y: Int)
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes a redundant constructor keyword for primary constructors.
</body>
</html>
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class RemoveConstructorKeywordIntention : SelfTargetingIntention<KtPrimaryConstructor>(
KtPrimaryConstructor::class.java,
"Remove constructor keyword"
) {
override fun applyTo(element: KtPrimaryConstructor, editor: Editor?) {
element.removeRedundantConstructorKeywordAndSpace()
}
override fun isApplicableTo(element: KtPrimaryConstructor, caretOffset: Int): Boolean {
if (element.containingClassOrObject !is KtClass) return false
if (element.getConstructorKeyword() == null) return false
return element.modifierList == null
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveConstructorKeywordIntention
@@ -0,0 +1 @@
abstract class Base <caret>constructor(val x: Int)
@@ -0,0 +1 @@
abstract class Base(val x: Int)
@@ -0,0 +1,5 @@
annotation class Ann
class AnnotatedParam <caret>constructor(@Ann x: Double) {
val y = x
}
@@ -0,0 +1,5 @@
annotation class Ann
class AnnotatedParam(@Ann x: Double) {
val y = x
}
@@ -0,0 +1 @@
annotation class Ann <caret>constructor(val i: Int)
@@ -0,0 +1 @@
annotation class Ann(val i: Int)
@@ -0,0 +1,12 @@
/**
* An important class
*/
class WithComments
/**
* Some nasty constructor
*/
constructor(
/* First parameter */
val first: Int,<caret>
/* Second Parameter */
val second: Double)
@@ -0,0 +1,11 @@
/**
* An important class
*/
class WithComments
/**
* Some nasty constructor
*/(
/* First parameter */
val first: Int,
/* Second Parameter */
val second: Double)
@@ -0,0 +1 @@
data class Data <caret>constructor(val value: String)
@@ -0,0 +1 @@
data class Data(val value: String)
@@ -0,0 +1 @@
class InParameters constructor(val x: Int, val y: Int = 7, private <caret>val z: Int = 13)
@@ -0,0 +1 @@
class InParameters(val x: Int, val y: Int = 7, private val z: Int = 13)
@@ -0,0 +1 @@
class VarargVal <caret>constructor(vararg val param: String)
@@ -0,0 +1 @@
class VarargVal(vararg val param: String)
@@ -0,0 +1 @@
class WithProperties constructor<caret>(val x: Int, val y: Int = 7, private val z: Int = 13)
@@ -0,0 +1 @@
class WithProperties(val x: Int, val y: Int = 7, private val z: Int = 13)
@@ -13257,6 +13257,59 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeConstructorKeyword")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveConstructorKeyword extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("abstractClass.kt")
public void testAbstractClass() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/abstractClass.kt");
}
public void testAllFilesPresentInRemoveConstructorKeyword() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeConstructorKeyword"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("annotatedParam.kt")
public void testAnnotatedParam() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/annotatedParam.kt");
}
@TestMetadata("annotationClass.kt")
public void testAnnotationClass() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/annotationClass.kt");
}
@TestMetadata("comments.kt")
public void testComments() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/comments.kt");
}
@TestMetadata("dataClass.kt")
public void testDataClass() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/dataClass.kt");
}
@TestMetadata("inParameters.kt")
public void testInParameters() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/inParameters.kt");
}
@TestMetadata("varargVal.kt")
public void testVarargVal() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/varargVal.kt");
}
@TestMetadata("withProperties.kt")
public void testWithProperties() throws Exception {
runTest("idea/testData/intentions/removeConstructorKeyword/withProperties.kt");
}
}
@TestMetadata("idea/testData/intentions/removeCurlyBracesFromTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)