From 37b6433df2559da68ef66cdcc6e5a90faf232cdc Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Thu, 10 Aug 2023 17:11:29 +0400 Subject: [PATCH] [kotlin k2] Extract the `shouldLambdaParameterBeNamed` check from the `moveInsideParenthesesAndReplaceWith` method ^KTIJ-21135 GitOrigin-RevId: 007fbc46fbb55556eb95cd9d66d816cff50ce6b6 --- .../base/psi/KotlinPsiModificationUtils.kt | 30 +++++++++++-------- .../kotlin/idea/core/psiModificationUtils.kt | 19 ++++++++++-- .../idea/codeInliner/ReplacementPerformer.kt | 5 ++-- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/plugins/kotlin/base/psi/src/org/jetbrains/kotlin/idea/base/psi/KotlinPsiModificationUtils.kt b/plugins/kotlin/base/psi/src/org/jetbrains/kotlin/idea/base/psi/KotlinPsiModificationUtils.kt index 938ed1b9d70c..3b4fda0c3695 100644 --- a/plugins/kotlin/base/psi/src/org/jetbrains/kotlin/idea/base/psi/KotlinPsiModificationUtils.kt +++ b/plugins/kotlin/base/psi/src/org/jetbrains/kotlin/idea/base/psi/KotlinPsiModificationUtils.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. @file:JvmName("KotlinPsiModificationUtils") package org.jetbrains.kotlin.idea.base.psi @@ -118,22 +118,23 @@ fun KtParameter.setDefaultValue(newDefaultValue: KtExpression): PsiElement { return addAfter(newDefaultValue, eq) as KtExpression } +/** + * Moves the lambda argument inside parentheses and replaces it with the specified replacement expression. + * + * @param replacement The replacement expression to be used. + * @param lambdaArgumentName The name of the lambda argument; use `null` if no name is needed. + * @return The modified `KtCallExpression` with the lambda argument moved inside parentheses and replaced with + * the specified replacement expression. + */ fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith( replacement: KtExpression, - functionLiteralArgumentName: Name?, - withNameCheck: Boolean = true, + lambdaArgumentName: Name?, ): KtCallExpression { val oldCallExpression = parent as KtCallExpression val newCallExpression = oldCallExpression.copy() as KtCallExpression val psiFactory = KtPsiFactory(project) - - val argument = - if (withNameCheck && shouldLambdaParameterBeNamed(newCallExpression.getValueArgumentsInParentheses(), oldCallExpression)) { - psiFactory.createArgument(replacement, functionLiteralArgumentName) - } else { - psiFactory.createArgument(replacement) - } + val argument = psiFactory.createArgument(replacement, lambdaArgumentName) val functionLiteralArgument = newCallExpression.lambdaArguments.firstOrNull()!! val valueArgumentList = newCallExpression.valueArgumentList ?: psiFactory.createCallArguments("()") @@ -149,8 +150,13 @@ fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith( return oldCallExpression.replace(newCallExpression) as KtCallExpression } -private fun shouldLambdaParameterBeNamed(args: List, callExpr: KtCallExpression): Boolean { +/** + * Returns `true` if the lambda argument should be named, `false` otherwise. + */ +fun shouldLambdaParameterBeNamed(argument: KtLambdaArgument): Boolean { + val callExpression = argument.parent as KtCallExpression + val args = callExpression.getValueArgumentsInParentheses() if (args.any { it.isNamed() }) return true - val callee = (callExpr.calleeExpression?.mainReference?.resolve() as? KtFunction) ?: return false + val callee = (callExpression.calleeExpression?.mainReference?.resolve() as? KtFunction) ?: return false return if (callee.valueParameters.any { it.isVarArg }) true else callee.valueParameters.size - 1 > args.size } \ No newline at end of file diff --git a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt b/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt index 325922da6eb0..a3d390ea75de 100644 --- a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt +++ b/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.kotlin.idea.core @@ -64,11 +64,24 @@ fun KtLambdaArgument.moveInsideParentheses(bindingContext: BindingContext): KtCa return moveInsideParenthesesAndReplaceWith(ktExpression, bindingContext) } +/** + * Moves the lambda argument inside parentheses and replaces it with the specified replacement expression. + * If the lambda argument should be named, it retrieves the lambda argument name from the binding context. + * + * @param replacement The replacement expression to be used. + * @param bindingContext The binding context used to retrieve the lambda argument name if necessary. + * @return The modified `KtCallExpression` with the lambda argument moved inside parentheses and replaced with + * the specified replacement expression. + */ fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith( replacement: KtExpression, bindingContext: BindingContext -): KtCallExpression = moveInsideParenthesesAndReplaceWith(replacement, getLambdaArgumentName(bindingContext)) - +): KtCallExpression { + val lambdaArgumentName = if (shouldLambdaParameterBeNamed(this)) { + this.getLambdaArgumentName(bindingContext) + } else null + return this.moveInsideParenthesesAndReplaceWith(replacement, lambdaArgumentName) +} fun KtLambdaArgument.getLambdaArgumentName(bindingContext: BindingContext): Name? { val callExpression = parent as KtCallExpression diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInliner/ReplacementPerformer.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInliner/ReplacementPerformer.kt index c5d604c0d964..cf92e95588eb 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInliner/ReplacementPerformer.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInliner/ReplacementPerformer.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.kotlin.idea.codeInliner @@ -102,8 +102,7 @@ private fun callWithoutLambdaArguments(callExpression: KtCallElement): String { val argumentExpression = lambdaArgument.getArgumentExpression() ?: return callExpression.text return lambdaArgument.moveInsideParenthesesAndReplaceWith( replacement = argumentExpression, - functionLiteralArgumentName = null, - withNameCheck = false + lambdaArgumentName = null, ).text ?: callExpression.text }