From f98a1de7c3a6cc0f6a6aa6b13b4c7c675d673da7 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Mon, 30 Mar 2020 15:24:59 +0300 Subject: [PATCH] Uast: making `convertDeclaration` always convert the `originalElement` (KT-35848) --- .../uast/kotlin/KotlinUastLanguagePlugin.kt | 11 +++--- .../tests/KotlinDetachedUastTest.kt | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt index 13eb45f594e..21538d95991 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt @@ -487,22 +487,23 @@ internal object KotlinConverter { givenParent: UElement?, expectedTypes: Array> ): UElement? { + val original = element.originalElement + fun

build(ctor: (P, UElement?) -> UElement): () -> UElement? = { @Suppress("UNCHECKED_CAST") - ctor(element as P, givenParent) + ctor(original as P, givenParent) } fun

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

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(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() } diff --git a/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt b/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt index 6ed6d4eb4d1..e0ff44d190f 100644 --- a/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt +++ b/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt @@ -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()) } + fun testRenameHandlers() { + myFixture.configureByText( + "JavaClass.java", """ + class JavaClass { + void foo(){ + new MyClass().getBar(); + } + } + """ + ) + + myFixture.configureByText( + "MyClass.kt", """ + class MyClass() { + val bar = 42 + } + """ + ) + + val element = myFixture.elementAtCaret + + val substitution = + RenamePsiElementProcessor.forElement(element).substituteElementToRename(element, editor).orFail("no element") + val linkedMapOf = linkedMapOf() + RenameProcessor(project, substitution, "newName", false, false) + .prepareRenaming(element, "newName", linkedMapOf) + + UsefulTestCase.assertTrue(linkedMapOf.any()) + + for ((k, _) in linkedMapOf) { + TestCase.assertEquals(element, k.toUElement()?.sourcePsi) + } + } + }