mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] Convert AddReturnToLastExpressionInFunctionFix to ModCommandAction
^KTIJ-29555 GitOrigin-RevId: ab55789f1308595894df7463e5f1491b3dc05d3c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
f5ca57c232
commit
ca6499d0f3
+14
-11
@@ -1,25 +1,28 @@
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.modcommand.ActionContext
|
||||
import com.intellij.modcommand.ModPsiUpdater
|
||||
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.KotlinQuickFixAction
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinPsiUpdateModCommandAction
|
||||
import org.jetbrains.kotlin.psi.KtDeclarationWithBody
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
|
||||
class AddReturnToLastExpressionInFunctionFix(element: KtDeclarationWithBody) : KotlinQuickFixAction<KtDeclarationWithBody>(element) {
|
||||
class AddReturnToLastExpressionInFunctionFix(
|
||||
element: KtNamedFunction,
|
||||
) : KotlinPsiUpdateModCommandAction.ElementBased<KtDeclarationWithBody, Unit>(element, Unit) {
|
||||
|
||||
override fun getText() = KotlinBundle.message("fix.add.return.last.expression")
|
||||
override fun getFamilyName() = text
|
||||
override fun getFamilyName(): String = KotlinBundle.message("fix.add.return.last.expression")
|
||||
|
||||
override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean = element is KtNamedFunction
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
override fun invoke(
|
||||
actionContext: ActionContext,
|
||||
element: KtDeclarationWithBody,
|
||||
elementContext: Unit,
|
||||
updater: ModPsiUpdater,
|
||||
) {
|
||||
val element = element as? KtNamedFunction ?: return
|
||||
val last = element.bodyBlockExpression?.statements?.lastOrNull() ?: return
|
||||
last.replace(KtPsiFactory(project).createExpression("return ${last.text}"))
|
||||
last.replace(KtPsiFactory(actionContext.project).createExpression("return ${last.text}"))
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,6 +27,6 @@ internal object AddReturnToLastExpressionInFunctionFixFactory : KotlinSingleInte
|
||||
val expectedType = namedFunction.resolveToDescriptorIfAny()?.returnType?.takeIf { !it.isError } ?: return null
|
||||
if (!lastType.isSubtypeOf(expectedType)) return null
|
||||
|
||||
return AddReturnToLastExpressionInFunctionFix(casted.psiElement)
|
||||
return AddReturnToLastExpressionInFunctionFix(namedFunction).asIntention()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user