From 5baa2e0e7cc8ce4b342cdc7deefa69c65bb1410e Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Mon, 22 Mar 2021 18:12:55 +0000 Subject: [PATCH] FIR IDE: Simplify the diagnosticFixFactory() API by removing HLApplicatorWithTargetAndInput and making HLQuickFix public. --- .../fixes/HLApplicatorWithTargetAndInput.kt | 19 -------------- .../fir/api/fixes/HLDiagnosticFixFactory.kt | 25 ++++++++----------- .../kotlin/idea/fir/api/fixes/HLQuickFix.kt | 2 +- .../quickfix/fixes/ReplaceCallFixFactories.kt | 14 +++-------- 4 files changed, 15 insertions(+), 45 deletions(-) delete mode 100644 idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLApplicatorWithTargetAndInput.kt diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLApplicatorWithTargetAndInput.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLApplicatorWithTargetAndInput.kt deleted file mode 100644 index aa5dd0163b4..00000000000 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLApplicatorWithTargetAndInput.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.fir.api.fixes - -import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicator -import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicatorInput - -class HLApplicatorWithTargetAndInput( - val applicator: HLApplicator, - private val targetAndInput: HLApplicatorTargetWithInput, -) { - operator fun component1() = applicator - operator fun component2() = targetAndInput.target - operator fun component3() = targetAndInput.input -} diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLDiagnosticFixFactory.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLDiagnosticFixFactory.kt index 5c37c0d6b07..cf36d95d444 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLDiagnosticFixFactory.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLDiagnosticFixFactory.kt @@ -13,30 +13,28 @@ import org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSession import org.jetbrains.kotlin.idea.frontend.api.diagnostics.KtDiagnosticWithPsi sealed class HLDiagnosticFixFactory, TARGET_PSI : PsiElement, INPUT : HLApplicatorInput> { - abstract fun KtAnalysisSession.createTargets(diagnostic: DIAGNOSTIC): List> + abstract fun KtAnalysisSession.createQuickFixes(diagnostic: DIAGNOSTIC): List> } private class HLDiagnosticFixFactoryWithFixedApplicator, TARGET_PSI : PsiElement, INPUT : HLApplicatorInput>( private val applicator: HLApplicator, - private val createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> + private val createTargets: KtAnalysisSession.(DIAGNOSTIC) -> List> ) : HLDiagnosticFixFactory() { - override fun KtAnalysisSession.createTargets(diagnostic: DIAGNOSTIC): List> = - createQuickFixes.invoke(this, diagnostic).map { targetAndInput -> HLApplicatorWithTargetAndInput(applicator, targetAndInput) } + override fun KtAnalysisSession.createQuickFixes(diagnostic: DIAGNOSTIC): List> = + createTargets.invoke(this, diagnostic).map { (target, input) -> HLQuickFix(target, input, applicator) } } private class HLDiagnosticFixFactoryWithVariableApplicator, TARGET_PSI : PsiElement, INPUT : HLApplicatorInput>( - private val createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> + private val createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> ) : HLDiagnosticFixFactory() { - override fun KtAnalysisSession.createTargets(diagnostic: DIAGNOSTIC): List> = + override fun KtAnalysisSession.createQuickFixes(diagnostic: DIAGNOSTIC): List> = createQuickFixes.invoke(this, diagnostic) } internal fun > KtAnalysisSession.createPlatformQuickFixes( diagnostic: DIAGNOSTIC, factory: HLDiagnosticFixFactory -): List = with(factory) { - createTargets(diagnostic).map { (applicator, target, input) -> HLQuickFix(target, input, applicator) } -} +): List = with(factory) { createQuickFixes(diagnostic) } /** * Returns a [HLDiagnosticFixFactory] that creates targets and inputs ([HLApplicatorTargetWithInput]) from a diagnostic. @@ -44,15 +42,14 @@ internal fun > KtAnalysisSession.cr */ fun , TARGET_PSI : PsiElement, INPUT : HLApplicatorInput> diagnosticFixFactory( applicator: HLApplicator, - createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> + createTargets: KtAnalysisSession.(DIAGNOSTIC) -> List> ): HLDiagnosticFixFactory = - HLDiagnosticFixFactoryWithFixedApplicator(applicator, createQuickFixes) + HLDiagnosticFixFactoryWithFixedApplicator(applicator, createTargets) /** - * Returns a [HLDiagnosticFixFactory] that creates applicators, targets, and inputs ([HLApplicatorWithTargetAndInput]) from a diagnostic. - * The targets and inputs are consumed by the corresponding applicator to apply fixes. + * Returns a [HLDiagnosticFixFactory] that creates [HLQuickFix]es from a diagnostic. */ fun , TARGET_PSI : PsiElement, INPUT : HLApplicatorInput> diagnosticFixFactory( - createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> + createQuickFixes: KtAnalysisSession.(DIAGNOSTIC) -> List> ): HLDiagnosticFixFactory = HLDiagnosticFixFactoryWithVariableApplicator(createQuickFixes) diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLQuickFix.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLQuickFix.kt index 314ceb1e860..40dc648423a 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLQuickFix.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/fixes/HLQuickFix.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicatorInput import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction import org.jetbrains.kotlin.psi.KtFile -internal class HLQuickFix( +class HLQuickFix( target: PSI, private val input: INPUT, val applicator: HLApplicator, diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ReplaceCallFixFactories.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ReplaceCallFixFactories.kt index 4ebf78f6316..70be62a31d7 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ReplaceCallFixFactories.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/fixes/ReplaceCallFixFactories.kt @@ -9,9 +9,8 @@ import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicatorInput import org.jetbrains.kotlin.idea.fir.api.applicator.applicatorByQuickFix -import org.jetbrains.kotlin.idea.fir.api.fixes.HLApplicatorWithTargetAndInput +import org.jetbrains.kotlin.idea.fir.api.fixes.HLQuickFix import org.jetbrains.kotlin.idea.fir.api.fixes.diagnosticFixFactory -import org.jetbrains.kotlin.idea.fir.api.fixes.withInput import org.jetbrains.kotlin.idea.frontend.api.fir.diagnostics.KtFirDiagnostic import org.jetbrains.kotlin.idea.frontend.api.types.KtTypeNullability import org.jetbrains.kotlin.idea.frontend.api.types.KtTypeWithNullability @@ -50,20 +49,13 @@ object ReplaceCallFixFactories { } when (val psi = diagnostic.psi) { - is KtDotQualifiedExpression -> listOf( - HLApplicatorWithTargetAndInput(replaceWithSafeCallFixApplicator, psi withInput Input(psi.shouldHaveNotNullType())) - ) + is KtDotQualifiedExpression -> listOf(HLQuickFix(psi, Input(psi.shouldHaveNotNullType()), replaceWithSafeCallFixApplicator)) is KtNameReferenceExpression -> { // TODO: As a safety precaution, resolve the expression to determine if it is a call with an implicit receiver. // This is a defensive check to ensure that the diagnostic was reported on such a call and not some other name reference. // This isn't strictly needed because FIR checkers aren't reporting on wrong elements, but ReplaceWithSafeCallFixFactory // in FE1.0 does so. - listOf( - HLApplicatorWithTargetAndInput( - replaceImplicitReceiverCallFixApplicator, - psi withInput Input(psi.shouldHaveNotNullType()) - ) - ) + listOf(HLQuickFix(psi, Input(psi.shouldHaveNotNullType()), replaceImplicitReceiverCallFixApplicator)) } else -> emptyList() }