Add intention replacing vararg with array and vice versa #KT-23419 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-05-07 12:11:15 +03:00
committed by Mikhail Glukhikh
parent e933bb8220
commit da92eb63a0
50 changed files with 378 additions and 0 deletions
@@ -0,0 +1 @@
fun foo(<spot>vararg a: String</spot>): Int = a.size
@@ -0,0 +1 @@
fun foo(<spot>a: Array<String></spot>): Int = a.size
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts an array parameter to a <b>vararg</b> parameter.
</body>
</html>
@@ -0,0 +1 @@
fun foo(<spot>a: Array<String></spot>): Int = a.size
@@ -0,0 +1 @@
fun foo(<spot>vararg a: String</spot>): Int = a.size
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts a <b>vararg</b> parameter to an array parameter.
</body>
</html>
+10
View File
@@ -1601,6 +1601,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+10
View File
@@ -1601,6 +1601,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+10
View File
@@ -1602,6 +1602,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+10
View File
@@ -1602,6 +1602,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+10
View File
@@ -1601,6 +1601,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+10
View File
@@ -1601,6 +1601,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2018 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.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.project.builtIns
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ConvertArrayParameterToVarargIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java, "Convert to vararg parameter"
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
val typeReference = element.getChildOfType<KtTypeReference>() ?: return false
if (element.parent.parent is KtFunctionLiteral) return false
if (element.isVarArg) return false
val type = element.descriptor?.type ?: return false
return when {
KotlinBuiltIns.isPrimitiveArray(type) -> {
text = "Convert to vararg parameter"
true
}
KotlinBuiltIns.isArray(type) -> {
val typeArgument = typeReference.typeElement?.typeArgumentsAsTypes?.firstOrNull()
val typeProjection = typeArgument?.parent as? KtTypeProjection
if (typeProjection?.hasModifier(KtTokens.IN_KEYWORD) == false) {
text = if (!typeProjection.hasModifier(KtTokens.OUT_KEYWORD)
&& !KotlinBuiltIns.isPrimitiveType(element.builtIns.getArrayElementType(type))
) {
"Convert to vararg parameter (may break code)"
} else {
"Convert to vararg parameter"
}
true
} else {
false
}
}
else ->
false
}
}
override fun applyTo(element: KtParameter, editor: Editor?) {
val typeReference = element.getChildOfType<KtTypeReference>() ?: return
val type = element.descriptor?.type ?: return
val newType = KotlinBuiltIns.getPrimitiveArrayElementType(type)?.typeName?.asString()
?: typeReference.typeElement?.typeArgumentsAsTypes?.firstOrNull()?.text
?: return
typeReference.replace(KtPsiFactory(element).createType(newType))
element.addModifier(KtTokens.VARARG_KEYWORD)
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2018 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.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ConvertVarargParameterToArrayIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java, "Convert to array parameter"
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
if (element.getChildOfType<KtTypeReference>() == null) return false
return element.isVarArg
}
override fun applyTo(element: KtParameter, editor: Editor?) {
val typeReference = element.getChildOfType<KtTypeReference>() ?: return
val type = element.descriptor?.type ?: return
val newType = if (KotlinBuiltIns.isPrimitiveArray(type)) type.toString() else "Array<${typeReference.text}>"
typeReference.replace(KtPsiFactory(element).createType(newType))
element.removeModifier(KtTokens.VARARG_KEYWORD)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertArrayParameterToVarargIntention
@@ -0,0 +1 @@
fun <T> test(a<caret>: Array<T>) = a.size
@@ -0,0 +1 @@
fun <T> test(vararg a: T) = a.size
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Convert to vararg parameter
fun test(<caret>a: Array<Int>) {
a[0] = 1
}
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Convert to vararg parameter
fun test(vararg a: Int) {
a[0] = 1
}
@@ -0,0 +1,5 @@
// INTENTION_TEXT: Convert to vararg parameter (may break code)
// DISABLE-ERRORS
fun test(a: Array<String><caret>) {
a[0] = ""
}
@@ -0,0 +1,5 @@
// INTENTION_TEXT: Convert to vararg parameter (may break code)
// DISABLE-ERRORS
fun test(vararg a: String) {
a[0] = ""
}
@@ -0,0 +1,3 @@
class Test(<caret>private val a: Array<String>) {
val size = a.size
}
@@ -0,0 +1,3 @@
class Test(private vararg val a: String) {
val size = a.size
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
listOf(arrayOf(1)).map { <caret>i: Array<Int> ->
i + 1
}
}
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Convert to vararg parameter
fun test(<caret>a: IntArray) {
a[0] = 1
}
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Convert to vararg parameter
fun test(vararg a: Int) {
a[0] = 1
}
@@ -0,0 +1 @@
fun test(<caret>a: LongArray) = a.size
@@ -0,0 +1 @@
fun test(vararg a: Long) = a.size
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
fun test(<caret>a: Array<*>) = a.size
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
fun test(<caret>vararg a: String) = a.size
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
fun <T> test(<caret>a: Array<in T>) = a.size
@@ -0,0 +1,2 @@
// INTENTION_TEXT: Convert to vararg parameter
fun <T> test(<caret>a: Array<out T>) = a.size
@@ -0,0 +1,2 @@
// INTENTION_TEXT: Convert to vararg parameter
fun <T> test(vararg a: T) = a.size
@@ -0,0 +1 @@
fun test(a: Array<String> = emptyArray()<caret>) = a.size
@@ -0,0 +1 @@
fun test(vararg a: String = emptyArray()) = a.size
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertVarargParameterToArrayIntention
@@ -0,0 +1 @@
fun <T> test(<caret>vararg a: T) = a.size
@@ -0,0 +1 @@
fun <T> test(a: Array<T>) = a.size
@@ -0,0 +1,3 @@
class Test(<caret>private vararg val a: String) {
val size = a.size
}
@@ -0,0 +1,3 @@
class Test(private val a: Array<String>) {
val size = a.size
}
@@ -0,0 +1 @@
fun test(vararg a: Int<caret>) = a.size
@@ -0,0 +1 @@
fun test(a: IntArray) = a.size
@@ -0,0 +1 @@
fun test(vararg <caret>a: Long) = a.size
@@ -0,0 +1 @@
fun test(a: LongArray) = a.size
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
fun test(<caret>a: String) {}
@@ -0,0 +1 @@
fun test(<caret>vararg a: String) = a.size
@@ -0,0 +1 @@
fun test(a: Array<String>) = a.size
@@ -0,0 +1 @@
fun test(vararg a: String = emptyArray()<caret>) = a.size
@@ -0,0 +1 @@
fun test(a: Array<String> = emptyArray()) = a.size
@@ -3994,6 +3994,87 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertArrayParameterToVararg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertArrayParameterToVararg extends AbstractIntentionTest {
public void testAllFilesPresentInConvertArrayParameterToVararg() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertArrayParameterToVararg"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("arrayGenericType.kt")
public void testArrayGenericType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/arrayGenericType.kt");
doTest(fileName);
}
@TestMetadata("arrayInt.kt")
public void testArrayInt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/arrayInt.kt");
doTest(fileName);
}
@TestMetadata("arrayString.kt")
public void testArrayString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/arrayString.kt");
doTest(fileName);
}
@TestMetadata("inConstructor.kt")
public void testInConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/inConstructor.kt");
doTest(fileName);
}
@TestMetadata("inLambda.kt")
public void testInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/inLambda.kt");
doTest(fileName);
}
@TestMetadata("intArray.kt")
public void testIntArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/intArray.kt");
doTest(fileName);
}
@TestMetadata("longArray.kt")
public void testLongArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/longArray.kt");
doTest(fileName);
}
@TestMetadata("starProjection.kt")
public void testStarProjection() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/starProjection.kt");
doTest(fileName);
}
@TestMetadata("vararg.kt")
public void testVararg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/vararg.kt");
doTest(fileName);
}
@TestMetadata("withContravariant.kt")
public void testWithContravariant() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/withContravariant.kt");
doTest(fileName);
}
@TestMetadata("withCovariance.kt")
public void testWithCovariance() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/withCovariance.kt");
doTest(fileName);
}
@TestMetadata("withDefaultValue.kt")
public void testWithDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertArrayParameterToVararg/withDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/convertAssertToIf")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -7375,6 +7456,57 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertVarargParameterToArray")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertVarargParameterToArray extends AbstractIntentionTest {
public void testAllFilesPresentInConvertVarargParameterToArray() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertVarargParameterToArray"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("genericType.kt")
public void testGenericType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/genericType.kt");
doTest(fileName);
}
@TestMetadata("inConstructor.kt")
public void testInConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/inConstructor.kt");
doTest(fileName);
}
@TestMetadata("int.kt")
public void testInt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/int.kt");
doTest(fileName);
}
@TestMetadata("long.kt")
public void testLong() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/long.kt");
doTest(fileName);
}
@TestMetadata("noVararg.kt")
public void testNoVararg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/noVararg.kt");
doTest(fileName);
}
@TestMetadata("string.kt")
public void testString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/string.kt");
doTest(fileName);
}
@TestMetadata("withDefaultValue.kt")
public void testWithDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertVarargParameterToArray/withDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)