From b32ac9dfaf89ebe1901aafb2a5f82c7d5914c33a Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 20 May 2025 14:18:48 +0900 Subject: [PATCH 1/5] Dynamically retrieve source and resource directory paths. --- .../action/sql/JumpToDaoFromSQLAction.kt | 4 +- .../doma/intellij/common/FileTypeCheck.kt | 37 +----- .../doma/intellij/common/JarFileSearch.kt | 1 - .../doma/intellij/common/dao/DaoMethodUtil.kt | 114 +++++++++++++----- .../common/dao/JumpActionFunctions.kt | 1 + .../doma/intellij/common/psi/PsiDaoMethod.kt | 23 ++-- .../intellij/extension/ModuleExtensions.kt | 7 +- .../gutter/sql/SqlLineMakerProvider.kt | 13 +- .../dao/DaoPackageRenameListenerProcessor.kt | 2 +- .../setting/state/DomaToolsSettings.kt | 101 +++++++++++++++- 10 files changed, 212 insertions(+), 91 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt index 4e3306f2..0db2ea00 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt @@ -56,8 +56,8 @@ class JumpToDaoFromSQLAction : AnAction() { val project = e.project ?: return val daoFile = findDaoFile(project, file) ?: return - val nameWithoutExtension = file.virtualFile?.nameWithoutExtension ?: return - jumpToDaoMethod(project, nameWithoutExtension, daoFile) + val sqlFileName = file.virtualFile?.nameWithoutExtension ?: return + jumpToDaoMethod(project, sqlFileName, daoFile) PluginLoggerUtil.countLoggingByAction( this::class.java.simpleName, "JumpToDao", diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt index 0c561430..6e25d989 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt @@ -16,9 +16,9 @@ package org.domaframework.doma.intellij.common import com.intellij.openapi.fileTypes.FileTypeManager -import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiFile -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.SRC_MAIN_PATH + +val sourceExtensionNames: List = listOf("JAVA", "Kotlin", "CLASS") /** * Get extension by file type identifier @@ -66,36 +66,3 @@ fun isInjectionSqlFile(file: PsiFile): Boolean { } && !(filePath.endsWith(".sql") || filePath.endsWith(".script")) } - -/** - * Dao file search for SQL files - */ -fun searchDaoFile( - contentRoot: VirtualFile?, - originFilePath: String, - relativeDaoFilePath: String, -): VirtualFile? { - val projectRootPath = contentRoot?.path ?: return null - if (projectRootPath.endsWith(SRC_MAIN_PATH)) { - return contentRoot.findFileByRelativePath(relativeDaoFilePath) - } - - if (projectRootPath.length > originFilePath.length) { - return null - } - - // TODO Dynamically build the source directory path and retrieve subproject info - // by inspecting file metadata instead of using string manipulation. - val index = originFilePath.indexOf(SRC_MAIN_PATH) - val projectRootPathBefore = projectRootPath.substringBefore(SRC_MAIN_PATH) - if (index < 0 || projectRootPathBefore.length < index) return null - val subProjectName = - originFilePath.substring(projectRootPathBefore.length, index) - - val daoFile = - contentRoot - .findFileByRelativePath(subProjectName) - ?.findFileByRelativePath(relativeDaoFilePath) - - return daoFile -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/JarFileSearch.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/JarFileSearch.kt index 98a47a72..2c85be29 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/JarFileSearch.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/JarFileSearch.kt @@ -18,7 +18,6 @@ package org.domaframework.doma.intellij.common import com.intellij.openapi.vfs.StandardFileSystems import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiFile -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_META_INF_PATH fun getJarRoot( virtualFile: VirtualFile, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt index b8294389..be14ade6 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt @@ -27,18 +27,19 @@ import com.intellij.psi.PsiManager import com.intellij.psi.PsiMethod import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.util.PsiTreeUtil -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_META_INF_PATH -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_PATH +import org.domaframework.doma.intellij.common.CommonPathParameter +import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH import org.domaframework.doma.intellij.common.getExtension import org.domaframework.doma.intellij.common.getJarRoot import org.domaframework.doma.intellij.common.getMethodDaoFilePath import org.domaframework.doma.intellij.common.isInjectionSqlFile import org.domaframework.doma.intellij.common.isSupportFileType -import org.domaframework.doma.intellij.common.searchDaoFile +import org.domaframework.doma.intellij.common.sourceExtensionNames import org.domaframework.doma.intellij.extension.findFile import org.domaframework.doma.intellij.extension.getContentRoot import org.domaframework.doma.intellij.extension.getJavaClazz import org.domaframework.doma.intellij.extension.getModule +import org.jetbrains.kotlin.idea.base.util.module /** * Get Dao method corresponding to SQL file @@ -57,15 +58,13 @@ fun findDaoMethod( } } else if (isSupportFileType(originalFile)) { // TODO: Add Support Kotlin - val fileTypeName = "JAVA" val methodName = virtualFile.nameWithoutExtension val daoFile = daoFile ?: findDaoFile(project, originalFile) ?: return null if (module != null) { val relativePath = - formatDaoPathFromSqlFilePath( + getDaoPathFromSqlFilePath( originalFile, project.getContentRoot(virtualFile)?.path ?: "", - fileTypeName, ) // get ClassPath with package name val daoClassName: String = @@ -73,12 +72,13 @@ fun findDaoMethod( .substringBefore(".") .replace("/", ".") .replace("\\", ".") - .substringAfter(".${getExtension(fileTypeName)}") .replace("..", ".") .trim('.') + val daoJavaFile = project.findFile(daoFile) findDaoClass(module, daoClassName)?.let { daoClass -> val daoMethod = + // TODO Support Kotlin Project when (daoJavaFile) { is PsiJavaFile -> findUseSqlDaoMethod(daoJavaFile, methodName) else -> null @@ -130,10 +130,36 @@ fun findDaoFile( if (contentRoot == null) { return getJarRoot(virtualFile, sqlFile) } + return searchDaoFile(sqlFile.module, contentRoot, sqlFile) +} + +/** + * Dao file search for SQL file + */ +private fun searchDaoFile( + module: Module?, + contentRoot: VirtualFile?, + sqlFile: PsiFile, +): VirtualFile? { + val contentRootPath = contentRoot?.path ?: return null + val pathParams = CommonPathParameter(module) + val moduleBaseName = pathParams.moduleBasePath?.nameWithoutExtension ?: "" // TODO: Add Support Kotlin - val relativeFilePath = - formatDaoPathFromSqlFilePath(sqlFile, contentRoot.path, "JAVA") - return searchDaoFile(contentRoot, virtualFile.path, relativeFilePath) + val relativeDaoFilePaths = + getDaoPathFromSqlFilePath(sqlFile, contentRoot.path) + val sources = pathParams.getSources(sqlFile.virtualFile) + + if (contentRootPath.endsWith(moduleBaseName) == true) { + sources.forEach { source -> + sourceExtensionNames.forEach { extension -> + val fileExtension = getExtension(extension) + val findDaoFile = + contentRoot.findFileByRelativePath("${source.nameWithoutExtension}$relativeDaoFilePaths.$fileExtension") + if (findDaoFile != null) return findDaoFile + } + } + } + return null } private fun findDaoClass( @@ -143,40 +169,70 @@ private fun findDaoClass( /** * Generate Dao deployment path from SQL file path + * @param sqlFile SQL File + * @param projectRootPath project content Root Path + * @return */ -fun formatDaoPathFromSqlFilePath( - relativeBaseSqlFile: PsiFile, +private fun getDaoPathFromSqlFilePath( + sqlFile: PsiFile, projectRootPath: String, - extension: String, ): String { - if (isInjectionSqlFile(relativeBaseSqlFile)) { + if (isInjectionSqlFile(sqlFile)) { return "" } - val sqlPath = relativeBaseSqlFile.virtualFile?.path ?: return "" + val module = sqlFile.module + val sqlPath = sqlFile.virtualFile?.path ?: return "" var relativeFilePath = sqlPath.substring(projectRootPath.length) if (!relativeFilePath.startsWith("/")) { relativeFilePath = "/$relativeFilePath" } - val extensionType = getExtension(extension.uppercase()) - return relativeFilePath - .replace("/$RESOURCES_PATH", "") - .replace(RESOURCES_META_INF_PATH, extension.lowercase()) - .replace("/${relativeBaseSqlFile.name}", "") - .plus(".$extensionType") + val pathParams = CommonPathParameter(module) + val resources = pathParams.getResources(sqlFile.virtualFile) + + return resources + .firstOrNull { resource -> + relativeFilePath.startsWith("/" + resource.nameWithoutExtension) + }?.let { resource -> + relativeFilePath + .replace("${resource.nameWithoutExtension}/$RESOURCES_META_INF_PATH/", "") + .replace("/${sqlFile.name}", "") + } ?: "" } /** * Generate SqlFile path from Dao file path + * @param daoFile Dao File + * @param module The module to which the Dao file belongs + * @return SqlFile path ex) META-INF/package/dao/DaoClassName/ */ -fun formatSqlPathFromDaoPath( - contentRootPath: String, +fun getRelativeSqlFilePathFromDaoFilePath( daoFile: VirtualFile, + module: Module?, ): String { - val fileType = daoFile.fileType.name + if (module == null) return "" val extension = daoFile.fileType.defaultExtension - val daoFilePath = daoFile.path - return daoFilePath - .replace(contentRootPath, RESOURCES_META_INF_PATH) - .replace("/${fileType.lowercase()}/", "/") - .replace(".$extension", "") + val pathParams = CommonPathParameter(module) + var relativeSqlFilePath = + daoFile.path + .replace(pathParams.moduleBasePath?.path ?: "", "") + .replace(".$extension", "") + val isTest = pathParams.moduleTestSourceDirectories.firstOrNull { dir -> daoFile.path.contains(dir.path) } != null + if (isTest) { + pathParams.moduleTestSourceDirectories.forEach { source -> + relativeSqlFilePath = + relativeSqlFilePath.replace( + "/" + source.nameWithoutExtension, + RESOURCES_META_INF_PATH, + ) + } + } else { + pathParams.moduleSourceDirectories.forEach { source -> + relativeSqlFilePath = + relativeSqlFilePath.replace( + "/" + source.nameWithoutExtension, + RESOURCES_META_INF_PATH, + ) + } + } + return relativeSqlFilePath } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt index 1e41df81..a31154eb 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt @@ -45,6 +45,7 @@ fun jumpToDaoMethod( } } +// TODO Support Kotlin Project private fun getKotlinFunctions( file: KtFile, targetMethodName: String, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt index 9b47697e..b1a5c6e7 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt @@ -32,8 +32,9 @@ import com.intellij.psi.PsiManager import com.intellij.psi.PsiMethod import com.intellij.psi.PsiNameValuePair import com.intellij.util.IncorrectOperationException -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_PATH -import org.domaframework.doma.intellij.common.dao.formatSqlPathFromDaoPath +import org.domaframework.doma.intellij.common.CommonPathParameter +import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH +import org.domaframework.doma.intellij.common.dao.getRelativeSqlFilePathFromDaoFilePath import org.domaframework.doma.intellij.common.getExtension import org.domaframework.doma.intellij.extension.findFile import org.domaframework.doma.intellij.extension.getContentRoot @@ -41,6 +42,7 @@ import org.domaframework.doma.intellij.extension.getModule import org.domaframework.doma.intellij.extension.getResourcesSQLFile import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType import org.domaframework.doma.intellij.setting.SqlLanguage +import org.jetbrains.kotlin.idea.base.util.module import java.io.File import java.io.IOException @@ -51,7 +53,7 @@ class PsiDaoMethod( private val psiProject: Project, val psiMethod: PsiMethod, ) { - private val isTest = false + private var isTest = false var sqlFile: VirtualFile? = null private var sqlFilePath: String = "" @@ -65,9 +67,15 @@ class PsiDaoMethod( setDaoAnnotationType() setSqlFileOption() setSqlFilePath() + setTest() setSqlFile() } + private fun setTest() { + val pathParameter = CommonPathParameter(psiMethod.module) + isTest = pathParameter.isTest(daoFile) + } + private fun setSqlFileOption() { val useSqlFileOptionAnnotation = daoType.getPsiAnnotation(psiMethod) ?: return val isSqlFile = daoType.getSqlFileVal(useSqlFileOptionAnnotation) @@ -115,12 +123,11 @@ class PsiDaoMethod( psiMethod.containingFile.originalFile.virtualFile.path .substringAfter(".jar!") sqlFilePath = - "META-INF${daoRelativePath.replace(".$fileType","")}/$methodName.$sqlExtension" + "$RESOURCES_META_INF_PATH${daoRelativePath.replace(".$fileType","")}/$methodName.$sqlExtension" } else { sqlFilePath = contentRoot.let { - formatSqlPathFromDaoPath(it, daoFile) - .replace("main/", "") + getRelativeSqlFilePathFromDaoFilePath(daoFile, psiMethod.module) .plus("/$methodName.$sqlExtension") } } @@ -175,7 +182,9 @@ class PsiDaoMethod( val rootDir = psiProject.getContentRoot(daoFile) ?: return@runReadAction val sqlFile = File(sqlFilePath) val sqlFileName = sqlFile.name - val parentDir = "${RESOURCES_PATH}/${sqlFile.parent?.replace("\\", "/")}" + val pathParams = CommonPathParameter(psiMethod.module) + val resourceDir = pathParams.getResources(daoFile).firstOrNull() ?: return@runReadAction + val parentDir = "${resourceDir.nameWithoutExtension}/${sqlFile.parent?.replace("\\", "/")}" val parenDirPathSpirit = parentDir.split("/").toTypedArray() WriteCommandAction.runWriteCommandAction(psiProject) { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt b/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt index f724fced..d3369fe3 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt @@ -21,8 +21,7 @@ import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiClass import com.intellij.psi.search.GlobalSearchScope -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_META_INF_PATH -import org.domaframework.doma.intellij.common.dao.formatSqlPathFromDaoPath +import org.domaframework.doma.intellij.common.dao.getRelativeSqlFilePathFromDaoFilePath import org.jetbrains.kotlin.idea.util.sourceRoots /** @@ -32,7 +31,7 @@ fun Module.getPackagePathFromDaoPath(daoFile: VirtualFile): VirtualFile? { val contentRoot = this.project.getContentRoot(daoFile)?.path val packagePath = contentRoot?.let { - formatSqlPathFromDaoPath(it, daoFile) + getRelativeSqlFilePathFromDaoFilePath(daoFile, this) } ?: "" return this.getResourcesSQLFile( @@ -59,7 +58,7 @@ fun Module.getResourcesSQLFile( includeTest: Boolean, ): VirtualFile? = ResourceFileUtil.findResourceFileInScope( - "$RESOURCES_META_INF_PATH/${relativePath.replace(RESOURCES_META_INF_PATH,"")}".replace("//", "/"), + relativePath.replace("//", "/"), this.project, this.getModuleScope(includeTest), ) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/gutter/sql/SqlLineMakerProvider.kt b/src/main/kotlin/org/domaframework/doma/intellij/gutter/sql/SqlLineMakerProvider.kt index 00f4ed85..5541822a 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/gutter/sql/SqlLineMakerProvider.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/gutter/sql/SqlLineMakerProvider.kt @@ -30,6 +30,7 @@ import org.domaframework.doma.intellij.common.dao.findDaoMethod import org.domaframework.doma.intellij.common.dao.jumpToDaoMethod import org.domaframework.doma.intellij.common.isInjectionSqlFile import org.domaframework.doma.intellij.common.isSupportFileType +import org.domaframework.doma.intellij.common.psi.PsiDaoMethod import org.domaframework.doma.intellij.common.util.PluginLoggerUtil import org.jetbrains.kotlin.idea.core.util.toPsiFile import java.awt.event.MouseEvent @@ -54,12 +55,12 @@ class SqlLineMakerProvider : RelatedItemLineMarkerProvider() { } val identifier = e.firstChild ?: e - val daoFile = - findDaoFile(project, file)?.let { - if (findDaoMethod(e.containingFile, it) == null) return - it - } ?: return - + val daoFile = findDaoFile(project, file) ?: return + val daoMethod = findDaoMethod(e.containingFile, daoFile) ?: return + val psiDaoMethod = PsiDaoMethod(file.project, daoMethod) + if (!psiDaoMethod.isUseSqlFileMethod()) { + return + } val marker = RelatedItemLineMarkerInfo( identifier, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt b/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt index 54ddfc97..1769e9ce 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt @@ -28,7 +28,7 @@ import com.intellij.psi.PsiPackage import com.intellij.refactoring.listeners.RefactoringElementListener import com.intellij.refactoring.listeners.RefactoringElementListenerProvider import com.intellij.util.IncorrectOperationException -import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_META_INF_PATH +import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH import org.domaframework.doma.intellij.common.dao.getDaoClass import org.domaframework.doma.intellij.common.util.PluginLoggerUtil import org.domaframework.doma.intellij.extension.getResourceRoot diff --git a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt index 5ec9891d..3f93380f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt @@ -13,14 +13,103 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.setting.state +package org.domaframework.doma.intellij.common -import org.domaframework.doma.intellij.setting.SettingComponent +import com.intellij.openapi.module.Module +import com.intellij.openapi.roots.ModuleRootManager +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.jps.model.java.JavaResourceRootType +import org.jetbrains.jps.model.java.JavaSourceRootType -interface DomaToolsSettings { - fun isModified(component: SettingComponent?): Boolean +val RESOURCES_META_INF_PATH: String + get() = "META-INF" - fun apply(component: SettingComponent?) +class CommonPathParameter( + module: Module?, +) { + /** + * module base path ex)Absolute path of "/src/main" + */ + var moduleBasePath: VirtualFile? = null - fun reset(component: SettingComponent?) + /** + * module source directories ex) Absolute path of "/src/main/java","/src/main/kotlin" + */ + var moduleSourceDirectories: MutableList = mutableListOf() + + /** + * module resource directory ex) Absolute path of "/src/main/resources" + */ + var moduleResourceDirectories: MutableList = mutableListOf() + + var moduleTestSourceDirectories: MutableList = mutableListOf() + var moduleTestResourceDirectories: MutableList = mutableListOf() + + init { + setModuleSourcesFiles(module) + } + + private fun setModuleSourcesFiles(module: Module?) { + if (module == null) return + + val modulemanager = ModuleRootManager.getInstance(module) + + moduleSourceDirectories.clear() + modulemanager?.contentEntries?.firstOrNull()?.let { entry -> + moduleBasePath = entry.file + entry.sourceFolders.map { folder -> + val file = folder.file + if (file != null) { + println("file:${file.name}") + when (folder.rootType) { + JavaSourceRootType.SOURCE -> { + moduleSourceDirectories.add(file) + println("moduleSourceDirectory:$file") + } + + JavaSourceRootType.TEST_SOURCE -> { + moduleTestSourceDirectories.add(file) + println("moduleTestSourceDirectory:$file") + } + + JavaResourceRootType.RESOURCE -> { + moduleResourceDirectories.add(file) + println("moduleResourceDirectory:$file") + } + + JavaResourceRootType.TEST_RESOURCE -> { + moduleTestResourceDirectories.add(file) + println("moduleTestResourceDirectory:$file") + } + } + } + } + } + } + + fun isTest(file: VirtualFile): Boolean { + val testSource = + moduleTestSourceDirectories.firstOrNull { testSource -> + file.path.contains(testSource.path) + } + if (testSource != null) return true + + return moduleTestResourceDirectories.firstOrNull { testSource -> + file.path.contains(testSource.path) + } != null + } + + fun getResources(file: VirtualFile): MutableList = + if (isTest(file)) { + moduleTestResourceDirectories + } else { + moduleResourceDirectories + } + + fun getSources(file: VirtualFile): MutableList = + if (isTest(file)) { + moduleTestSourceDirectories + } else { + moduleSourceDirectories + } } From 958506317c917ee0bd06b84e5a1c23f00da24cde Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 20 May 2025 14:19:59 +0900 Subject: [PATCH 2/5] Remove unused methods and improve code readability in SQL-related classes --- .../intellij/psi/SqlElCommentExprImpl.java | 5 --- .../action/sql/BindVariableElement.kt | 34 ------------------- .../intellij/common/psi/PsiParentClass.kt | 18 ---------- .../intellij/common/sql/PsiClassTypeUtil.kt | 12 ------- .../doma/intellij/formatter/SqlBlockUtil.kt | 2 +- .../formatter/block/SqlLineCommentBlock.kt | 3 -- .../subgroup/SqlUpdateValueGroupBlock.kt | 5 --- .../setting/state/DomaToolsSettings.kt | 5 --- 8 files changed, 1 insertion(+), 83 deletions(-) delete mode 100644 src/main/kotlin/org/domaframework/doma/intellij/action/sql/BindVariableElement.kt diff --git a/src/main/java/org/domaframework/doma/intellij/psi/SqlElCommentExprImpl.java b/src/main/java/org/domaframework/doma/intellij/psi/SqlElCommentExprImpl.java index 738f7350..529a7159 100644 --- a/src/main/java/org/domaframework/doma/intellij/psi/SqlElCommentExprImpl.java +++ b/src/main/java/org/domaframework/doma/intellij/psi/SqlElCommentExprImpl.java @@ -41,11 +41,6 @@ public SqlElCommentExprImpl(@NotNull ASTNode node) { super(node); } - @Override - public @NotNull ASTNode getNode() { - return super.getNode(); - } - @Override public @NotNull IElementType getTokenType() { return getNode().getElementType(); diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/BindVariableElement.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/BindVariableElement.kt deleted file mode 100644 index 9c682c5e..00000000 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/BindVariableElement.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Doma Tools Authors - * - * 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 - * - * https://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.domaframework.doma.intellij.action.sql - -import com.intellij.psi.PsiElement -import com.intellij.util.PsiNavigateUtil - -/** - * Jump from bind variable - */ -class BindVariableElement( - private val bindProperty: PsiElement, -) { - fun jumpToEntity() { - PsiNavigateUtil.navigate(bindProperty) - } - - fun jumpToDao() { - PsiNavigateUtil.navigate(bindProperty) - } -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiParentClass.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiParentClass.kt index d4820b62..34e7280d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiParentClass.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiParentClass.kt @@ -67,15 +67,6 @@ class PsiParentClass( m.hasModifierProperty(PsiModifier.PUBLIC) } - fun findStaticField(fieldName: String): PsiField? = - fields - ?.filter { f -> - f.hasModifierProperty(PsiModifier.STATIC) && - PropertyModifyUtil.filterPrivateField(f, type) - }?.firstOrNull { f -> - f.name == fieldName - } - fun searchStaticField(fieldName: String): List? = fields ?.filter { f -> @@ -84,15 +75,6 @@ class PsiParentClass( PropertyModifyUtil.filterPrivateField(f, type) } - fun findStaticMethod(methodName: String): PsiMethod? = - methods - ?.filter { m -> - m.hasModifierProperty(PsiModifier.STATIC) && - m.hasModifierProperty(PsiModifier.PUBLIC) - }?.firstOrNull { m -> - m.name == methodName - } - fun searchStaticMethod(methodName: String): List? = methods?.filter { m -> m.hasModifierProperty(PsiModifier.STATIC) && diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/sql/PsiClassTypeUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/sql/PsiClassTypeUtil.kt index cb2da97e..5901549d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/sql/PsiClassTypeUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/sql/PsiClassTypeUtil.kt @@ -22,18 +22,6 @@ import com.intellij.psi.search.GlobalSearchScope class PsiClassTypeUtil { companion object { - fun getPsiTypeByList( - classType: PsiClassType, - project: Project, - useListParam: Boolean, - ): PsiType? { - val isIterableType = isIterableType(classType, project) - if (isIterableType && useListParam) { - return classType.parameters.firstOrNull() - } - return classType - } - fun isIterableType( type: PsiClassType, project: Project, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt index 9824ec6f..da03d602 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlBlockUtil.kt @@ -51,7 +51,7 @@ import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr import org.domaframework.doma.intellij.psi.SqlTypes class SqlBlockUtil( - private val sqlBlock: SqlBlock, + sqlBlock: SqlBlock, ) { val wrap = sqlBlock.wrap val alignment = sqlBlock.alignment diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt index 9dac5b0a..e2a03014 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt @@ -16,7 +16,6 @@ package org.domaframework.doma.intellij.formatter.block import com.intellij.formatting.Alignment -import com.intellij.formatting.Indent import com.intellij.formatting.SpacingBuilder import com.intellij.formatting.Wrap import com.intellij.lang.ASTNode @@ -41,8 +40,6 @@ open class SqlLineCommentBlock( override fun buildChildren(): MutableList = mutableListOf() - override fun getIndent(): Indent? = super.getIndent() - override fun isLeaf(): Boolean = true override fun createBlockIndentLen(): Int { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt index 8a462545..90fddf66 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt @@ -23,7 +23,6 @@ import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlUpdateKeywordGroupBlock -import org.domaframework.doma.intellij.psi.SqlTypes /** * In an UPDATE statement using the row value constructor, @@ -53,10 +52,6 @@ class SqlUpdateValueGroupBlock( override fun createBlockIndentLen(): Int { parentBlock?.let { parent -> if (parent is SqlUpdateKeywordGroupBlock) { - val keywords = - parent.childBlocks - .dropLast(1) - .takeWhile { parent.node.elementType == SqlTypes.KEYWORD } return parent.indent.indentLen .plus(parent.getNodeText().length) .plus(3) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt index 3f93380f..49216d30 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt @@ -60,26 +60,21 @@ class CommonPathParameter( entry.sourceFolders.map { folder -> val file = folder.file if (file != null) { - println("file:${file.name}") when (folder.rootType) { JavaSourceRootType.SOURCE -> { moduleSourceDirectories.add(file) - println("moduleSourceDirectory:$file") } JavaSourceRootType.TEST_SOURCE -> { moduleTestSourceDirectories.add(file) - println("moduleTestSourceDirectory:$file") } JavaResourceRootType.RESOURCE -> { moduleResourceDirectories.add(file) - println("moduleResourceDirectory:$file") } JavaResourceRootType.TEST_RESOURCE -> { moduleTestResourceDirectories.add(file) - println("moduleTestResourceDirectory:$file") } } } From 92c20c4ed768f1eeb14e02b29838f18ab29645ce Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 20 May 2025 19:15:01 +0900 Subject: [PATCH 3/5] Refactor resource path handling and improve SQL file retrieval logic --- .../intellij/extension/ModuleExtensions.kt | 9 ++- .../dao/DaoPackageRenameListenerProcessor.kt | 60 ++++++++++++------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt b/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt index d3369fe3..adb3d034 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt @@ -22,7 +22,6 @@ import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiClass import com.intellij.psi.search.GlobalSearchScope import org.domaframework.doma.intellij.common.dao.getRelativeSqlFilePathFromDaoFilePath -import org.jetbrains.kotlin.idea.util.sourceRoots /** * Get SQL directory corresponding to Dao file @@ -40,8 +39,6 @@ fun Module.getPackagePathFromDaoPath(daoFile: VirtualFile): VirtualFile? { ) } -fun Module.getResourceRoot(): VirtualFile? = this.sourceRoots.firstOrNull { it.path.contains("/resources") } - fun Module.getJavaClazz( includeTest: Boolean, fqdn: String, @@ -53,6 +50,12 @@ fun Module.getJavaClazz( .firstOrNull() } +/*** + * Get SQL file corresponding to Dao file + * @param relativePath SQL file relativePath path ex) META-INF/packageName/dao/DaoClassName/sqlFileName.sql + * @param includeTest true: test source, false: main source + * @return SQL file + */ fun Module.getResourcesSQLFile( relativePath: String, includeTest: Boolean, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt b/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt index 1769e9ce..25106001 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt @@ -19,6 +19,7 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.module.Module import com.intellij.openapi.module.ModuleUtilCore import com.intellij.openapi.vfs.VfsUtil +import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.JavaDirectoryService import com.intellij.psi.PsiClass import com.intellij.psi.PsiDirectory @@ -28,10 +29,10 @@ import com.intellij.psi.PsiPackage import com.intellij.refactoring.listeners.RefactoringElementListener import com.intellij.refactoring.listeners.RefactoringElementListenerProvider import com.intellij.util.IncorrectOperationException +import org.domaframework.doma.intellij.common.CommonPathParameter import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH import org.domaframework.doma.intellij.common.dao.getDaoClass import org.domaframework.doma.intellij.common.util.PluginLoggerUtil -import org.domaframework.doma.intellij.extension.getResourceRoot import org.domaframework.doma.intellij.extension.getResourcesSQLFile import org.jetbrains.kotlin.idea.base.util.module import java.io.IOException @@ -74,7 +75,9 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider { psiPackage.directories .filter { !it.name.contains("build") } directories.forEach { dir -> - handlePackageMove(psiPackage, oldQualifiedName, module) + newElement.containingFile + ?.virtualFile + ?.let { handlePackageMove(psiPackage, oldQualifiedName, module, it) } } PluginLoggerUtil.countLogging( @@ -95,7 +98,9 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider { psiPackage.directories .filter { !it.name.contains("build") } directories.forEach { dir -> - handlePackageMove(psiPackage, oldQualifiedName, module, moveFileName) + element.containingFile + ?.virtualFile + ?.let { handlePackageMove(psiPackage, oldQualifiedName, module, it, moveFileName) } } PluginLoggerUtil.countLogging( @@ -110,6 +115,7 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider { packageElement: PsiPackage, oldQualifiedName: String, module: Module, + file: VirtualFile, moveFileName: String? = null, ) { val newQualifiedName = packageElement.qualifiedName @@ -117,6 +123,7 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider { module, oldQualifiedName, newQualifiedName, + file, moveFileName, ) } @@ -125,29 +132,42 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider { module: Module, oldQualifiedName: String, newQualifiedName: String, + file: VirtualFile, moveFileName: String? = null, ) { - val baseDir = "${module.getResourceRoot()?.path}/$RESOURCES_META_INF_PATH/" - val newPath = "$baseDir/${newQualifiedName.replace(".", "/")}" + val pathParameter = CommonPathParameter(module) + val resources = pathParameter.getResources(file) + val isTest = pathParameter.isTest(file) + val baseDirs: List = resources.map { resource -> "${resource.path}/$RESOURCES_META_INF_PATH/" } + val newPaths = baseDirs.map { baseDir -> "$baseDir/${newQualifiedName.replace(".", "/")}" } ApplicationManager.getApplication().runWriteAction { - try { - val newDir = VfsUtil.createDirectories(newPath) - val oldResourcePath = module.getResourcesSQLFile(oldQualifiedName.replace(".", "/"), false) - oldResourcePath?.children?.forEach { old -> - if (moveFileName != null && old.name == moveFileName) { - old?.move(this, newDir) - } else if (moveFileName == null) { - old?.move(this, newDir) - } - } - } catch (e: IOException) { - when (e) { - is FileSystemException -> { - e.printStackTrace() + newPaths.forEach { newPath -> + try { + val newDir = VfsUtil.createDirectories(newPath) + val oldResourcePath = + module.getResourcesSQLFile( + RESOURCES_META_INF_PATH + "/" + oldQualifiedName.replace(".", "/"), + isTest, + ) + if (oldResourcePath != null) { + oldResourcePath.children?.forEach { old -> + if (moveFileName != null && old.name == moveFileName) { + old?.move(this, newDir) + } else if (moveFileName == null) { + old?.move(this, newDir) + } + } + return@forEach } + } catch (e: IOException) { + when (e) { + is FileSystemException -> { + e.printStackTrace() + } - else -> throw IncorrectOperationException(e) + else -> throw IncorrectOperationException(e) + } } } } From 2edd8fed29d459908b02df03eb21808657e4f63b Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 20 May 2025 19:15:13 +0900 Subject: [PATCH 4/5] Refactor directory structure handling in DomaSqlTest and update resource file paths --- .../doma/intellij/common/dao/DaoMethodUtil.kt | 23 ++++++------ .../doma/intellij/DomaSqlTest.kt | 36 +++++++++++++------ 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt index be14ade6..d488bd83 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt @@ -76,15 +76,17 @@ fun findDaoMethod( .trim('.') val daoJavaFile = project.findFile(daoFile) - findDaoClass(module, daoClassName)?.let { daoClass -> - val daoMethod = - // TODO Support Kotlin Project - when (daoJavaFile) { - is PsiJavaFile -> findUseSqlDaoMethod(daoJavaFile, methodName) - else -> null - } - return daoMethod - } + val paramParameter = CommonPathParameter(module) + findDaoClass(module, paramParameter.isTest(originalFile.virtualFile), daoClassName) + ?.let { daoClass -> + val daoMethod = + // TODO Support Kotlin Project + when (daoJavaFile) { + is PsiJavaFile -> findUseSqlDaoMethod(daoJavaFile, methodName) + else -> null + } + return daoMethod + } } else { val fileType = getExtension(daoFile.fileType.name) val jarRootPath = virtualFile.path.substringBefore("jar!").plus("jar!") @@ -164,8 +166,9 @@ private fun searchDaoFile( private fun findDaoClass( module: Module, + includeTest: Boolean, daoClassName: String, -): PsiClass? = module.getJavaClazz(true, daoClassName) +): PsiClass? = module.getJavaClazz(includeTest, daoClassName) /** * Generate Dao deployment path from SQL file path diff --git a/src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt b/src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt index 2a4bda8d..c3610368 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt @@ -28,6 +28,7 @@ import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiClass import com.intellij.testFramework.PsiTestUtil import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase +import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH import org.domaframework.doma.intellij.extension.getResourcesSQLFile import org.domaframework.doma.intellij.setting.SettingComponent import org.domaframework.doma.intellij.setting.state.DomaToolsCustomFunctionSettings @@ -39,7 +40,8 @@ import java.io.File @Ignore open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { protected val packagePath = "doma/example" - private val resourceRoot = "main/resources" + private val sourceRoot = "java" + private val resourceRoot = "resources" override fun getTestDataPath(): String = "src/test/testData/src/main/" @@ -125,9 +127,21 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { } private fun setDirectoryRoot() { + WriteAction.runAndWait { + ModuleRootModificationUtil.updateModel(myFixture.module) { model -> + val iterator = model.contentEntries.iterator() + while (iterator.hasNext()) { + val entry = iterator.next() + if (entry.file == null || entry.file?.name == "src") { + model.removeContentEntry(entry) + } + } + } + } + val mainDir = myFixture.tempDirFixture.findOrCreateDir("main") - val javaDir = myFixture.tempDirFixture.findOrCreateDir("main/java") - val resourcesDir = myFixture.tempDirFixture.findOrCreateDir(resourceRoot) + val javaDir = myFixture.tempDirFixture.findOrCreateDir("main/$sourceRoot") + val resourcesDir = myFixture.tempDirFixture.findOrCreateDir("main/$resourceRoot") WriteAction.runAndWait { ModuleRootModificationUtil.updateModel(myFixture.module) { model -> val contentEntry = model.addContentEntry(mainDir) @@ -139,18 +153,18 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { fun addDaoJavaFile(vararg fileNames: String) { for (fileName in fileNames) { - val file = File("$testDataPath/java/$packagePath/dao/$fileName") + val file = File("$testDataPath/$sourceRoot/$packagePath/dao/$fileName") myFixture.addFileToProject( - "main/java/$packagePath/dao/$fileName", + "main/$sourceRoot/$packagePath/dao/$fileName", file.readText(), ) } } private fun addEntityJavaFile(fileName: String) { - val file = File("$testDataPath/java/$packagePath/entity/$fileName") + val file = File("$testDataPath/$sourceRoot/$packagePath/entity/$fileName") myFixture.addFileToProject( - "main/java/$packagePath/entity/$fileName", + "main/$sourceRoot/$packagePath/entity/$fileName", file.readText(), ) } @@ -158,7 +172,7 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { fun addResourceEmptyFile(vararg sqlFileNames: String) { for (sqlFileName in sqlFileNames) { myFixture.addFileToProject( - "$resourceRoot/META-INF/$packagePath/dao/$sqlFileName", + "main/$resourceRoot/$RESOURCES_META_INF_PATH/$packagePath/dao/$sqlFileName", "", ) } @@ -166,9 +180,9 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { fun addSqlFile(vararg sqlNames: String) { for (sqlName in sqlNames) { - val file = File("$testDataPath/resources/META-INF/$packagePath/dao/$sqlName") + val file = File("$testDataPath/$resourceRoot/$RESOURCES_META_INF_PATH/$packagePath/dao/$sqlName") myFixture.addFileToProject( - "$resourceRoot/META-INF/$packagePath/dao/$sqlName", + "main/$resourceRoot/$RESOURCES_META_INF_PATH/$packagePath/dao/$sqlName", file.readText(), ) } @@ -177,7 +191,7 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() { fun findSqlFile(sqlName: String): VirtualFile? { val module = myFixture.module return module?.getResourcesSQLFile( - "$packagePath/dao/$sqlName", + "$RESOURCES_META_INF_PATH/$packagePath/dao/$sqlName", false, ) } From 4e211a3539e86aa408c1565ef21aa05d3b94bd5a Mon Sep 17 00:00:00 2001 From: xterao Date: Tue, 20 May 2025 19:37:05 +0900 Subject: [PATCH 5/5] Configuring Custom Function SupportResolving Class Conflicts --- .../common/CommonPathParameterHelper.kt | 99 +++++++++++++++++-- .../setting/state/DomaToolsSettings.kt | 96 ++---------------- 2 files changed, 96 insertions(+), 99 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameterHelper.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameterHelper.kt index 2267912e..49216d30 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameterHelper.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameterHelper.kt @@ -15,15 +15,96 @@ */ package org.domaframework.doma.intellij.common -// TODO Dynamically build the source directory path and retrieve subproject info -// by inspecting file metadata instead of using string manipulation. -object CommonPathParameterHelper { - val SRC_MAIN_PATH: String - get() = "/src/main" +import com.intellij.openapi.module.Module +import com.intellij.openapi.roots.ModuleRootManager +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.jps.model.java.JavaResourceRootType +import org.jetbrains.jps.model.java.JavaSourceRootType - val RESOURCES_PATH: String - get() = "resources" +val RESOURCES_META_INF_PATH: String + get() = "META-INF" - val RESOURCES_META_INF_PATH: String - get() = "META-INF" +class CommonPathParameter( + module: Module?, +) { + /** + * module base path ex)Absolute path of "/src/main" + */ + var moduleBasePath: VirtualFile? = null + + /** + * module source directories ex) Absolute path of "/src/main/java","/src/main/kotlin" + */ + var moduleSourceDirectories: MutableList = mutableListOf() + + /** + * module resource directory ex) Absolute path of "/src/main/resources" + */ + var moduleResourceDirectories: MutableList = mutableListOf() + + var moduleTestSourceDirectories: MutableList = mutableListOf() + var moduleTestResourceDirectories: MutableList = mutableListOf() + + init { + setModuleSourcesFiles(module) + } + + private fun setModuleSourcesFiles(module: Module?) { + if (module == null) return + + val modulemanager = ModuleRootManager.getInstance(module) + + moduleSourceDirectories.clear() + modulemanager?.contentEntries?.firstOrNull()?.let { entry -> + moduleBasePath = entry.file + entry.sourceFolders.map { folder -> + val file = folder.file + if (file != null) { + when (folder.rootType) { + JavaSourceRootType.SOURCE -> { + moduleSourceDirectories.add(file) + } + + JavaSourceRootType.TEST_SOURCE -> { + moduleTestSourceDirectories.add(file) + } + + JavaResourceRootType.RESOURCE -> { + moduleResourceDirectories.add(file) + } + + JavaResourceRootType.TEST_RESOURCE -> { + moduleTestResourceDirectories.add(file) + } + } + } + } + } + } + + fun isTest(file: VirtualFile): Boolean { + val testSource = + moduleTestSourceDirectories.firstOrNull { testSource -> + file.path.contains(testSource.path) + } + if (testSource != null) return true + + return moduleTestResourceDirectories.firstOrNull { testSource -> + file.path.contains(testSource.path) + } != null + } + + fun getResources(file: VirtualFile): MutableList = + if (isTest(file)) { + moduleTestResourceDirectories + } else { + moduleResourceDirectories + } + + fun getSources(file: VirtualFile): MutableList = + if (isTest(file)) { + moduleTestSourceDirectories + } else { + moduleSourceDirectories + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt index 49216d30..5ec9891d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/setting/state/DomaToolsSettings.kt @@ -13,98 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.common +package org.domaframework.doma.intellij.setting.state -import com.intellij.openapi.module.Module -import com.intellij.openapi.roots.ModuleRootManager -import com.intellij.openapi.vfs.VirtualFile -import org.jetbrains.jps.model.java.JavaResourceRootType -import org.jetbrains.jps.model.java.JavaSourceRootType +import org.domaframework.doma.intellij.setting.SettingComponent -val RESOURCES_META_INF_PATH: String - get() = "META-INF" +interface DomaToolsSettings { + fun isModified(component: SettingComponent?): Boolean -class CommonPathParameter( - module: Module?, -) { - /** - * module base path ex)Absolute path of "/src/main" - */ - var moduleBasePath: VirtualFile? = null + fun apply(component: SettingComponent?) - /** - * module source directories ex) Absolute path of "/src/main/java","/src/main/kotlin" - */ - var moduleSourceDirectories: MutableList = mutableListOf() - - /** - * module resource directory ex) Absolute path of "/src/main/resources" - */ - var moduleResourceDirectories: MutableList = mutableListOf() - - var moduleTestSourceDirectories: MutableList = mutableListOf() - var moduleTestResourceDirectories: MutableList = mutableListOf() - - init { - setModuleSourcesFiles(module) - } - - private fun setModuleSourcesFiles(module: Module?) { - if (module == null) return - - val modulemanager = ModuleRootManager.getInstance(module) - - moduleSourceDirectories.clear() - modulemanager?.contentEntries?.firstOrNull()?.let { entry -> - moduleBasePath = entry.file - entry.sourceFolders.map { folder -> - val file = folder.file - if (file != null) { - when (folder.rootType) { - JavaSourceRootType.SOURCE -> { - moduleSourceDirectories.add(file) - } - - JavaSourceRootType.TEST_SOURCE -> { - moduleTestSourceDirectories.add(file) - } - - JavaResourceRootType.RESOURCE -> { - moduleResourceDirectories.add(file) - } - - JavaResourceRootType.TEST_RESOURCE -> { - moduleTestResourceDirectories.add(file) - } - } - } - } - } - } - - fun isTest(file: VirtualFile): Boolean { - val testSource = - moduleTestSourceDirectories.firstOrNull { testSource -> - file.path.contains(testSource.path) - } - if (testSource != null) return true - - return moduleTestResourceDirectories.firstOrNull { testSource -> - file.path.contains(testSource.path) - } != null - } - - fun getResources(file: VirtualFile): MutableList = - if (isTest(file)) { - moduleTestResourceDirectories - } else { - moduleResourceDirectories - } - - fun getSources(file: VirtualFile): MutableList = - if (isTest(file)) { - moduleTestSourceDirectories - } else { - moduleSourceDirectories - } + fun reset(component: SettingComponent?) }