@@ -19,6 +19,7 @@ import com.intellij.openapi.application.ApplicationManager
1919import com.intellij.openapi.module.Module
2020import com.intellij.openapi.module.ModuleUtilCore
2121import com.intellij.openapi.vfs.VfsUtil
22+ import com.intellij.openapi.vfs.VirtualFile
2223import com.intellij.psi.JavaDirectoryService
2324import com.intellij.psi.PsiClass
2425import com.intellij.psi.PsiDirectory
@@ -28,10 +29,10 @@ import com.intellij.psi.PsiPackage
2829import com.intellij.refactoring.listeners.RefactoringElementListener
2930import com.intellij.refactoring.listeners.RefactoringElementListenerProvider
3031import com.intellij.util.IncorrectOperationException
32+ import org.domaframework.doma.intellij.common.CommonPathParameter
3133import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH
3234import org.domaframework.doma.intellij.common.dao.getDaoClass
3335import org.domaframework.doma.intellij.common.util.PluginLoggerUtil
34- import org.domaframework.doma.intellij.extension.getResourceRoot
3536import org.domaframework.doma.intellij.extension.getResourcesSQLFile
3637import org.jetbrains.kotlin.idea.base.util.module
3738import java.io.IOException
@@ -74,7 +75,9 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider {
7475 psiPackage.directories
7576 .filter { ! it.name.contains(" build" ) }
7677 directories.forEach { dir ->
77- handlePackageMove(psiPackage, oldQualifiedName, module)
78+ newElement.containingFile
79+ ?.virtualFile
80+ ?.let { handlePackageMove(psiPackage, oldQualifiedName, module, it) }
7881 }
7982
8083 PluginLoggerUtil .countLogging(
@@ -95,7 +98,9 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider {
9598 psiPackage.directories
9699 .filter { ! it.name.contains(" build" ) }
97100 directories.forEach { dir ->
98- handlePackageMove(psiPackage, oldQualifiedName, module, moveFileName)
101+ element.containingFile
102+ ?.virtualFile
103+ ?.let { handlePackageMove(psiPackage, oldQualifiedName, module, it, moveFileName) }
99104 }
100105
101106 PluginLoggerUtil .countLogging(
@@ -110,13 +115,15 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider {
110115 packageElement : PsiPackage ,
111116 oldQualifiedName : String ,
112117 module : Module ,
118+ file : VirtualFile ,
113119 moveFileName : String? = null,
114120 ) {
115121 val newQualifiedName = packageElement.qualifiedName
116122 renameOrMovePackage(
117123 module,
118124 oldQualifiedName,
119125 newQualifiedName,
126+ file,
120127 moveFileName,
121128 )
122129 }
@@ -125,29 +132,42 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider {
125132 module : Module ,
126133 oldQualifiedName : String ,
127134 newQualifiedName : String ,
135+ file : VirtualFile ,
128136 moveFileName : String? = null,
129137 ) {
130- val baseDir = " ${module.getResourceRoot()?.path} /$RESOURCES_META_INF_PATH /"
131- val newPath = " $baseDir /${newQualifiedName.replace(" ." , " /" )} "
138+ val pathParameter = CommonPathParameter (module)
139+ val resources = pathParameter.getResources(file)
140+ val isTest = pathParameter.isTest(file)
141+ val baseDirs: List <String > = resources.map { resource -> " ${resource.path} /$RESOURCES_META_INF_PATH /" }
142+ val newPaths = baseDirs.map { baseDir -> " $baseDir /${newQualifiedName.replace(" ." , " /" )} " }
132143
133144 ApplicationManager .getApplication().runWriteAction {
134- try {
135- val newDir = VfsUtil .createDirectories(newPath)
136- val oldResourcePath = module.getResourcesSQLFile(oldQualifiedName.replace(" ." , " /" ), false )
137- oldResourcePath?.children?.forEach { old ->
138- if (moveFileName != null && old.name == moveFileName) {
139- old?.move(this , newDir)
140- } else if (moveFileName == null ) {
141- old?.move(this , newDir)
142- }
143- }
144- } catch (e: IOException ) {
145- when (e) {
146- is FileSystemException -> {
147- e.printStackTrace()
145+ newPaths.forEach { newPath ->
146+ try {
147+ val newDir = VfsUtil .createDirectories(newPath)
148+ val oldResourcePath =
149+ module.getResourcesSQLFile(
150+ RESOURCES_META_INF_PATH + " /" + oldQualifiedName.replace(" ." , " /" ),
151+ isTest,
152+ )
153+ if (oldResourcePath != null ) {
154+ oldResourcePath.children?.forEach { old ->
155+ if (moveFileName != null && old.name == moveFileName) {
156+ old?.move(this , newDir)
157+ } else if (moveFileName == null ) {
158+ old?.move(this , newDir)
159+ }
160+ }
161+ return @forEach
148162 }
163+ } catch (e: IOException ) {
164+ when (e) {
165+ is FileSystemException -> {
166+ e.printStackTrace()
167+ }
149168
150- else -> throw IncorrectOperationException (e)
169+ else -> throw IncorrectOperationException (e)
170+ }
151171 }
152172 }
153173 }
0 commit comments