From 720c97c7954ba1379f021ac21fa1f2f5695896e0 Mon Sep 17 00:00:00 2001 From: Bart van Helvert Date: Wed, 26 Apr 2023 22:59:41 +0200 Subject: [PATCH] [kotlin] Move `OuterInstanceReferenceUsageInfo` to common refactoring module ^KTIJ-23273 Also makes inheritor element fields non-nullable GitOrigin-RevId: 8ca0a3ebb7cccc43b3cfd8bb2d17ac1fa06e5229 --- .../MoveMemberToCompanionObjectIntention.kt | 5 +- .../MoveDeclarationsDelegate.kt | 5 +- .../kotlin/idea/refactoring/move/moveUtils.kt | 49 ----------------- .../move/OuterInstanceReferenceUsageInfo.kt | 53 +++++++++++++++++++ 4 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/move/OuterInstanceReferenceUsageInfo.kt diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberToCompanionObjectIntention.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberToCompanionObjectIntention.kt index 880176849e63..8ce1e15652ab 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberToCompanionObjectIntention.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberToCompanionObjectIntention.kt @@ -295,11 +295,12 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention { - usage.expression?.replace(newOuterInstanceRef) + usage.expression.replace(newOuterInstanceRef) } is OuterInstanceReferenceUsageInfo.ImplicitReceiver -> { - usage.callElement?.let { it.replace(ktPsiFactory.createExpressionByPattern("$0.$1", newOuterInstanceRef, it)) } + val newElem = ktPsiFactory.createExpressionByPattern("$0.$1", newOuterInstanceRef, usage.callElement) + usage.callElement.replace(newElem) } } } diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveDeclarationsDelegate.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveDeclarationsDelegate.kt index f35b6bd376b7..cc130a79b6f5 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveDeclarationsDelegate.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveDeclarationsDelegate.kt @@ -160,10 +160,11 @@ sealed class MoveDeclarationsDelegate { when (usage) { is OuterInstanceReferenceUsageInfo.ExplicitThis -> { - usage.expression?.replace(newOuterInstanceRef) + usage.expression.replace(newOuterInstanceRef) } is OuterInstanceReferenceUsageInfo.ImplicitReceiver -> { - usage.callElement?.let { it.replace(psiFactory.createExpressionByPattern("$0.$1", outerInstanceParameterName, it)) } + val newElem = psiFactory.createExpressionByPattern("$0.$1", outerInstanceParameterName, usage.callElement) + usage.callElement.replace(newElem) } } } diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt index c6fcd285452a..d26c02376088 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt @@ -22,7 +22,6 @@ import com.intellij.refactoring.util.NonCodeUsageInfo import com.intellij.usageView.UsageInfo import com.intellij.util.IncorrectOperationException import com.intellij.util.SmartList -import com.intellij.util.containers.MultiMap import org.jetbrains.kotlin.asJava.unwrapped import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.KotlinFileType @@ -519,54 +518,6 @@ fun postProcessMoveUsages( var KtFile.updatePackageDirective: Boolean? by UserDataProperty(Key.create("UPDATE_PACKAGE_DIRECTIVE")) -sealed class OuterInstanceReferenceUsageInfo(element: PsiElement, private val isIndirectOuter: Boolean) : UsageInfo(element) { - open fun reportConflictIfAny(conflicts: MultiMap): Boolean { - val element = element ?: return false - - if (isIndirectOuter) { - conflicts.putValue(element, KotlinBundle.message("text.indirect.outer.instances.will.not.be.extracted.0", element.text)) - return true - } - - return false - } - - class ExplicitThis( - expression: KtThisExpression, - isIndirectOuter: Boolean - ) : OuterInstanceReferenceUsageInfo(expression, isIndirectOuter) { - val expression: KtThisExpression? - get() = element as? KtThisExpression - } - - class ImplicitReceiver( - callElement: KtElement, - isIndirectOuter: Boolean, - private val isDoubleReceiver: Boolean - ) : OuterInstanceReferenceUsageInfo(callElement, isIndirectOuter) { - val callElement: KtElement? - get() = element as? KtElement - - override fun reportConflictIfAny(conflicts: MultiMap): Boolean { - if (super.reportConflictIfAny(conflicts)) return true - - val fullCall = callElement?.let { it.getQualifiedExpressionForSelector() ?: it } ?: return false - return when { - fullCall is KtQualifiedExpression -> { - conflicts.putValue(fullCall, KotlinBundle.message("text.qualified.call.will.not.be.processed.0", fullCall.text)) - true - } - - isDoubleReceiver -> { - conflicts.putValue(fullCall, KotlinBundle.message("text.member.extension.call.will.not.be.processed.0", fullCall.text)) - true - } - else -> false - } - } - } -} - @JvmOverloads fun traverseOuterInstanceReferences( member: KtNamedDeclaration, diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/move/OuterInstanceReferenceUsageInfo.kt b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/move/OuterInstanceReferenceUsageInfo.kt new file mode 100644 index 000000000000..c8635d961302 --- /dev/null +++ b/plugins/kotlin/refactorings/kotlin.refactorings.common/src/org/jetbrains/kotlin/idea/refactoring/move/OuterInstanceReferenceUsageInfo.kt @@ -0,0 +1,53 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.refactoring.move + +import com.intellij.psi.PsiElement +import com.intellij.usageView.UsageInfo +import com.intellij.util.containers.MultiMap +import org.jetbrains.kotlin.idea.base.resources.KotlinBundle +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtQualifiedExpression +import org.jetbrains.kotlin.psi.KtThisExpression +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector + +sealed class OuterInstanceReferenceUsageInfo(element: PsiElement, private val isIndirectOuter: Boolean) : UsageInfo(element) { + open fun reportConflictIfAny(conflicts: MultiMap): Boolean { + val element = element ?: return false + if (isIndirectOuter) { + conflicts.putValue(element, KotlinBundle.message("text.indirect.outer.instances.will.not.be.extracted.0", element.text)) + return true + } + return false + } + + class ExplicitThis( + expression: KtThisExpression, + isIndirectOuter: Boolean + ) : OuterInstanceReferenceUsageInfo(expression, isIndirectOuter) { + val expression: KtThisExpression get() = element as KtThisExpression + } + + class ImplicitReceiver( + callElement: KtElement, + isIndirectOuter: Boolean, + private val isDoubleReceiver: Boolean + ) : OuterInstanceReferenceUsageInfo(callElement, isIndirectOuter) { + val callElement: KtElement get() = element as KtElement + + override fun reportConflictIfAny(conflicts: MultiMap): Boolean { + if (super.reportConflictIfAny(conflicts)) return true + val fullCall = callElement.getQualifiedExpressionForSelector() ?: callElement + return when { + fullCall is KtQualifiedExpression -> { + conflicts.putValue(fullCall, KotlinBundle.message("text.qualified.call.will.not.be.processed.0", fullCall.text)) + true + } + isDoubleReceiver -> { + conflicts.putValue(fullCall, KotlinBundle.message("text.member.extension.call.will.not.be.processed.0", fullCall.text)) + true + } + else -> false + } + } + } +} \ No newline at end of file