diff --git a/ChangeLog.md b/ChangeLog.md index ced371087c0..bbe2b74e48f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -182,6 +182,7 @@ ###### New features - [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename - [`KT-8044`](https://youtrack.jetbrains.com/issue/KT-8044), [`KT-9432`](https://youtrack.jetbrains.com/issue/KT-9432) Support @JvmName annotation in rename refactoring +- [`KT-8512`](https://youtrack.jetbrains.com/issue/KT-8512) Support "Rename tests" options in Rename dialog ###### Issues fixed - [`KT-8541`](https://youtrack.jetbrains.com/issue/KT-8541), [`KT-8786`](https://youtrack.jetbrains.com/issue/KT-8786) Do now show 'Rename overloads' options if target function has no overloads diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/ConfigLibraryUtil.java b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/ConfigLibraryUtil.java index 57680054448..bf953a1ef86 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/ConfigLibraryUtil.java +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/ConfigLibraryUtil.java @@ -189,8 +189,8 @@ public class ConfigLibraryUtil { addLibrary(editor, module); } - public static void configureLibrariesByDirective(@NotNull Module module, String rootPath, String fileText) { - for (String libraryInfo : InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: ")) { + public static void configureLibraries(@NotNull Module module, String rootPath, List libraryInfos) { + for (String libraryInfo : libraryInfos) { int i = libraryInfo.indexOf('@'); String libraryName = libraryInfo.substring(0, i); String[] jarPaths = libraryInfo.substring(i + 1).split(";"); @@ -198,6 +198,29 @@ public class ConfigLibraryUtil { } } + public static void unconfigureLibrariesByName(@NotNull Module module, List libraryNames) { + for (Iterator iterator = libraryNames.iterator(); iterator.hasNext(); ) { + String libraryName = iterator.next(); + if (removeLibrary(module, libraryName)) { + iterator.remove(); + } + } + + if (!libraryNames.isEmpty()) throw new AssertionError("Couldn't find the following libraries: " + libraryNames); + } + + public static void unconfigureLibrariesByInfo(@NotNull Module module, List libraryInfos) { + List libraryNames = new ArrayList(); + for (String libraryInfo : libraryInfos) { + libraryNames.add(libraryInfo.substring(0, libraryInfo.indexOf('@'))); + } + unconfigureLibrariesByName(module, libraryNames); + } + + public static void configureLibrariesByDirective(@NotNull Module module, String rootPath, String fileText) { + configureLibraries(module, rootPath, InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: ")); + } + public static void unconfigureLibrariesByDirective(@NotNull Module module, String fileText) { List libraryNames = new ArrayList(); for (String libInfo : InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: ")) { @@ -207,13 +230,6 @@ public class ConfigLibraryUtil { libraryNames.add(libraryName); } - for (Iterator iterator = libraryNames.iterator(); iterator.hasNext(); ) { - String libraryName = iterator.next(); - if (removeLibrary(module, libraryName)) { - iterator.remove(); - } - } - - if (!libraryNames.isEmpty()) throw new AssertionError("Couldn't find the following libraries: " + libraryNames); + unconfigureLibrariesByName(module, libraryNames); } } diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 76cd6b1c581..61fb023ad44 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -434,6 +434,7 @@ + diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAutomaticTestRenamerFactory.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAutomaticTestRenamerFactory.kt new file mode 100644 index 00000000000..8a9fd622f1c --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/KotlinAutomaticTestRenamerFactory.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.refactoring.rename + +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiElement +import com.intellij.refactoring.rename.naming.AutomaticRenamer +import com.intellij.refactoring.rename.naming.AutomaticTestRenamerFactory +import com.intellij.usageView.UsageInfo +import org.jetbrains.kotlin.asJava.KtLightClass +import org.jetbrains.kotlin.asJava.toLightClass +import org.jetbrains.kotlin.psi.KtClassOrObject + +class KotlinAutomaticTestRenamerFactory : AutomaticTestRenamerFactory() { + private fun getPsiClass(element: PsiElement): PsiClass? { + return when (element) { + is KtLightClass -> element + is KtClassOrObject -> element.toLightClass() + else -> null + } + } + + override fun isApplicable(element: PsiElement): Boolean { + val psiClass = getPsiClass(element) ?: return false + return super.isApplicable(psiClass) + } + + override fun createRenamer(element: PsiElement, newName: String?, usages: MutableCollection): AutomaticRenamer { + return super.createRenamer(getPsiClass(element)!!, newName, usages) + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JBazBarNewFooTest.java b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JBazBarNewFooTest.java new file mode 100644 index 00000000000..3e22fb4ab79 --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JBazBarNewFooTest.java @@ -0,0 +1,10 @@ +package test; + +import org.junit.Test; + +public class JBazBarNewFooTest { + @Test + void testFoo() { + + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JFooBarNewBazTestCase.java b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JFooBarNewBazTestCase.java new file mode 100644 index 00000000000..e5d2e93321b --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/JFooBarNewBazTestCase.java @@ -0,0 +1,7 @@ +package test; + +import junit.framework.TestCase; + +public class JFooBarNewBazTestCase extends TestCase { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/test.kt b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/test.kt new file mode 100644 index 00000000000..45db44ba92c --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/after/test/test.kt @@ -0,0 +1,16 @@ +package test + +import junit.framework.TestCase +import org.junit.Test + +class BarNew { + +} + +class FooBarNewBazTestCase : TestCase() { + +} + +class BazBarNewFooTest { + @Test fun testFoo() {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test new file mode 100644 index 00000000000..5c70e743ccb --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test @@ -0,0 +1,7 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test/test.kt", + "newName": "BarNew", + "withRuntime": "true", + "libraries": ["JUnit@lib/junit-4.12.jar"] +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JBazBarFooTest.java b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JBazBarFooTest.java new file mode 100644 index 00000000000..52affe4a236 --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JBazBarFooTest.java @@ -0,0 +1,10 @@ +package test; + +import org.junit.Test; + +public class JBazBarFooTest { + @Test + void testFoo() { + + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JFooBarBazTestCase.java b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JFooBarBazTestCase.java new file mode 100644 index 00000000000..fc06b3fbbd1 --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/JFooBarBazTestCase.java @@ -0,0 +1,7 @@ +package test; + +import junit.framework.TestCase; + +public class JFooBarBazTestCase extends TestCase { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/test.kt b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/test.kt new file mode 100644 index 00000000000..663824f30d3 --- /dev/null +++ b/idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/before/test/test.kt @@ -0,0 +1,16 @@ +package test + +import junit.framework.TestCase +import org.junit.Test + +class /*rename*/Bar { + +} + +class FooBarBazTestCase : TestCase() { + +} + +class BazBarFooTest { + @Test fun testFoo() {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt index 4087e21265d..333b65266d3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/AbstractRenameTest.kt @@ -41,6 +41,7 @@ import com.intellij.refactoring.rename.RenameProcessor import com.intellij.refactoring.rename.RenamePsiElementProcessor import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintException +import com.intellij.testFramework.PlatformTestUtil import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze @@ -99,6 +100,9 @@ abstract class AbstractRenameTest : KotlinMultiFileTestCase() { ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk()) } + val libraryInfos = renameObject.getAsJsonArray("libraries")?.map { it.asString!! } ?: emptyList() + ConfigLibraryUtil.configureLibraries(myModule, PlatformTestUtil.getCommunityPath(), libraryInfos) + val fixtureClasses = renameObject.getAsJsonArray("fixtureClasses")?.map { it.asString } ?: emptyList() try { @@ -150,6 +154,7 @@ abstract class AbstractRenameTest : KotlinMultiFileTestCase() { } finally { fixtureClasses.forEach { TestFixtureExtension.unloadFixture(it) } + ConfigLibraryUtil.unconfigureLibrariesByInfo(myModule, libraryInfos) } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java index bbece237a4d..b7840634ed3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -53,6 +53,12 @@ public class RenameTestGenerated extends AbstractRenameTest { doTest(fileName); } + @TestMetadata("automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test") + public void testAutomaticRenamerKotlinTestClass_AutomaticRenamerKotlinTestClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test"); + doTest(fileName); + } + @TestMetadata("automaticRenamerOverloads/package.test") public void testAutomaticRenamerOverloads_Package() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerOverloads/package.test");