From bfa4d04aca8dbe20b223243f3768ad97f4078846 Mon Sep 17 00:00:00 2001 From: Aleksandr Izmailov Date: Wed, 1 Mar 2023 10:31:39 +0100 Subject: [PATCH] [devkit] IDEA-309527: remove quick fix for defaultModalityState GitOrigin-RevId: 83e2df6ede2c6502a3e14a24b63ba9c5f62db4b5 --- ...denInSuspectContextMethodInspectionTest.kt | 35 ------------------- .../messages/DevKitKotlinBundle.properties | 1 - ...rbiddenInSuspectContextMethodInspection.kt | 35 ------------------- 3 files changed, 71 deletions(-) diff --git a/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/ForbiddenInSuspectContextMethodInspectionTest.kt b/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/ForbiddenInSuspectContextMethodInspectionTest.kt index 53bd1bdfbcb9..06f5de56e391 100644 --- a/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/ForbiddenInSuspectContextMethodInspectionTest.kt +++ b/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/ForbiddenInSuspectContextMethodInspectionTest.kt @@ -44,7 +44,6 @@ class ForbiddenInSuspectContextMethodInspectionTest : LightJavaCodeInsightFixtur private val invokeAndWaitFix = "Replace 'invokeAndWait' call with 'withContext(Dispatchers.EDT) {}'" private val defaultModalityDescr = "'defaultModalityState()' does not work in suspend context. If it is really necessary, use 'contextModality()'" - private val defaultModalityFix = "Replace with 'contextModality()'" private val navigateToSuspendContextFix = "Navigate to suspend context" @@ -594,23 +593,6 @@ class ForbiddenInSuspectContextMethodInspectionTest : LightJavaCodeInsightFixtur } """.trimIndent()) myFixture.testHighlighting() - - val intention = myFixture.getAvailableIntention(defaultModalityFix) - assertNotNullK(intention) - myFixture.checkPreviewAndLaunchAction(intention) - - myFixture.checkResult(""" - @file:Suppress("UNUSED_VARIABLE", "UNUSED_PARAMETER") - - import com.intellij.openapi.application.* - import kotlinx.coroutines.* - - class MyService { - suspend fun fn() { - val state: ModalityState = currentCoroutineContext().contextModality() ?: TODO("Handle absence of ModalityState") - } - } - """.trimIndent()) } @Test @@ -630,23 +612,6 @@ class ForbiddenInSuspectContextMethodInspectionTest : LightJavaCodeInsightFixtur } """.trimIndent()) myFixture.testHighlighting() - - val intention = myFixture.getAvailableIntention(defaultModalityFix) - assertNotNullK(intention) - myFixture.checkPreviewAndLaunchAction(intention) - - myFixture.checkResult(""" - @file:Suppress("UNUSED_VARIABLE", "UNUSED_PARAMETER") - - import com.intellij.openapi.application.* - import kotlinx.coroutines.* - - class MyService { - suspend fun fn() { - val state: ModalityState = currentCoroutineContext().contextModality() ?: TODO("Handle absence of ModalityState") - } - } - """.trimIndent()) } @Test diff --git a/plugins/devkit/intellij.kotlin.devkit/resources/messages/DevKitKotlinBundle.properties b/plugins/devkit/intellij.kotlin.devkit/resources/messages/DevKitKotlinBundle.properties index 759f0479b34f..25ea5c019b3a 100644 --- a/plugins/devkit/intellij.kotlin.devkit/resources/messages/DevKitKotlinBundle.properties +++ b/plugins/devkit/intellij.kotlin.devkit/resources/messages/DevKitKotlinBundle.properties @@ -11,5 +11,4 @@ inspections.forbidden.method.in.suspend.context.invoke.later.text='invokeLater' inspections.forbidden.method.in.suspend.context.invoke.later.fix.with.context.text=(May change semantics) Replace 'invokeLater' call with 'withContext(Dispatchers.EDT) {}' inspections.forbidden.method.in.suspend.context.invoke.later.fix.launch.text=Replace 'invokeLater' with 'launch(Dispatchers.EDT)' inspections.forbidden.method.in.suspend.context.default.modality.state.text='defaultModalityState()' does not work in suspend context. If it is really necessary, use 'contextModality()' -inspections.forbidden.method.in.suspend.context.default.modality.state.fix.text=Replace with 'contextModality()' inspections.forbidden.method.in.suspend.context.navigate.to.suspend.context=Navigate to suspend context \ No newline at end of file diff --git a/plugins/devkit/intellij.kotlin.devkit/src/inspections/ForbiddenInSuspectContextMethodInspection.kt b/plugins/devkit/intellij.kotlin.devkit/src/inspections/ForbiddenInSuspectContextMethodInspection.kt index 2e6187ecd531..7d735df4108c 100644 --- a/plugins/devkit/intellij.kotlin.devkit/src/inspections/ForbiddenInSuspectContextMethodInspection.kt +++ b/plugins/devkit/intellij.kotlin.devkit/src/inspections/ForbiddenInSuspectContextMethodInspection.kt @@ -41,7 +41,6 @@ private const val MODALITY_STATE_DEFAULT_MODALITY_STATE = "com.intellij.openapi. private const val APPLICATION_GET_DEFAULT_MODALITY_STATE = "com.intellij.openapi.application.Application.getDefaultModalityState" private const val RESTRICTS_SUSPENSION = "kotlin.coroutines.RestrictsSuspension" private const val INTELLIJ_EDT_DISPATCHER = "com.intellij.openapi.application.EDT" -private const val CONTEXT_MODALITY_EXT = "com.intellij.openapi.application.contextModality" private const val LAUNCH = "kotlinx.coroutines.launch" private val requiresSuspendContextAnnotation = FqName(REQUIRES_SUSPEND_CONTEXT_ANNOTATION) @@ -54,14 +53,11 @@ private val modalityStateDefaultModalityState = FqName(MODALITY_STATE_DEFAULT_MO private val applicationGetDefaultModalityState = FqName(APPLICATION_GET_DEFAULT_MODALITY_STATE) private val restrictsSuspensionName = FqName(RESTRICTS_SUSPENSION) private val intelliJEdtDispatcher = FqName(INTELLIJ_EDT_DISPATCHER) -private val contextModalityExt = FqName(CONTEXT_MODALITY_EXT) private val coroutinesLaunch = FqName(LAUNCH) private const val COROUTINE_CHECK_CANCELED_FIX = "com.intellij.openapi.progress.checkCancelled" private const val WITH_CONTEXT = "kotlinx.coroutines.withContext" private const val DISPATCHERS = "kotlinx.coroutines.Dispatchers" -private const val CURRENT_COROUTINE_CONTEXT = "kotlinx.coroutines.currentCoroutineContext" -private const val KOTLIN_TODO = "kotlin.TODO" private const val COROUTINE_SCOPE = "kotlinx.coroutines.CoroutineScope" internal class ForbiddenInSuspectContextMethodInspection : LocalInspectionTool() { @@ -150,7 +146,6 @@ internal class ForbiddenInSuspectContextMethodInspection : LocalInspectionTool() extractElementToHighlight(expression), DevKitKotlinBundle.message("inspections.forbidden.method.in.suspend.context.default.modality.state.text"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, - ifInSuspend { ReplaceDefaultModalityStateWithCurrentModalityQuickFix(expression) } ) } applicationInvokeLater, invokeLaterKt -> { @@ -316,36 +311,6 @@ internal class ForbiddenInSuspectContextMethodInspection : LocalInspectionTool() } } - private class ReplaceDefaultModalityStateWithCurrentModalityQuickFix(element: PsiElement) : LocalQuickFixAndIntentionActionOnPsiElement( - element - ) { - override fun getFamilyName(): String = DevKitKotlinBundle.message( - "inspections.forbidden.method.in.suspend.context.default.modality.state.fix.text") - - override fun getText(): String = familyName - - override fun isAvailable(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement): Boolean = - getCallExpression(startElement) != null - - override fun invoke(project: Project, file: PsiFile, editor: Editor?, startElement: PsiElement, endElement: PsiElement) { - val callExpression = getCallExpression(startElement)!! - val factory = KtPsiFactory(project) - - if (!isImported(contextModalityExt, callExpression.containingKtFile)) { - ImportInsertHelperImpl.addImport(project, callExpression.containingKtFile, contextModalityExt) - } - - // ?: because defaultModalityState is @NotNull, so it can be unexpected to replace with something nullable - val contextModalityCall = factory.createExpression( - "$CURRENT_COROUTINE_CONTEXT().${contextModalityExt.shortName()}() ?: $KOTLIN_TODO(\"Handle absence of ModalityState\")" - ) - val qualifiedExpression = callExpression.getQualifiedExpressionForSelector() - val expressionToReplace = qualifiedExpression ?: callExpression - val resultExpression = expressionToReplace.replace(contextModalityCall) - ShortenReferencesFacility.getInstance().shorten(resultExpression as KtElement) - } - } - private class NavigateToCallInSuspendFunction(callingElement: KtCallExpression) : IntentionAndQuickFixAction() { private val pointer: SmartPsiElementPointer = SmartPointerManager.createPointer(callingElement)