From 9b0548f1ba892cf32ab3cc6407cec7481c739c8b Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 25 Oct 2023 20:25:46 +0200 Subject: [PATCH] [kotlin] k2: rename: move lambda outside of parentheses when possible GitOrigin-RevId: dafae17d7a54aa67625df440fa8111bef11486cc --- .../refactoring/rename/K1ReferenceMutateService.kt | 13 ++----------- .../renameGetToInvoke/renameGetToInvoke.test | 2 +- .../rename/KtReferenceMutateServiceBase.kt | 14 +++++++++++++- .../k2/refactoring/K2ReferenceMutateService.kt | 8 +++++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/refactoring/rename/K1ReferenceMutateService.kt b/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/refactoring/rename/K1ReferenceMutateService.kt index 83ed0703714d..482873ae2f25 100644 --- a/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/refactoring/rename/K1ReferenceMutateService.kt +++ b/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/refactoring/rename/K1ReferenceMutateService.kt @@ -17,8 +17,6 @@ import org.jetbrains.kotlin.idea.codeInsight.shorten.addDelayedImportRequest import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.canMoveLambdaOutsideParentheses -import org.jetbrains.kotlin.idea.refactoring.intentions.OperatorToFunctionConverter -import org.jetbrains.kotlin.idea.refactoring.moveFunctionLiteralOutsideParentheses import org.jetbrains.kotlin.idea.references.KtReference import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.references.KtSimpleReference @@ -188,14 +186,7 @@ class K1ReferenceMutateService : KtReferenceMutateServiceBase() { return selector as KtNameReferenceExpression } - override fun replaceWithImplicitInvokeInvocation(newExpression: KtDotQualifiedExpression): KtExpression? { - val canMoveLambda = newExpression.getPossiblyQualifiedCallExpression()?.canMoveLambdaOutsideParentheses() == true - return OperatorToFunctionConverter.replaceExplicitInvokeCallWithImplicit(newExpression)?.let { newQualifiedExpression -> - newQualifiedExpression.getPossiblyQualifiedCallExpression() - ?.takeIf { canMoveLambda } - ?.let(KtCallExpression::moveFunctionLiteralOutsideParentheses) - - newQualifiedExpression - } + override fun canMoveLambdaOutsideParentheses(newExpression: KtDotQualifiedExpression): Boolean { + return newExpression.getPossiblyQualifiedCallExpression()?.canMoveLambdaOutsideParentheses() == true } } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/rename/renameGetToInvoke/renameGetToInvoke.test b/plugins/kotlin/idea/tests/testData/refactoring/rename/renameGetToInvoke/renameGetToInvoke.test index 09faec9d5bee..ac0372952294 100644 --- a/plugins/kotlin/idea/tests/testData/refactoring/rename/renameGetToInvoke/renameGetToInvoke.test +++ b/plugins/kotlin/idea/tests/testData/refactoring/rename/renameGetToInvoke/renameGetToInvoke.test @@ -4,5 +4,5 @@ "oldName": "get", "newName": "invoke", "mainFile": "main.kt", - "enabledInK2": "false" + "enabledInK2": "true" } \ No newline at end of file diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/rename/KtReferenceMutateServiceBase.kt b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/rename/KtReferenceMutateServiceBase.kt index c73f698eafd1..1e50790c799d 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/rename/KtReferenceMutateServiceBase.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/rename/KtReferenceMutateServiceBase.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.idea.base.psi.replaced import org.jetbrains.kotlin.idea.base.psi.unquoteKotlinIdentifier import org.jetbrains.kotlin.idea.kdoc.KDocElementFactory import org.jetbrains.kotlin.idea.refactoring.intentions.OperatorToFunctionConverter +import org.jetbrains.kotlin.idea.refactoring.moveFunctionLiteralOutsideParentheses import org.jetbrains.kotlin.idea.references.* import org.jetbrains.kotlin.lexer.KtSingleValueToken import org.jetbrains.kotlin.lexer.KtToken @@ -252,7 +253,18 @@ abstract class KtReferenceMutateServiceBase : KtReferenceMutateService { private fun convertOperatorToFunctionCall(opExpression: KtOperationExpression): Pair = OperatorToFunctionConverter.convert(opExpression) - protected abstract fun replaceWithImplicitInvokeInvocation(newExpression: KtDotQualifiedExpression): KtExpression? + protected abstract fun canMoveLambdaOutsideParentheses(newExpression: KtDotQualifiedExpression): Boolean + + protected fun replaceWithImplicitInvokeInvocation(newExpression: KtDotQualifiedExpression): KtExpression? { + val canMoveLambda = canMoveLambdaOutsideParentheses(newExpression) + return OperatorToFunctionConverter.replaceExplicitInvokeCallWithImplicit(newExpression)?.let { newQualifiedExpression -> + newQualifiedExpression.getPossiblyQualifiedCallExpression() + ?.takeIf { canMoveLambda } + ?.let(KtCallExpression::moveFunctionLiteralOutsideParentheses) + + newQualifiedExpression + } + } private fun AbstractKtReference.renameImplicitConventionalCall(newName: String): KtExpression { val (newExpression, newNameElement) = OperatorToFunctionConverter.convert(expression) diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt index c4e86539393d..ac27ef6a48e7 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.idea.base.codeInsight.KotlinOptimizeImportsFacility import org.jetbrains.kotlin.idea.base.psi.kotlinFqName import org.jetbrains.kotlin.idea.base.psi.replaced import org.jetbrains.kotlin.idea.kdoc.KDocElementFactory -import org.jetbrains.kotlin.idea.refactoring.intentions.OperatorToFunctionConverter import org.jetbrains.kotlin.idea.refactoring.rename.KtReferenceMutateServiceBase import org.jetbrains.kotlin.idea.references.KDocReference import org.jetbrains.kotlin.idea.references.KtReference @@ -27,7 +26,9 @@ import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.references.KtSimpleReference import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getPossiblyQualifiedCallExpression import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement + /** * At the moment, this implementation of [org.jetbrains.kotlin.idea.references.KtReferenceMutateService] is not able to do some of the * required operations. It is OK and on purpose - this functionality will be added later. @@ -186,8 +187,9 @@ internal class K2ReferenceMutateService : KtReferenceMutateServiceBase() { } } - override fun replaceWithImplicitInvokeInvocation(newExpression: KtDotQualifiedExpression): KtExpression? = - OperatorToFunctionConverter.replaceExplicitInvokeCallWithImplicit(newExpression) + override fun canMoveLambdaOutsideParentheses(newExpression: KtDotQualifiedExpression): Boolean { + return newExpression.getPossiblyQualifiedCallExpression()?.canMoveLambdaOutsideParentheses() == true + } private fun operationNotSupportedInK2Error(): Nothing { throw IncorrectOperationException("K2 plugin does not yet support this operation")