mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[devkit] IDEA-309527: remove quick fix for defaultModalityState
GitOrigin-RevId: 83e2df6ede2c6502a3e14a24b63ba9c5f62db4b5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8601919df1
commit
bfa4d04aca
-35
@@ -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
|
||||
|
||||
@@ -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
|
||||
-35
@@ -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<KtCallExpression> = SmartPointerManager.createPointer(callingElement)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user