Uast: making convertDeclaration always convert the originalElement (KT-35848)

This commit is contained in:
Nicolay Mitropolsky
2020-03-30 15:24:59 +03:00
parent 61ad32f012
commit f98a1de7c3
2 changed files with 43 additions and 5 deletions
@@ -487,22 +487,23 @@ internal object KotlinConverter {
givenParent: UElement?,
expectedTypes: Array<out Class<out UElement>>
): UElement? {
val original = element.originalElement
fun <P : PsiElement> build(ctor: (P, UElement?) -> UElement): () -> UElement? = {
@Suppress("UNCHECKED_CAST")
ctor(element as P, givenParent)
ctor(original as P, givenParent)
}
fun <P : PsiElement, K : KtElement> buildKt(ktElement: K, ctor: (P, K, UElement?) -> UElement): () -> UElement? = {
@Suppress("UNCHECKED_CAST")
ctor(element as P, ktElement, givenParent)
ctor(original as P, ktElement, givenParent)
}
fun <P : PsiElement, K : KtElement> buildKtOpt(ktElement: K?, ctor: (P, K?, UElement?) -> UElement): () -> UElement? = {
@Suppress("UNCHECKED_CAST")
ctor(element as P, ktElement, givenParent)
ctor(original as P, ktElement, givenParent)
}
val original = element.originalElement
return with(expectedTypes) {
when (original) {
is KtLightMethod -> el<UMethod>(build(KotlinUMethod.Companion::create)) // .Companion is needed because of KT-13934
@@ -562,7 +563,7 @@ internal object KotlinConverter {
is KtProperty ->
if (original.isLocal) {
KotlinConverter.convertPsiElement(element, givenParent, expectedTypes)
KotlinConverter.convertPsiElement(original, givenParent, expectedTypes)
} else {
convertNonLocalProperty(original, givenParent, expectedTypes).firstOrNull()
}
@@ -19,7 +19,10 @@ package org.jetbrains.uast.test.kotlin
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiLanguageInjectionHost
import com.intellij.refactoring.rename.RenameProcessor
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.UsefulTestCase
import junit.framework.TestCase
import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.idea.core.copied
@@ -169,6 +172,40 @@ class KotlinDetachedUastTest : KotlinLightCodeInsightFixtureTestCase() {
TestCase.assertNull(ktFunctionDetached.toUElementOfType<UMethod>())
}
fun testRenameHandlers() {
myFixture.configureByText(
"JavaClass.java", """
class JavaClass {
void foo(){
new MyClass().getBar();
}
}
"""
)
myFixture.configureByText(
"MyClass.kt", """
class MyClass() {
val b<caret>ar = 42
}
"""
)
val element = myFixture.elementAtCaret
val substitution =
RenamePsiElementProcessor.forElement(element).substituteElementToRename(element, editor).orFail("no element")
val linkedMapOf = linkedMapOf<PsiElement, String>()
RenameProcessor(project, substitution, "newName", false, false)
.prepareRenaming(element, "newName", linkedMapOf)
UsefulTestCase.assertTrue(linkedMapOf.any())
for ((k, _) in linkedMapOf) {
TestCase.assertEquals(element, k.toUElement()?.sourcePsi)
}
}
}