Introduce Type Alias: Fix replacement of references to nested classes
#KT-15159 Fixed
This commit is contained in:
+17
-4
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user