Suggest primitive array type for collection literals in "add type" fix

So #KT-18549 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-10-10 12:51:27 +03:00
committed by Mikhail Glukhikh
parent e65112fcab
commit 0b5b5d8e89
8 changed files with 53 additions and 1 deletions
@@ -19,11 +19,14 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
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.psi.KtCollectionLiteralExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPsiFactory
@@ -36,7 +39,14 @@ class AddTypeAnnotationToValueParameterFix(element: KtParameter) : KotlinQuickFi
init {
val defaultValue = element.defaultValue
val type = defaultValue?.getType(defaultValue.analyze(BodyResolveMode.PARTIAL))
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)
}
}
typeNameShort = type?.let { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) }
typeName = type?.let { IdeDescriptorRenderers.SOURCE_CODE.renderType(it) }
@@ -0,0 +1,4 @@
// "Add type 'DoubleArray' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value = [1.0, 2.2]<caret>)
@@ -0,0 +1,4 @@
// "Add type 'DoubleArray' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value: DoubleArray = [1.0, 2.2])
@@ -0,0 +1,4 @@
// "Add type 'IntArray' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value = [1, 2]<caret>)
@@ -0,0 +1,4 @@
// "Add type 'IntArray' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value: IntArray = [1, 2])
@@ -0,0 +1,4 @@
// "Add type 'Array<String>' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value = ["alpha", "beta"]<caret>)
@@ -0,0 +1,4 @@
// "Add type 'Array<String>' to parameter 'value'" "true"
// LANGUAGE_VERSION: 1.2
annotation class CollectionDefault(val value: Array<String> = ["alpha", "beta"]<caret>)
@@ -893,6 +893,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addTypeAnnotationToValueParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("annotationWithArrayLiteralDouble.kt")
public void testAnnotationWithArrayLiteralDouble() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithArrayLiteralDouble.kt");
doTest(fileName);
}
@TestMetadata("annotationWithArrayLiteralInt.kt")
public void testAnnotationWithArrayLiteralInt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithArrayLiteralInt.kt");
doTest(fileName);
}
@TestMetadata("annotationWithArrayLiteralString.kt")
public void testAnnotationWithArrayLiteralString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/annotationWithArrayLiteralString.kt");
doTest(fileName);
}
@TestMetadata("noDefaultValue.kt")
public void testNoDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addTypeAnnotationToValueParameter/noDefaultValue.kt");