Take into account vararg modifier in "Add type" quick fix
So #KT-20894 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
4d93c08bd0
commit
2dd66225f3
+11
-5
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.project.builtIns
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
@@ -40,11 +41,16 @@ class AddTypeAnnotationToValueParameterFix(element: KtParameter) : KotlinQuickFi
|
||||
init {
|
||||
val defaultValue = element.defaultValue
|
||||
var type = defaultValue?.getType(defaultValue.analyze(BodyResolveMode.PARTIAL))
|
||||
if (defaultValue is KtCollectionLiteralExpression && type != null && KotlinBuiltIns.isArray(type)) {
|
||||
val builtIns = element.builtIns
|
||||
val elementType = builtIns.getArrayElementType(type)
|
||||
if (KotlinBuiltIns.isPrimitiveType(elementType)) {
|
||||
type = builtIns.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(elementType)
|
||||
if (type != null && KotlinBuiltIns.isArray(type)) {
|
||||
if (element.hasModifier(KtTokens.VARARG_KEYWORD)) {
|
||||
type = type.arguments.singleOrNull()?.type
|
||||
}
|
||||
else if (defaultValue is KtCollectionLiteralExpression) {
|
||||
val builtIns = element.builtIns
|
||||
val elementType = builtIns.getArrayElementType(type)
|
||||
if (KotlinBuiltIns.isPrimitiveType(elementType)) {
|
||||
type = builtIns.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(elementType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'Double' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value = [1.0, 2.2]<caret>)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'Double' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value: Double = [1.0, 2.2])
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'Int' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value = [1, 2]<caret>)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'Int' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value: Int = [1, 2])
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'String' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value = ["alpha", "beta"]<caret>)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Add type 'String' to parameter 'value'" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class CollectionDefault(vararg val value: String = ["alpha", "beta"])
|
||||
@@ -1025,6 +1025,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("annotationWithVarargArrayLiteralDouble.kt")
|
||||
public void testAnnotationWithVarargArrayLiteralDouble() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithVarargArrayLiteralDouble.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("annotationWithVarargArrayLiteralInt.kt")
|
||||
public void testAnnotationWithVarargArrayLiteralInt() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithVarargArrayLiteralInt.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("annotationWithVarargArrayLiteralString.kt")
|
||||
public void testAnnotationWithVarargArrayLiteralString() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithVarargArrayLiteralString.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noDefaultValue.kt")
|
||||
public void testNoDefaultValue() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt");
|
||||
|
||||
Reference in New Issue
Block a user