From 5609645156de79309da533940aae67728074babf Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Mon, 22 Mar 2021 17:27:33 -0700 Subject: [PATCH] FIR IDE: tweak nullability of applyTo It looks like `project` can never be null --- .../idea/fir/api/applicator/HLApplicator.kt | 22 +++++++++++-------- .../CallableReturnTypeUpdaterApplicator.kt | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/applicator/HLApplicator.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/applicator/HLApplicator.kt index 7548cb889e0..95929220f62 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/applicator/HLApplicator.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/applicator/HLApplicator.kt @@ -28,7 +28,7 @@ sealed class HLApplicator { * @param psi a [PsiElement] to apply fix to * @param input additional data needed to apply the fix, the [input] can be collected by [HLApplicatorInputProvider */ - fun applyTo(psi: PSI, input: INPUT, project: Project?, editor: Editor?) = ForbidKtResolve.forbidResolveIn("HLApplicator.applyTo") { + fun applyTo(psi: PSI, input: INPUT, project: Project, editor: Editor?) = ForbidKtResolve.forbidResolveIn("HLApplicator.applyTo") { applyToImpl(psi, input, project, editor) } @@ -57,7 +57,7 @@ sealed class HLApplicator { getFamilyNameImpl() } - protected abstract fun applyToImpl(psi: PSI, input: INPUT, project: Project?, editor: Editor?) + protected abstract fun applyToImpl(psi: PSI, input: INPUT, project: Project, editor: Editor?) protected abstract fun isApplicableByPsiImpl(psi: PSI): Boolean protected abstract fun getActionNameImpl(psi: PSI, input: INPUT): String protected abstract fun getFamilyNameImpl(): String @@ -95,12 +95,12 @@ fun HLApplicator( - val applyTo: (PSI, INPUT, Project?, Editor?) -> Unit, + val applyTo: (PSI, INPUT, Project, Editor?) -> Unit, val isApplicableByPsi: (PSI) -> Boolean, val getActionName: (PSI, INPUT) -> String, val getFamilyName: () -> String, ) : HLApplicator() { - override fun applyToImpl(psi: PSI, input: INPUT, project: Project?, editor: Editor?) { + override fun applyToImpl(psi: PSI, input: INPUT, project: Project, editor: Editor?) { applyTo.invoke(psi, input, project, editor) } @@ -117,7 +117,7 @@ internal class HLApplicatorImpl( class HLApplicatorBuilder internal constructor( @PrivateForInline - var applyTo: ((PSI, INPUT, Project?, Editor?) -> Unit)? = null, + var applyTo: ((PSI, INPUT, Project, Editor?) -> Unit)? = null, private var isApplicableByPsi: ((PSI) -> Boolean)? = null, private var getActionName: ((PSI, INPUT) -> String)? = null, private var getFamilyName: (() -> String)? = null @@ -144,10 +144,14 @@ class HLApplicatorBuilder internal } @OptIn(PrivateForInline::class) - fun applyTo(doApply: (PSI, INPUT, Project?, Editor?) -> Unit) { - applyTo = doApply + fun applyToWithEditorRequired(doApply: (PSI, INPUT, Project, Editor) -> Unit) { + applyTo = { element, data, project, editor -> if (editor != null) doApply(element, data, project, editor) } } + @OptIn(PrivateForInline::class) + fun applyTo(doApply: (PSI, INPUT, Project, Editor?) -> Unit) { + applyTo = doApply + } @OptIn(PrivateForInline::class) fun applyTo(doApply: (PSI, INPUT) -> Unit) { @@ -155,7 +159,7 @@ class HLApplicatorBuilder internal } @OptIn(PrivateForInline::class) - fun applyTo(doApply: (PSI, INPUT, Project?) -> Unit) { + fun applyTo(doApply: (PSI, INPUT, Project) -> Unit) { applyTo = { element, data, project, _ -> doApply(element, data, project) } } @@ -206,7 +210,7 @@ fun Boolean = { true }, quickFixByInput: (PSI, INPUT) -> QUICK_FIX, ): HLApplicator = HLApplicatorImpl( - applyTo = { psi, input, project, editor -> quickFixByInput(psi, input).invoke(project ?: psi.project, editor, psi.containingFile) }, + applyTo = { psi, input, project, editor -> quickFixByInput(psi, input).invoke(project, editor, psi.containingFile) }, isApplicableByPsi = isApplicableByPsi, getActionName = { psi, input -> quickFixByInput(psi, input).text }, getFamilyName = getFamilyName diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/applicators/CallableReturnTypeUpdaterApplicator.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/applicators/CallableReturnTypeUpdaterApplicator.kt index 43029164383..5bdd848ced9 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/applicators/CallableReturnTypeUpdaterApplicator.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/applicators/CallableReturnTypeUpdaterApplicator.kt @@ -25,7 +25,7 @@ object CallableReturnTypeUpdaterApplicator { applyTo { declaration, type, project -> val newTypeRef = if (!declaration.isProcedure(type)) { // TODO use longTypeRepresentation and then shorten - KtPsiFactory(project ?: declaration.project).createType(type.shortTypeRepresentation) + KtPsiFactory(project).createType(type.shortTypeRepresentation) } else null runWriteAction { declaration.typeReference = newTypeRef