[kotlin k2] Extract the shouldLambdaParameterBeNamed check from the moveInsideParenthesesAndReplaceWith method

^KTIJ-21135

GitOrigin-RevId: 007fbc46fbb55556eb95cd9d66d816cff50ce6b6
This commit is contained in:
Andrey Cherkasov
2023-08-10 17:11:29 +04:00
committed by intellij-monorepo-bot
parent 50942dcb0e
commit 37b6433df2
3 changed files with 36 additions and 18 deletions

View File

@@ -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<ValueArgument>, 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
}

View File

@@ -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

View File

@@ -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
}