[kotlin] k2 introduce parameter: check if super callable should be modified

^KTIJ-29897 fixed

GitOrigin-RevId: 9b7330498d3310038ed136aee5c6873cb1a2b3d0
This commit is contained in:
Anna Kozlova
2024-05-12 17:48:41 +00:00
committed by intellij-monorepo-bot
parent 8c8ae99e5b
commit 477463f520
5 changed files with 42 additions and 5 deletions
@@ -3601,6 +3601,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/introduceParameter/lastParameterHasEolCommentAndTrailingComma.kt");
}
@TestMetadata("overrideFunction.kt")
public void testOverrideFunction() throws Exception {
runTest("testData/refactoring/introduceParameter/overrideFunction.kt");
}
@TestMetadata("partialSubstitution.kt")
public void testPartialSubstitution() throws Exception {
runTest("testData/refactoring/introduceParameter/partialSubstitution.kt");
@@ -0,0 +1,11 @@
// WITH_STDLIB
interface I {
fun f()
}
class A: I {
override fun f() {
val p = <selection>42</selection>
}
}
@@ -0,0 +1,11 @@
// WITH_STDLIB
interface I {
fun f(i: Int = 42)
}
class A: I {
override fun f(i: Int) {
val p = i
}
}
@@ -8,6 +8,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.refactoring.RefactoringActionHandler
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer
import com.intellij.usageView.UsageInfo
import com.intellij.util.SmartList
@@ -32,6 +33,7 @@ import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.utils.NamedArgumentUtils
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.k2.refactoring.changeSignature.*
import org.jetbrains.kotlin.idea.k2.refactoring.checkSuperMethods
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.K2ExtractableSubstringInfo
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.K2SemanticMatcher
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.extractionEngine.KotlinNameSuggester
@@ -361,13 +363,16 @@ class KotlinFirIntroduceParameterHandler(private val helper: KotlinIntroducePara
}
fun IntroduceParameterDescriptor<KtNamedDeclaration>.performRefactoring(onExit: (() -> Unit)? = null) {
val methodDescriptor = KotlinMethodDescriptor((callable as? KtClass)?.primaryConstructor ?: callable)
val superMethods = checkSuperMethods(callable, emptyList(), RefactoringBundle.message("to.refactor"))
val targetCallable = superMethods.filterIsInstance<KtNamedDeclaration>().firstOrNull() ?: return
val methodDescriptor = KotlinMethodDescriptor((targetCallable as? KtClass)?.primaryConstructor ?: targetCallable)
val changeInfo = KotlinChangeInfo(methodDescriptor)
val defaultValue = if (newArgumentValue is KtProperty) (newArgumentValue as KtProperty).initializer else newArgumentValue
if (!withDefaultValue) {
val parameters = callable.getValueParameters()
val parameters = targetCallable.getValueParameters()
val withReceiver = methodDescriptor.receiver != null
parametersToRemove
.map {
@@ -382,18 +387,18 @@ fun IntroduceParameterDescriptor<KtNamedDeclaration>.performRefactoring(onExit:
}
val parameterInfo = KotlinParameterInfo(
originalType = KotlinTypeInfo(newParameterTypeText, callable),
originalType = KotlinTypeInfo(newParameterTypeText, targetCallable),
name = newParameterName,
originalIndex = -1,
valOrVar = valVar,
defaultValueForCall = defaultValue,
defaultValueAsDefaultParameter = withDefaultValue,
defaultValue = if (withDefaultValue) defaultValue else null,
context = callable
context = targetCallable
)
changeInfo.addParameter(parameterInfo)
object : KotlinChangeSignatureProcessor(callable.project, changeInfo) {
object : KotlinChangeSignatureProcessor(targetCallable.project, changeInfo) {
override fun performRefactoring(usages: Array<out UsageInfo?>) {
super.performRefactoring(usages)
occurrencesToReplace.forEach {
@@ -355,6 +355,11 @@ public abstract class K2IntroduceParameterTestGenerated extends AbstractK2Introd
runTest("../../idea/tests/testData/refactoring/introduceParameter/lastParameterHasEolCommentAndTrailingComma.kt");
}
@TestMetadata("overrideFunction.kt")
public void testOverrideFunction() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceParameter/overrideFunction.kt");
}
@TestMetadata("partialSubstitution.kt")
public void testPartialSubstitution() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceParameter/partialSubstitution.kt");