KTIJ-33579 [kotlin] Use ImportContextWithFixedReceiverType in ComponentFunctionImportQuickFixFactory to properly handle destructuring in lambdas

^KTIJ-33579 Fixed

GitOrigin-RevId: b3f921bba5ef097f1998692aed8fac2e99c303b5
This commit is contained in:
Roman Golyshev
2025-04-12 14:32:17 +00:00
committed by intellij-monorepo-bot
parent 999141b959
commit c5c7b01cd0
3 changed files with 15 additions and 5 deletions
@@ -8,6 +8,7 @@ import org.jetbrains.kotlin.idea.base.analysis.api.utils.KtSymbolFromIndexProvid
import org.jetbrains.kotlin.idea.k2.codeinsight.fixes.imprt.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtParameter
internal object ComponentFunctionImportQuickFixFactory : AbstractImportQuickFixFactory() {
override fun KaSession.detectPositionContext(diagnostic: KaDiagnosticWithPsi<*>): ImportContext? =
@@ -15,7 +16,20 @@ internal object ComponentFunctionImportQuickFixFactory : AbstractImportQuickFixF
is KaFirDiagnostic.ComponentFunctionMissing,
is KaFirDiagnostic.ComponentFunctionAmbiguity -> {
val destructuredExpression = diagnostic.psi as? KtExpression ?: return null
DefaultImportContext(destructuredExpression, ImportPositionTypeAndReceiver.OperatorCall(destructuredExpression))
val destructuredType = when (destructuredExpression) {
// destructuring in lambda parameter position (e.g. `foo { (a, b) -> ... }`)
is KtParameter -> destructuredExpression.returnType
// regular assignment destructuring (e.g. `val (a, b) = ...`)
else -> destructuredExpression.expressionType
} ?: return null
ImportContextWithFixedReceiverType(
destructuredExpression,
ImportPositionType.OperatorCall,
explicitReceiverType = destructuredType,
)
}
else -> null
@@ -1,7 +1,6 @@
// FILE: first.before.kt
// "Import operator 'Some.component1'" "true"
// IGNORE_K1
// IGNORE_K2
package testing
@@ -26,7 +25,6 @@ operator fun Some.component2() = 3
// FILE: first.after.kt
// "Import operator 'Some.component1'" "true"
// IGNORE_K1
// IGNORE_K2
package testing
@@ -1,7 +1,6 @@
// FILE: first.before.kt
// "Import operator 'Some.component2'" "true"
// IGNORE_K1
// IGNORE_K2
package testing
@@ -26,7 +25,6 @@ operator fun Some.component2() = 3
// FILE: first.after.kt
// "Import operator 'Some.component2'" "true"
// IGNORE_K1
// IGNORE_K2
package testing