diff --git a/.idea/libraries/testng_plugin.xml b/.idea/libraries/testng_plugin.xml index cacf809be54..e27b3d32860 100644 --- a/.idea/libraries/testng_plugin.xml +++ b/.idea/libraries/testng_plugin.xml @@ -1,5 +1,8 @@ + + + diff --git a/annotations/com/intellij/refactoring/annotations.xml b/annotations/com/intellij/refactoring/annotations.xml new file mode 100644 index 00000000000..489d1ad655d --- /dev/null +++ b/annotations/com/intellij/refactoring/annotations.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/annotations/com/intellij/testFramework/annotations.xml b/annotations/com/intellij/testFramework/annotations.xml index 128954237ea..902f6d4f9df 100644 --- a/annotations/com/intellij/testFramework/annotations.xml +++ b/annotations/com/intellij/testFramework/annotations.xml @@ -7,4 +7,10 @@ name='com.intellij.testFramework.LightProjectDescriptor void configureModule(com.intellij.openapi.module.Module, com.intellij.openapi.roots.ModifiableRootModel, com.intellij.openapi.roots.ContentEntry) 1'> + + + + + + diff --git a/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test b/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test index 2edc8b81ae1..890ec0bac53 100644 --- a/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test +++ b/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test @@ -1 +1,5 @@ -// RENAME: JAVA_CLASS->testing.SomeClass->NewName \ No newline at end of file +{ + "type": "JAVA_CLASS", + "classFQN": "testing.SomeClass", + "newName": "NewName" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameJavaInterface/renameJavaInterface.test b/idea/testData/refactoring/rename/renameJavaInterface/renameJavaInterface.test index 09526210938..3f6bba935f0 100644 --- a/idea/testData/refactoring/rename/renameJavaInterface/renameJavaInterface.test +++ b/idea/testData/refactoring/rename/renameJavaInterface/renameJavaInterface.test @@ -1,3 +1,6 @@ -// RENAME: JAVA_CLASS->testing.SomeInterface->NewInterfaceName - -// For KT-4100 Imports doesn't change after java interface name refactoring \ No newline at end of file +{ + "type": "JAVA_CLASS", + "classFQN": "testing.SomeInterface", + "newName": "NewInterfaceName", + "comment": "For KT-4100 Imports doesn't change after java interface name refactoring" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameJavaMethod/javaBaseMethod.test b/idea/testData/refactoring/rename/renameJavaMethod/javaBaseMethod.test index 98a1141e76b..c27ff5a510b 100644 --- a/idea/testData/refactoring/rename/renameJavaMethod/javaBaseMethod.test +++ b/idea/testData/refactoring/rename/renameJavaMethod/javaBaseMethod.test @@ -1 +1,6 @@ -// RENAME: JAVA_METHOD->testing.JavaClass->String foo()->bar \ No newline at end of file +{ + "type": "JAVA_METHOD", + "classFQN": "testing.JavaClass", + "methodSignature": "String foo()", + "newName": "bar" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameJavaMethod/kotlinOverridenMethod.test b/idea/testData/refactoring/rename/renameJavaMethod/kotlinOverridenMethod.test index 4bb32bec920..ba46061895a 100644 --- a/idea/testData/refactoring/rename/renameJavaMethod/kotlinOverridenMethod.test +++ b/idea/testData/refactoring/rename/renameJavaMethod/kotlinOverridenMethod.test @@ -1,2 +1,7 @@ -// RENAME: KOTLIN_FUNCTION->testing.kotlin.KotlinClass.foo->bar -// FILE: MethodUsages.kt \ No newline at end of file +{ + "type": "KOTLIN_FUNCTION", + "classFQN": "testing.kotlin.KotlinClass", + "oldName": "foo", + "newName": "bar", + "mainFile": "MethodUsages.kt" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForBaseFunction.test b/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForBaseFunction.test index d9c8385f1b4..1c23ae82abc 100644 --- a/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForBaseFunction.test +++ b/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForBaseFunction.test @@ -1 +1,6 @@ -// RENAME: JAVA_METHOD->testing.rename.A->void first()->second \ No newline at end of file +{ + "type": "JAVA_METHOD", + "classFQN": "testing.rename.A", + "methodSignature": "void first()", + "newName": "second" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForOverridenFunctionWithKotlinBase.test b/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForOverridenFunctionWithKotlinBase.test index 478bf881750..9fe63b7f986 100644 --- a/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForOverridenFunctionWithKotlinBase.test +++ b/idea/testData/refactoring/rename/renameKotlinBaseMethod/javaWrapperForOverridenFunctionWithKotlinBase.test @@ -1 +1,6 @@ -// RENAME: JAVA_METHOD->testing.JavaClient.D->void first()->second \ No newline at end of file +{ + "type": "JAVA_METHOD", + "classFQN": "testing.JavaClient.D", + "methodSignature": "void first()", + "newName": "second" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinBaseMethod/kotlinBaseFunction.test b/idea/testData/refactoring/rename/renameKotlinBaseMethod/kotlinBaseFunction.test index 6b1ffbd52d5..ef591a70813 100644 --- a/idea/testData/refactoring/rename/renameKotlinBaseMethod/kotlinBaseFunction.test +++ b/idea/testData/refactoring/rename/renameKotlinBaseMethod/kotlinBaseFunction.test @@ -1,3 +1,7 @@ -// RENAME: KOTLIN_FUNCTION->testing.rename.A.first->second - -// KT-2836 Rename method with all its implementations \ No newline at end of file +{ + "type": "KOTLIN_FUNCTION", + "classFQN": "testing.rename.A", + "oldName": "first", + "newName": "second", + "comment": "KT-2836 Rename method with all its implementations" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClass/javaWrapperForKotlinClass.test b/idea/testData/refactoring/rename/renameKotlinClass/javaWrapperForKotlinClass.test index ba92cec85a8..dfb8d1de7bd 100644 --- a/idea/testData/refactoring/rename/renameKotlinClass/javaWrapperForKotlinClass.test +++ b/idea/testData/refactoring/rename/renameKotlinClass/javaWrapperForKotlinClass.test @@ -1 +1,5 @@ -// RENAME: JAVA_CLASS->testing.rename.First->Third \ No newline at end of file +{ + "type": "JAVA_CLASS", + "classFQN": "testing.rename.First", + "newName": "Third" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test b/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test index e42e956643c..37fa91172bc 100644 --- a/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test +++ b/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test @@ -1 +1,5 @@ -// RENAME: KOTLIN_CLASS->testing.rename.First->Third \ No newline at end of file +{ + "type": "KOTLIN_CLASS", + "classFQN": "testing.rename.First", + "newName": "Third" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test b/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test index 6d6d8d73f18..cf5b0e470e2 100644 --- a/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test +++ b/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test @@ -1 +1,5 @@ -// RENAME: KOTLIN_CLASS->X->TestX \ No newline at end of file +{ + "type": "KOTLIN_CLASS", + "classFQN": "X", + "newName": "TestX" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClassWithFile/javaClassWrapper.test b/idea/testData/refactoring/rename/renameKotlinClassWithFile/javaClassWrapper.test index 1f9222a2ba8..3cc1e06a8a6 100644 --- a/idea/testData/refactoring/rename/renameKotlinClassWithFile/javaClassWrapper.test +++ b/idea/testData/refactoring/rename/renameKotlinClassWithFile/javaClassWrapper.test @@ -1 +1,5 @@ -// RENAME: JAVA_CLASS->testing.ClassBefore->ClassAfter \ No newline at end of file +{ + "type": "JAVA_CLASS", + "classFQN": "testing.ClassBefore", + "newName": "ClassAfter" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClassWithFile/kotlinClass.test b/idea/testData/refactoring/rename/renameKotlinClassWithFile/kotlinClass.test index 454692dfe6b..c966523e9f9 100644 --- a/idea/testData/refactoring/rename/renameKotlinClassWithFile/kotlinClass.test +++ b/idea/testData/refactoring/rename/renameKotlinClassWithFile/kotlinClass.test @@ -1,2 +1,6 @@ -// RENAME: KOTLIN_CLASS->testing.ClassBefore->ClassAfter -// FILE: ClassBefore.kt \ No newline at end of file +{ + "type": "KOTLIN_CLASS", + "classFQN": "testing.ClassBefore", + "newName": "ClassAfter", + "mainFile": "ClassBefore.kt" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinMethod/javaWrapperForKotlinMethod.test b/idea/testData/refactoring/rename/renameKotlinMethod/javaWrapperForKotlinMethod.test index a164e4d5bbc..59a3f619c44 100644 --- a/idea/testData/refactoring/rename/renameKotlinMethod/javaWrapperForKotlinMethod.test +++ b/idea/testData/refactoring/rename/renameKotlinMethod/javaWrapperForKotlinMethod.test @@ -1,3 +1,7 @@ -// RENAME: JAVA_METHOD->testing.rename.C->int first()->second - -// KT-3256 Can't rename a Kotlin function from Java code \ No newline at end of file +{ + "type": "JAVA_METHOD", + "classFQN": "testing.rename.C", + "methodSignature": "int first()", + "newName": "second", + "comment": "KT-3256 Can't rename a Kotlin function from Java code" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test b/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test index f3d1379d9a0..b0b4c5190ca 100644 --- a/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test +++ b/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test @@ -1 +1,6 @@ -// RENAME: KOTLIN_FUNCTION->testing.rename.C.first->second \ No newline at end of file +{ + "type": "KOTLIN_FUNCTION", + "classFQN": "testing.rename.C", + "oldName": "first", + "newName": "second" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test b/idea/testData/refactoring/rename/renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test index 3f74d37a8ec..3f0b79938c7 100644 --- a/idea/testData/refactoring/rename/renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test +++ b/idea/testData/refactoring/rename/renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test @@ -1,2 +1,6 @@ -// RENAME: JAVA_CLASS->testing.rename.RenamePackage->NewPackageName -// HINT: Can't rename kotlin package class \ No newline at end of file +{ + "type": "JAVA_CLASS", + "hint": "Can\u0027t rename kotlin package class", + "classFQN": "testing.rename.RenamePackage", + "newName": "NewPackageName" +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinPackageFunctionFromJava/renameKotlinPackageFunctionFromJava.test b/idea/testData/refactoring/rename/renameKotlinPackageFunctionFromJava/renameKotlinPackageFunctionFromJava.test index 31c9f71cb03..70feefbf1c7 100644 --- a/idea/testData/refactoring/rename/renameKotlinPackageFunctionFromJava/renameKotlinPackageFunctionFromJava.test +++ b/idea/testData/refactoring/rename/renameKotlinPackageFunctionFromJava/renameKotlinPackageFunctionFromJava.test @@ -1 +1,6 @@ -// RENAME: JAVA_METHOD->testing.rename.RenamePackage->int foo()->bar \ No newline at end of file +{ + "type": "JAVA_METHOD", + "classFQN": "testing.rename.RenamePackage", + "methodSignature": "int foo()", + "newName": "bar" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/PluginTestCaseBase.java b/idea/tests/org/jetbrains/jet/plugin/PluginTestCaseBase.java index f7d5967630f..8b598c58a76 100644 --- a/idea/tests/org/jetbrains/jet/plugin/PluginTestCaseBase.java +++ b/idea/tests/org/jetbrains/jet/plugin/PluginTestCaseBase.java @@ -36,6 +36,7 @@ public class PluginTestCaseBase { private PluginTestCaseBase() { } + @NotNull public static String getTestDataPathBase() { return JetTestCaseBuilder.getHomeDirectory() + TEST_DATA_PROJECT_RELATIVE; } diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.kt b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.kt index d0510a076d7..6a517b448c6 100644 --- a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.kt +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.kt @@ -14,243 +14,196 @@ * limitations under the License. */ -package org.jetbrains.jet.plugin.refactoring.rename; +package org.jetbrains.jet.plugin.refactoring.rename -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.fileEditor.FileDocumentManager; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.VirtualFileManager; -import com.intellij.psi.*; -import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.refactoring.MultiFileTestCase; -import com.intellij.refactoring.rename.RenameProcessor; -import com.intellij.refactoring.rename.RenamePsiElementProcessor; -import com.intellij.refactoring.util.CommonRefactoringUtil; -import com.intellij.util.Function; -import junit.framework.Assert; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.InTextDirectivesUtils; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; -import org.jetbrains.jet.lang.descriptors.VariableDescriptor; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.BindingContextUtils; -import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.scopes.JetScope; -import org.jetbrains.jet.lang.types.TypeProjection; -import org.jetbrains.jet.plugin.PluginTestCaseBase; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -import org.jetbrains.jet.utils.ExceptionUtils; +import com.intellij.refactoring.MultiFileTestCase +import com.intellij.openapi.util.io.FileUtil +import java.io.File +import junit.framework.Assert +import com.google.gson.JsonParser +import com.google.gson.JsonObject +import com.intellij.refactoring.util.CommonRefactoringUtil +import com.intellij.openapi.util.text.StringUtil +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.psi.PsiDocumentManager +import com.intellij.refactoring.rename.RenameProcessor +import com.intellij.refactoring.rename.RenamePsiElementProcessor +import org.jetbrains.jet.lang.psi.JetFile +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.jet.plugin.PluginTestCaseBase +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.JavaPsiFacade +import com.intellij.openapi.project.Project +import com.intellij.openapi.module.Module +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.lang.resolve.BindingContext +import java.util.Collections +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.lang.resolve.BindingContextUtils +import org.jetbrains.jet.lang.descriptors.ClassDescriptor +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor -import java.io.File; -import java.util.Collections; +private enum class RenameType { + JAVA_CLASS + JAVA_METHOD + KOTLIN_CLASS + KOTLIN_FUNCTION + KOTLIN_PROPERTY +} -public abstract class AbstractRenameTest extends MultiFileTestCase { - private enum RenameType { - JAVA_CLASS, - JAVA_METHOD, - KOTLIN_CLASS, - KOTLIN_FUNCTION, - KOTLIN_PROPERTY, +fun JsonObject.getString(name: String): String { + val member = getNullableString(name) + if (member == null) { + throw IllegalStateException("Member with name '$name' is expected in '$this'") } - public void doTest(String path) { + return member +} + +fun JsonObject.getNullableString(name: String): String? = this[name]?.getAsString() + +public abstract class AbstractRenameTest : MultiFileTestCase() { + inner class TestContext( + val project: Project = getProject()!!, + val javaFacade: JavaPsiFacade = getJavaFacade()!!, + val module: Module = getModule()!!) + + public open fun doTest(path : String) { + val fileText = FileUtil.loadFile(File(path)) + + val jsonParser = JsonParser() + val renameObject = jsonParser.parse(fileText) as JsonObject + + val renameTypeStr = renameObject.getString("type") + + val hintDirective = renameObject.getNullableString("hint") + try { - String fileText = FileUtil.loadFile(new File(path)); - String renameDirective = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// RENAME:"); - Assert.assertNotNull("'// RENAME:' directive is expected for rename test file", renameDirective); + val context = TestContext() - String[] strings = renameDirective.split("->"); - Assert.assertTrue("'// RENAME:' directive should have at least AbstractRenameTest.RenameType parameter", strings.length > 0); - - String hintDirective = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// HINT:"); - - String mainFile = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// FILE:"); - - String renameTypeStr = strings[0]; - RenameType type = RenameType.valueOf(renameTypeStr); - - try { - FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]); - - switch (type) { - case JAVA_CLASS: - renameJavaClassTest(fqNameUnsafe.asString(), strings[2]); - break; - case JAVA_METHOD: - renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]); - break; - case KOTLIN_CLASS: - renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2], mainFile); - break; - case KOTLIN_FUNCTION: - renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2], mainFile); - break; - case KOTLIN_PROPERTY: - renameKotlinPropertyTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2], mainFile); - break; - } - - if (hintDirective != null) { - Assert.fail(String.format("Hint \"%s\" was expected", hintDirective)); - } + when (RenameType.valueOf(renameTypeStr)) { + RenameType.JAVA_CLASS -> renameJavaClassTest(renameObject, context) + RenameType.JAVA_METHOD -> renameJavaMethodTest(renameObject, context) + RenameType.KOTLIN_CLASS -> renameKotlinClassTest(renameObject, context) + RenameType.KOTLIN_FUNCTION -> renameKotlinFunctionTest(renameObject, context) + RenameType.KOTLIN_PROPERTY -> renameKotlinPropertyTest(renameObject, context) } - catch (CommonRefactoringUtil.RefactoringErrorHintException hintException) { - String hintExceptionUnquoted = StringUtil.unquoteString(hintException.getMessage()); - if (hintDirective != null) { - Assert.assertEquals(hintDirective, hintExceptionUnquoted); - } - else { - Assert.fail(String.format("Unexpected hint: // HINT: %s", hintExceptionUnquoted)); - } + + if (hintDirective != null) { + Assert.fail("""Hint "$hintDirective" was expected""") } } - catch (Exception e) { - throw ExceptionUtils.rethrow(e); + catch (hintException : CommonRefactoringUtil.RefactoringErrorHintException) { + val hintExceptionUnquoted = StringUtil.unquoteString(hintException.getMessage()!!) + if (hintDirective != null) { + Assert.assertEquals(hintDirective, hintExceptionUnquoted) + } + else { + Assert.fail("""Unexpected "hint: $hintExceptionUnquoted" """) + } } } - private void renameJavaClassTest(@NonNls final String qClassName, @NonNls final String newName) throws Exception { - doTest(new MultiFileTestCase.PerformAction() { - @Override - public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception { - PsiClass aClass = myJavaFacade.findClass(qClassName, GlobalSearchScope.allScope(getProject())); - assertNotNull("Class " + qClassName + " not found", aClass); + private fun renameJavaClassTest(renameParamsObject: JsonObject, context: TestContext) { + val classFQN = renameParamsObject.getString("classFQN") + val newName = renameParamsObject.getString("newName") - PsiElement substitution = RenamePsiElementProcessor.forElement(aClass).substituteElementToRename(aClass, null); + doTest { rootDir, rootAfter -> + val aClass = context.javaFacade.findClass(classFQN, GlobalSearchScope.allScope(context.project))!! + val substitution = RenamePsiElementProcessor.forElement(aClass).substituteElementToRename(aClass, null) - new RenameProcessor(myProject, substitution, newName, true, true).run(); + RenameProcessor(context.project, substitution, newName, true, true).run() - PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - FileDocumentManager.getInstance().saveAllDocuments(); - } - }); + PsiDocumentManager.getInstance(context.project).commitAllDocuments() + FileDocumentManager.getInstance()?.saveAllDocuments() + } } - private void renameJavaMethodTest(final String className, final String methodSignature, final String newName) throws Exception { - doTest(new MultiFileTestCase.PerformAction() { - @Override - public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception { - JavaPsiFacade manager = getJavaFacade(); + private fun renameJavaMethodTest(renameParamsObject: JsonObject, context: TestContext) { + val classFQN = renameParamsObject.getString("classFQN") + val methodSignature = renameParamsObject.getString("methodSignature") + val newName = renameParamsObject.getString("newName") - PsiClass aClass = manager.findClass(className, GlobalSearchScope.moduleScope(myModule)); - assertNotNull("Class " + className + " not found", aClass); + doTest { rootDir, rootAfter -> + val aClass = context.javaFacade.findClass(classFQN, GlobalSearchScope.moduleScope(context.module))!! - PsiMethod methodBySignature = aClass.findMethodBySignature(manager.getElementFactory().createMethodFromText(methodSignature + "{}", null), false); - assertNotNull("Method with signature '" + methodSignature + "' wasn't found in class " + className, methodBySignature); + val methodText = context.javaFacade.getElementFactory().createMethodFromText(methodSignature + "{}", null) + val method = aClass.findMethodBySignature(methodText, false) - PsiElement substitution = RenamePsiElementProcessor.forElement(methodBySignature).substituteElementToRename(methodBySignature, null); - assert substitution != null; + if (method == null) throw IllegalStateException("Method with signature '$methodSignature' wasn't found in class $classFQN") - new RenameProcessor(myProject, substitution, newName, false, false).run(); + val substitution = RenamePsiElementProcessor.forElement(method).substituteElementToRename(method, null) - PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - FileDocumentManager.getInstance().saveAllDocuments(); - } - }); + RenameProcessor(context.project, substitution, newName, false, false).run() + + PsiDocumentManager.getInstance(context.project).commitAllDocuments() + FileDocumentManager.getInstance()?.saveAllDocuments() + } } - private void renameKotlinFunctionTest(final FqName qClassName, final String oldMethodName, String newMethodName, String mainFile) throws Exception { - doTestWithKotlinRename(new Function() { - @Override - public PsiElement fun(PsiFile file) { - BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file) - .getBindingContext(); - ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName); + private fun renameKotlinFunctionTest(renameParamsObject: JsonObject, context: TestContext) { + val oldMethodName = Name.identifier(renameParamsObject.getString("oldName")) - assertNotNull(classDescriptor); - JetScope scope = classDescriptor.getMemberScope(Collections.emptyList()); - FunctionDescriptor methodDescriptor = scope.getFunctions(Name.identifier(oldMethodName)).iterator().next(); - return BindingContextUtils.callableDescriptorToDeclaration(bindingContext, methodDescriptor); - } - }, newMethodName, mainFile); + doRenameInKotlinClass(renameParamsObject, context) { classDescriptor -> + val scope = classDescriptor.getMemberScope(Collections.emptyList()) + scope.getFunctions(oldMethodName).first() + } } - private void renameKotlinPropertyTest(final FqName qClassName, final String oldPropertyName, String newPropertyName, String mainFile) throws Exception { - doTestWithKotlinRename(new Function() { - @Override - public PsiElement fun(PsiFile file) { - BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file).getBindingContext(); - ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName); - assertNotNull(classDescriptor); + private fun renameKotlinPropertyTest(renameParamsObject: JsonObject, context: TestContext) { + val oldPropertyName = Name.identifier(renameParamsObject.getString("oldName")) - JetScope scope = classDescriptor.getMemberScope(Collections.emptyList()); - VariableDescriptor propertyName = scope.getProperties(Name.identifier(oldPropertyName)).iterator().next(); - return BindingContextUtils.descriptorToDeclaration(bindingContext, propertyName); - } - }, newPropertyName, mainFile); + doRenameInKotlinClass(renameParamsObject, context) { classDescriptor -> + val scope = classDescriptor.getMemberScope(Collections.emptyList()) + scope.getProperties(oldPropertyName).first() + } } - private void renameKotlinClassTest(@NonNls final FqName qClassName, @NonNls String newName, String mainFile) throws Exception { - doTestWithKotlinRename(new Function() { - @Override - public PsiElement fun(PsiFile file) { - BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file) - .getBindingContext(); - ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName); - - assertNotNull(classDescriptor); - - return BindingContextUtils.classDescriptorToDeclaration(bindingContext, classDescriptor); - } - }, newName, mainFile); + private fun renameKotlinClassTest(renameParamsObject: JsonObject, context: TestContext) { + doRenameInKotlinClass(renameParamsObject, context) { classDescriptor -> classDescriptor } } - private void doTestWithKotlinRename( - @NonNls final Function elementToRenameCallback, - @NonNls final String newName, - @Nullable final String mainFile - ) throws Exception { - doTest(new MultiFileTestCase.PerformAction() { - @Override - public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception { - VirtualFile child = rootDir.findChild(mainFile == null ? (getTestDirName(false) + ".kt") : mainFile); - assertNotNull(child); + private fun doRenameInKotlinClass(renameParamsObject: JsonObject, context: TestContext, + findDescriptorToRename: (ClassDescriptor) -> DeclarationDescriptor + ) { + val classFqName = FqName(renameParamsObject.getString("classFQN")) + val newName = renameParamsObject.getString("newName") + val mainFilePath = renameParamsObject.getNullableString("mainFile") ?: "${getTestDirName(false)}.kt" - Document document = FileDocumentManager.getInstance().getDocument(child); - assertNotNull(document); + doTest { rootDir, rootAfter -> + val mainFile = rootDir.findChild(mainFilePath)!! + val document = FileDocumentManager.getInstance()!!.getDocument(mainFile)!! + val jetFile = PsiDocumentManager.getInstance(context.project).getPsiFile(document) as JetFile - PsiFile file = PsiDocumentManager.getInstance(getProject()).getPsiFile(document); - assertTrue(file instanceof JetFile); + val bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache(jetFile).getBindingContext() + val classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, classFqName)!! - PsiElement psiElement = elementToRenameCallback.fun(file); - assertNotNull(psiElement); + val psiElement = BindingContextUtils.descriptorToDeclaration(bindingContext, findDescriptorToRename(classDescriptor))!! - PsiElement substitution = RenamePsiElementProcessor.forElement(psiElement).substituteElementToRename(psiElement, null); - assert substitution != null; + val substitution = RenamePsiElementProcessor.forElement(psiElement).substituteElementToRename(psiElement, null) - new RenameProcessor(myProject, substitution, newName, true, true).run(); + RenameProcessor(context.project, substitution, newName, true, true).run() - PsiDocumentManager.getInstance(myProject).commitAllDocuments(); - FileDocumentManager.getInstance().saveAllDocuments(); - VirtualFileManager.getInstance().syncRefresh(); - } - }); + PsiDocumentManager.getInstance(context.project).commitAllDocuments() + FileDocumentManager.getInstance()?.saveAllDocuments() + } } - protected String getTestDirName(boolean lowercaseFirstLetter) { - String testName = getTestName(lowercaseFirstLetter); - return testName.substring(0, testName.indexOf('_')); + protected fun getTestDirName(lowercaseFirstLetter : Boolean) : String { + val testName = getTestName(lowercaseFirstLetter) + return testName.substring(0, testName.indexOf('_')) } - @Override - protected void doTest(PerformAction performAction) throws Exception { - super.doTest(performAction, getTestDirName(true)); + protected fun doTest(action : (VirtualFile, VirtualFile?) -> Unit) { + super.doTest(action, getTestDirName(true)) } - @Override - protected String getTestRoot() { - return "/refactoring/rename/"; + protected override fun getTestRoot() : String { + return "/refactoring/rename/" } - @Override - protected String getTestDataPath() { - return PluginTestCaseBase.getTestDataPathBase(); + protected override fun getTestDataPath() : String { + return PluginTestCaseBase.getTestDataPathBase() } }