[kotlin] rename: ensure replace takes an element of a corresponding type

^KTIJ-24219 fixed

add check to test that psi structure after modification is the same to the psi structure from text: this guarantees that psi is replaced consistently and no reparse is needed to produce correct psi

GitOrigin-RevId: f97ff4ec92401e94a1941ad81f1a7f4d0093af29
This commit is contained in:
Anna Kozlova
2023-11-28 22:38:16 +00:00
committed by intellij-monorepo-bot
parent f290bb1b14
commit 0badf4c448
7 changed files with 45 additions and 12 deletions
@@ -30,6 +30,7 @@ import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintException
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.PsiTestUtil
import com.intellij.testFramework.UsefulTestCase
import com.intellij.testFramework.fixtures.CodeInsightTestUtil
import org.jetbrains.kotlin.asJava.finder.KtLightPackage
@@ -365,6 +366,7 @@ abstract class AbstractRenameTest : KotlinLightCodeInsightFixtureTestCase() {
RenamePsiElementProcessor.forElement(psiElement).substituteElementToRename(psiElement, null)
runRenameProcessor(context.project, newName, substitution, renameParamsObject, true, true)
PsiTestUtil.checkFileStructure(ktFile)
}
}
@@ -1263,6 +1263,11 @@ public class RenameTestGenerated extends AbstractRenameTest {
runTest("testData/refactoring/rename/renamePackageWithQuotation/renamePackageWithQuotation.test");
}
@TestMetadata("renamePlus1/plus1.test")
public void testRenamePlus1_Plus1() throws Exception {
runTest("testData/refactoring/rename/renamePlus1/plus1.test");
}
@TestMetadata("renamePlusAssign/plusAssign.test")
public void testRenamePlusAssign_PlusAssign() throws Exception {
runTest("testData/refactoring/rename/renamePlusAssign/plusAssign.test");
@@ -0,0 +1,7 @@
data class Point(val x:Int, val y: Int) {
fun foo(other: Point): Point {
return Point(x + other.x, y + other.y)
}
}
fun test(p: Point) = p.foo(p)
@@ -0,0 +1,7 @@
data class Point(val x:Int, val y: Int) {
operator fun plus(other: Point): Point {
return Point(x + other.x, y + other.y)
}
}
fun test(p: Point) = p + p
@@ -0,0 +1,8 @@
{
"type": "KOTLIN_FUNCTION",
"classId": "/Point",
"oldName": "plus",
"newName": "foo",
"mainFile": "plus.kt",
"enabledInK2": true
}
@@ -209,22 +209,21 @@ abstract class KtReferenceMutateServiceBase : KtReferenceMutateService {
}
}
val psiFactory = KtPsiFactory(expression)
val psiFactory = KtPsiFactory(expression.project)
val nameElement = expression.getReferencedNameElement()
val elementType = nameElement.node.elementType
val opExpression = if (elementType is KtToken && OperatorConventions.getNameForOperationSymbol(elementType) != null) {
expression.parent as? KtOperationExpression
} else null
val element = expression.project.extensionArea.getExtensionPoint(SimpleNameReferenceExtension.EP_NAME).extensions
.asSequence()
.map { it.handleElementRename(this, psiFactory, newElementName) }
.firstOrNull { it != null } ?: psiFactory.createNameIdentifier(newElementName.quoteIfNeeded())
.firstOrNull { it != null } ?: if (opExpression != null) psiFactory.createSimpleName(newElementName.quoteIfNeeded()) else psiFactory.createNameIdentifier(newElementName.quoteIfNeeded())
val nameElement = expression.getReferencedNameElement()
val elementType = nameElement.node.elementType
if (elementType is KtToken && OperatorConventions.getNameForOperationSymbol(elementType) != null) {
val opExpression = expression.parent as? KtOperationExpression
if (opExpression != null) {
val (newExpression, newNameElement) = convertOperatorToFunctionCall(opExpression)
newNameElement.replace(element)
return newExpression
}
if (opExpression != null) {
val (newExpression, newNameElement) = convertOperatorToFunctionCall(opExpression)
newNameElement.replace(psiFactory.createSimpleName(newElementName.quoteIfNeeded()))
return newExpression
}
if (element.node.elementType == KtTokens.IDENTIFIER) {
@@ -1263,6 +1263,11 @@ public class FirRenameTestGenerated extends AbstractFirRenameTest {
runTest("../../idea/tests/testData/refactoring/rename/renamePackageWithQuotation/renamePackageWithQuotation.test");
}
@TestMetadata("renamePlus1/plus1.test")
public void testRenamePlus1_Plus1() throws Exception {
runTest("../../idea/tests/testData/refactoring/rename/renamePlus1/plus1.test");
}
@TestMetadata("renamePlusAssign/plusAssign.test")
public void testRenamePlusAssign_PlusAssign() throws Exception {
runTest("../../idea/tests/testData/refactoring/rename/renamePlusAssign/plusAssign.test");