update Java references to file class when a file is renamed
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@ public object PackagePartClassUtils {
|
||||
|
||||
private val PART_CLASS_NAME_SUFFIX = "Kt"
|
||||
|
||||
private @JvmStatic fun getPartClassName(str: String): String =
|
||||
public @JvmStatic fun getPartClassName(str: String): String =
|
||||
if (str.isEmpty())
|
||||
"_$PART_CLASS_NAME_SUFFIX"
|
||||
else
|
||||
|
||||
@@ -180,6 +180,8 @@ public class KotlinLightClassForFacade private constructor(
|
||||
|
||||
override fun getName() = facadeClassFqName.shortName().asString()
|
||||
|
||||
override fun setName(name: String): PsiElement? = this
|
||||
|
||||
override fun getQualifiedName() = facadeClassFqName.asString()
|
||||
|
||||
override fun isValid() = files.all { it.isValid() }
|
||||
|
||||
@@ -353,6 +353,9 @@
|
||||
<renamePsiElementProcessor implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinParameterProcessor"
|
||||
id="KotlinParameter"
|
||||
order="first"/>
|
||||
<renamePsiElementProcessor implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinFileProcessor"
|
||||
id="KotlinFile"
|
||||
order="first"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinImplicitLambdaParameter"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameDynamicMemberHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameOnSecondaryConstructorHandler"/>
|
||||
|
||||
@@ -20,7 +20,9 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiReference
|
||||
import com.intellij.refactoring.RefactoringBundle
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import org.jetbrains.kotlin.asJava.KotlinLightClass
|
||||
import org.jetbrains.kotlin.asJava.KotlinLightClassForExplicitDeclaration
|
||||
import org.jetbrains.kotlin.asJava.KotlinLightClassForFacade
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.openapi.fileTypes.FileTypeManager
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.psi.JavaPsiFacade
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.refactoring.rename.RenamePsiFileProcessor
|
||||
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
||||
import org.jetbrains.kotlin.idea.JetFileType
|
||||
import org.jetbrains.kotlin.idea.search.allScope
|
||||
import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
|
||||
class RenameKotlinFileProcessor() : RenamePsiFileProcessor() {
|
||||
override fun canProcessElement(element: PsiElement) = element is JetFile
|
||||
|
||||
override fun prepareRenaming(element: PsiElement?,
|
||||
newName: String,
|
||||
allRenames: MutableMap<PsiElement, String>,
|
||||
scope: SearchScope) {
|
||||
val jetFile = element as? JetFile ?: return
|
||||
if (FileTypeManager.getInstance().getFileTypeByFileName(newName) != JetFileType.INSTANCE) {
|
||||
return
|
||||
}
|
||||
|
||||
val fileInfo = JvmFileClassUtil.getFileClassInfoNoResolve(jetFile)
|
||||
if (!fileInfo.isWithJvmName) {
|
||||
val facadeFqName = fileInfo.facadeClassFqName
|
||||
val project = jetFile.project
|
||||
val facadeClass = JavaPsiFacade.getInstance(project).findClass(facadeFqName.asString(), project.allScope())
|
||||
if (facadeClass != null) {
|
||||
allRenames[facadeClass] = PackagePartClassUtils.getPartClassName(FileUtil.getNameWithoutExtension(newName))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package testing.rename
|
||||
|
||||
var foo: String = "xyzzy"
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.BarKt;
|
||||
|
||||
class JavaClient {
|
||||
public void foo() {
|
||||
String old = BarKt.getFoo();
|
||||
BarKt.setFoo(old + "new");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package testing.rename
|
||||
|
||||
var foo: String = "xyzzy"
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.FooKt;
|
||||
|
||||
class JavaClient {
|
||||
public void foo() {
|
||||
String old = FooKt.getFoo();
|
||||
FooKt.setFoo(old + "new");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "FILE",
|
||||
"file": "Foo.kt",
|
||||
"newName": "Bar.kt"
|
||||
}
|
||||
@@ -65,7 +65,8 @@ private enum class RenameType {
|
||||
KOTLIN_FUNCTION,
|
||||
KOTLIN_PROPERTY,
|
||||
KOTLIN_PACKAGE,
|
||||
MARKED_ELEMENT
|
||||
MARKED_ELEMENT,
|
||||
FILE
|
||||
}
|
||||
|
||||
public abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
|
||||
@@ -100,6 +101,7 @@ public abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
|
||||
RenameType.KOTLIN_PROPERTY -> renameKotlinPropertyTest(renameObject, context)
|
||||
RenameType.KOTLIN_PACKAGE -> renameKotlinPackageTest(renameObject, context)
|
||||
RenameType.MARKED_ELEMENT -> renameMarkedElement(renameObject, context)
|
||||
RenameType.FILE -> renameFile(renameObject, context)
|
||||
}
|
||||
|
||||
if (hintDirective != null) {
|
||||
@@ -230,6 +232,18 @@ public abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun renameFile(renameParamsObject: JsonObject, context: TestContext) {
|
||||
val file = renameParamsObject.getString("file")
|
||||
val newName = renameParamsObject.getString("newName")
|
||||
|
||||
doTestCommittingDocuments { rootDir, rootAfter ->
|
||||
val mainFile = rootDir.findChild(file)!!
|
||||
val psiFile = PsiManager.getInstance(context.project).findFile(mainFile)
|
||||
|
||||
runRenameProcessor(context, newName, psiFile, true, true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun doRenameInKotlinClassOrPackage(
|
||||
renameParamsObject: JsonObject, context: TestContext, findDescriptorToRename: (JetScope) -> DeclarationDescriptor
|
||||
) {
|
||||
|
||||
@@ -269,6 +269,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("renameKotlinFile/renameFile.test")
|
||||
public void testRenameKotlinFile_RenameFile() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinFile/renameFile.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("renameKotlinFunctionInEnum/renameKotlinFunctionInEnum.test")
|
||||
public void testRenameKotlinFunctionInEnum_RenameKotlinFunctionInEnum() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinFunctionInEnum/renameKotlinFunctionInEnum.test");
|
||||
|
||||
Reference in New Issue
Block a user