diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddLabeledReturnInLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddLabeledReturnInLambdaIntention.kt index 752022c0483..947be6636f9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddLabeledReturnInLambdaIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddLabeledReturnInLambdaIntention.kt @@ -10,7 +10,6 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression class AddLabeledReturnInLambdaIntention : SelfTargetingRangeIntention( @@ -19,14 +18,13 @@ class AddLabeledReturnInLambdaIntention : SelfTargetingRangeIntention() ?: return null - val callExpression = lambdaExpression.getStrictParentOfType() ?: return null - val valueArgument = callExpression.valueArguments.find { - it.getArgumentExpression()?.unpackFunctionLiteral(allowParentheses = false) === lambdaExpression - } ?: return null - val lambdaLabelName = (valueArgument.getArgumentExpression() as? KtLabeledExpression)?.getLabelName() - return lambdaLabelName ?: callExpression.getCallNameExpression()?.text - } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveLabeledReturnInLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveLabeledReturnInLambdaIntention.kt index a1736c25358..31ca4bce648 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveLabeledReturnInLambdaIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveLabeledReturnInLambdaIntention.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType class RemoveLabeledReturnInLambdaIntention : SelfTargetingIntention( @@ -19,13 +18,7 @@ class RemoveLabeledReturnInLambdaIntention : SelfTargetingIntention() ?: return false if (block.statements.lastOrNull() != element) return false - val lambdaExpression = block.getStrictParentOfType() ?: return false - val callExpression = lambdaExpression.getStrictParentOfType() ?: return false - val lambdaArgument = callExpression.lambdaArguments.find { - it.getArgumentExpression().unpackFunctionLiteral(allowParentheses = false) === lambdaExpression - } ?: return false - val callName = (lambdaArgument.getArgumentExpression() as? KtLabeledExpression)?.getLabelName() - ?: callExpression.getCallNameExpression()?.text ?: return false + val callName = block.getParentLambdaLabelName() ?: return false if (labelName != callName) return false text = "Remove return@$labelName" return true diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index 37d09a5a9a0..c636233dce5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -30,6 +30,8 @@ import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.CollectionLiteralResolver import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall @@ -294,3 +296,13 @@ fun KtCallExpression.isArrayOfMethod(): Boolean { return (descriptor.containingDeclaration as? PackageFragmentDescriptor)?.fqName == KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME && ARRAY_OF_METHODS.contains(descriptor.name) } + +fun KtBlockExpression.getParentLambdaLabelName(): String? { + val lambdaExpression = getStrictParentOfType() ?: return null + val callExpression = lambdaExpression.getStrictParentOfType() ?: return null + val valueArgument = callExpression.valueArguments.find { + it.getArgumentExpression()?.unpackFunctionLiteral(allowParentheses = false) === lambdaExpression + } ?: return null + val lambdaLabelName = (valueArgument.getArgumentExpression() as? KtLabeledExpression)?.getLabelName() + return lambdaLabelName ?: callExpression.getCallNameExpression()?.text +}