From 05518341648a0568e056a010d9a374e7b3bb5b88 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Mon, 1 Feb 2021 23:21:08 +0100 Subject: [PATCH] Extract getText/getFamilyName from ChangeCallableReturnTypeFix to use in FIR IDE --- .../quickfix/ChangeCallableReturnTypeFix.kt | 101 +++++++++++------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt index bf871505b96..ab2061bc3c2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeCallableReturnTypeFix.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil import org.jetbrains.kotlin.idea.project.builtIns import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext import org.jetbrains.kotlin.resolve.BindingContext @@ -71,19 +72,10 @@ abstract class ChangeCallableReturnTypeFix( open fun functionPresentation(): String? { val element = element!! - val name = element.name - if (name != null) { - val container = element.unsafeResolveToDescriptor().containingDeclaration as? ClassDescriptor - val containerName = container?.name?.takeUnless { it.isSpecial }?.asString() - val fullName = if (containerName != null) "'$containerName.$name'" else "'$name'" - if (element is KtParameter) { - return KotlinBundle.message("fix.change.return.type.presentation.property", fullName) - } else { - return KotlinBundle.message("fix.change.return.type.presentation.function", fullName) - } - } else { - return null - } + if (element.name == null) return null + val container = element.unsafeResolveToDescriptor().containingDeclaration as? ClassDescriptor + val containerName = container?.name?.takeUnless { it.isSpecial } + return StringPresentation.functionOrConstructorParameterPresentation(element, containerName) } class OnType(element: KtFunction, type: KotlinType) : ChangeCallableReturnTypeFix(element, type), HighPriorityAction { @@ -112,7 +104,7 @@ abstract class ChangeCallableReturnTypeFix( class ForOverridden(element: KtFunction, type: KotlinType) : ChangeCallableReturnTypeFix(element, type) { override fun functionPresentation(): String? { val presentation = super.functionPresentation() ?: return null - return KotlinBundle.message("fix.change.return.type.presentation.base", presentation) + return StringPresentation.baseFunctionOrConstructorParameterPresentation(presentation) } } @@ -123,32 +115,10 @@ abstract class ChangeCallableReturnTypeFix( return changeFunctionLiteralReturnTypeFix.text } - val functionPresentation = functionPresentation() - - if (isUnitType && element is KtFunction && element.hasBlockBody()) { - return if (functionPresentation == null) - KotlinBundle.message("fix.change.return.type.remove.explicit.return.type") - else - KotlinBundle.message("fix.change.return.type.remove.explicit.return.type.of", functionPresentation) - } - - return when (element) { - is KtFunction -> { - if (functionPresentation != null) - KotlinBundle.message("fix.change.return.type.return.type.text.of", functionPresentation, typePresentation) - else - KotlinBundle.message("fix.change.return.type.return.type.text", typePresentation) - } - else -> { - if (functionPresentation != null) - KotlinBundle.message("fix.change.return.type.type.text.of", functionPresentation, typePresentation) - else - KotlinBundle.message("fix.change.return.type.type.text", typePresentation) - } - } + return StringPresentation.getTextForQuickFix(element, functionPresentation(), isUnitType, typePresentation) } - override fun getFamilyName() = KotlinBundle.message("fix.change.return.type.family") + override fun getFamilyName(): String = StringPresentation.familyName() override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean { return !typeContainsError && @@ -264,4 +234,59 @@ abstract class ChangeCallableReturnTypeFix( return multiDeclaration.entries[componentIndex - 1] } } + + object StringPresentation { + fun familyName(): String = KotlinBundle.message("fix.change.return.type.family") + + fun functionOrConstructorParameterPresentation(element: KtCallableDeclaration, containerName: Name?): String? { + val name = element.name + return if (name != null) { + val fullName = if (containerName != null) "'${containerName.asString()}.$name'" else "'$name'" + when (element) { + is KtParameter -> KotlinBundle.message("fix.change.return.type.presentation.property", fullName) + is KtProperty -> KotlinBundle.message("fix.change.return.type.presentation.property", fullName) + else -> KotlinBundle.message("fix.change.return.type.presentation.function", fullName) + } + } else null + } + + + fun baseFunctionOrConstructorParameterPresentation(presentation: String): String = + KotlinBundle.message("fix.change.return.type.presentation.base", presentation) + + fun baseFunctionOrConstructorParameterPresentation(element: KtCallableDeclaration, containerName: Name?): String? { + val presentation = functionOrConstructorParameterPresentation(element, containerName) ?: return null + return baseFunctionOrConstructorParameterPresentation(presentation) + } + + fun getTextForQuickFix( + element: KtCallableDeclaration, + presentation: String?, + isUnitType: Boolean, + typePresentation: String + ): String { + if (isUnitType && element is KtFunction && element.hasBlockBody()) { + return if (presentation == null) + KotlinBundle.message("fix.change.return.type.remove.explicit.return.type") + else + KotlinBundle.message("fix.change.return.type.remove.explicit.return.type.of", presentation) + } + + return when (element) { + is KtFunction -> { + if (presentation != null) + KotlinBundle.message("fix.change.return.type.return.type.text.of", presentation, typePresentation) + else + KotlinBundle.message("fix.change.return.type.return.type.text", typePresentation) + } + else -> { + if (presentation != null) + KotlinBundle.message("fix.change.return.type.type.text.of", presentation, typePresentation) + else + KotlinBundle.message("fix.change.return.type.type.text", typePresentation) + } + } + } + } } +