Introduce Type Alias: Fix replacement of references to nested classes

#KT-15159 Fixed
This commit is contained in:
Alexey Sedunov
2018-05-31 15:39:43 +03:00
parent 3902e1b51d
commit eea9113d1c
6 changed files with 105 additions and 4 deletions
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.compareDescriptors
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.refactoring.introduce.insertDeclaration
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.psi.patternMatching.KotlinPsiRange
@@ -164,23 +165,35 @@ fun findDuplicates(typeAlias: KtTypeAlias): Map<KotlinPsiRange, () -> Unit> {
val psiFactory = KtPsiFactory(typeAlias)
fun replaceTypeElement(occurrence: KtTypeElement, typeArgumentsText: String) {
occurrence.replace(psiFactory.createType("$aliasName$typeArgumentsText").typeElement!!)
}
fun replaceOccurrence(occurrence: PsiElement, arguments: List<KtTypeElement>) {
val typeArgumentsText = if (arguments.isNotEmpty()) "<${arguments.joinToString { it.text }}>" else ""
when (occurrence) {
is KtTypeElement -> {
occurrence.replace(psiFactory.createType("$aliasName$typeArgumentsText").typeElement!!)
replaceTypeElement(occurrence, typeArgumentsText)
}
is KtSuperTypeCallEntry -> {
occurrence.calleeExpression.typeReference?.typeElement?.let { replaceTypeElement(it, typeArgumentsText) }
}
is KtCallElement -> {
val typeArgumentList = occurrence.typeArgumentList
val qualifiedExpression = occurrence.parent as? KtQualifiedExpression
val callExpression = if (qualifiedExpression != null && qualifiedExpression.selectorExpression == occurrence) {
qualifiedExpression.replaced(occurrence)
} else occurrence
val typeArgumentList = callExpression.typeArgumentList
if (arguments.isNotEmpty()) {
val newTypeArgumentList = psiFactory.createTypeArguments(typeArgumentsText)
typeArgumentList?.replace(newTypeArgumentList) ?: occurrence.addAfter(newTypeArgumentList, occurrence)
typeArgumentList?.replace(newTypeArgumentList) ?: callExpression.addAfter(newTypeArgumentList, callExpression.calleeExpression)
}
else {
typeArgumentList?.delete()
}
occurrence.calleeExpression?.replace(psiFactory.createExpression(aliasName))
callExpression.calleeExpression?.replace(psiFactory.createExpression(aliasName))
}
is KtExpression -> occurrence.replace(psiFactory.createExpression(aliasName))