CreateTypeParameterFromUsageFix: fix KNPE for type alias

#KT-33302 Fixed
#EA-120181 Fixed
This commit is contained in:
Dmitry Gridin
2019-08-13 16:54:45 +07:00
parent 932765744a
commit cf3b92d80e
5 changed files with 21 additions and 2 deletions
@@ -489,9 +489,10 @@ fun KtTypeParameterListOwner.addTypeParameter(typeParameter: KtTypeParameter): K
val list = KtPsiFactory(this).createTypeParameterList("<X>")
list.parameters[0].replace(typeParameter)
val leftAnchor = when (this) {
is KtClass -> nameIdentifier ?: getClassOrInterfaceKeyword()
is KtClass -> nameIdentifier
is KtNamedFunction -> funKeyword
is KtProperty -> valOrVarKeyword
is KtTypeAlias -> nameIdentifier
else -> null
} ?: return null
return (addAfter(list, leftAnchor) as KtTypeParameterList).parameters.first()
@@ -97,7 +97,8 @@ class CreateTypeParameterFromUsageFix(
} else null
val upperBound = upperBoundText?.let { psiFactory.createType(it) }
val newTypeParameterText = if (upperBound != null) "${typeParameter.name} : ${upperBound.text}" else typeParameter.name
val newTypeParameter = declaration.addTypeParameter(psiFactory.createTypeParameter(newTypeParameterText))!!
val newTypeParameter = declaration.addTypeParameter(psiFactory.createTypeParameter(newTypeParameterText))
?: error("Couldn't create type parameter from '$newTypeParameterText' for '$declaration'")
elementsToShorten += newTypeParameter
val anonymizedTypeParameter = createFakeTypeParameterDescriptor(typeParameter.fakeTypeParameter.containingDeclaration, "_")
@@ -0,0 +1,6 @@
// "Create type parameter in type alias 'G'" "true"
class C
typealias G = C
fun <T> a(g: G<T<caret>>) = Unit
@@ -0,0 +1,6 @@
// "Create type parameter in type alias 'G'" "true"
class C
typealias G<T> = C
fun <T> a(g: G<T>) = Unit
@@ -4780,6 +4780,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
public void testNotOnTypeArgumentList() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createTypeParameter/inReferencedDeclaration/notOnTypeArgumentList.kt");
}
@TestMetadata("typealias.kt")
public void testTypealias() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createTypeParameter/inReferencedDeclaration/typealias.kt");
}
}
}