Revert "Minor: Extract lambda return expression handling"

This reverts commit 55038e19
This commit is contained in:
Yan Zhulanow
2020-10-24 00:20:30 +09:00
parent b18de1e3ff
commit 601198634d

View File

@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
@@ -63,28 +62,23 @@ class ChangeToLabeledReturnFix(
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
val element = diagnostic.psiElement as? KtElement ?: return emptyList()
val context by lazy { element.analyze() }
val returnExpression = when (diagnostic.factory) {
Errors.RETURN_NOT_ALLOWED ->
Errors.RETURN_NOT_ALLOWED -> {
diagnostic.psiElement as? KtReturnExpression
Errors.TYPE_MISMATCH,
Errors.CONSTANT_EXPECTED_TYPE_MISMATCH,
Errors.NULL_FOR_NONNULL_TYPE ->
getLambdaReturnExpression(diagnostic.psiElement, context)
}
Errors.TYPE_MISMATCH, Errors.CONSTANT_EXPECTED_TYPE_MISMATCH, Errors.NULL_FOR_NONNULL_TYPE -> {
val returnExpression = diagnostic.psiElement.getStrictParentOfType<KtReturnExpression>() ?: return emptyList()
val lambda = returnExpression.getStrictParentOfType<KtLambdaExpression>() ?: return emptyList()
val lambdaReturnType = context[BindingContext.FUNCTION, lambda.functionLiteral]?.returnType ?: return emptyList()
val returnType = returnExpression.returnedExpression?.getType(context) ?: return emptyList()
if (!returnType.isSubtypeOf(lambdaReturnType)) return emptyList()
returnExpression
}
else -> null
} ?: return emptyList()
val candidates = findAccessibleLabels(context, returnExpression)
return candidates.map { ChangeToLabeledReturnFix(returnExpression, labeledReturn = "return@${it.render()}") }
}
private fun getLambdaReturnExpression(element: PsiElement, bindingContext: BindingContext): KtReturnExpression? {
val returnExpression = element.getStrictParentOfType<KtReturnExpression>() ?: return null
val lambda = returnExpression.getStrictParentOfType<KtLambdaExpression>() ?: return null
val lambdaReturnType = bindingContext[BindingContext.FUNCTION, lambda.functionLiteral]?.returnType ?: return null
val returnType = returnExpression.returnedExpression?.getType(bindingContext) ?: return null
if (!returnType.isSubtypeOf(lambdaReturnType)) return null
return returnExpression
return findAccessibleLabels(context, returnExpression).map {
ChangeToLabeledReturnFix(returnExpression, labeledReturn = "return@${it.render()}")
}
}
}
}