@@ -34,13 +34,11 @@ import org.domaframework.doma.intellij.common.getJarRoot
3434import org.domaframework.doma.intellij.common.getMethodDaoFilePath
3535import org.domaframework.doma.intellij.common.isInjectionSqlFile
3636import org.domaframework.doma.intellij.common.isSupportFileType
37- import org.domaframework.doma.intellij.common.sourceExtensionNames
38- import org.domaframework.doma.intellij.extension.findFile
3937import org.domaframework.doma.intellij.extension.getContentRoot
4038import org.domaframework.doma.intellij.extension.getJavaClazz
4139import org.domaframework.doma.intellij.extension.getModule
40+ import org.domaframework.doma.intellij.extension.getSourceRootDir
4241import org.jetbrains.kotlin.idea.base.util.module
43- import java.nio.file.Paths
4442
4543/* *
4644 * Get DAO method corresponding to SQL file
@@ -58,36 +56,24 @@ fun findDaoMethod(
5856 return PsiTreeUtil .getParentOfType(originalFile.context, PsiMethod ::class .java)
5957 }
6058 } else if (isSupportFileType(originalFile)) {
61- // TODO: Add Support Kotlin
6259 val methodName = virtualFile.nameWithoutExtension
6360 val daoFile = daoFile ? : findDaoFile(project, originalFile) ? : return null
6461 if (module != null ) {
65- val relativePath =
62+ val contentRootPath = project.getContentRoot(virtualFile)?.path ? : return null
63+ val daoJavaFile =
6664 getDaoPathFromSqlFilePath(
6765 originalFile,
68- project.getContentRoot(virtualFile)?.path ? : " " ,
69- )
70- // get ClassPath with package name
71- val daoClassName: String =
72- relativePath
73- .substringBefore(" ." )
74- .replace(" /" , " ." )
75- .replace(" \\ " , " ." )
76- .replace(" .." , " ." )
77- .trim(' .' )
66+ contentRootPath,
67+ ) ? : return null
7868
79- val daoJavaFile = project.findFile(daoFile)
80- val isTest = CommonPathParameterUtil .isTest(module, originalFile.virtualFile)
81- findDaoClass(module, isTest, daoClassName)
82- ?.let { daoClass ->
83- val daoMethod =
84- // TODO Support Kotlin Project
85- when (daoJavaFile) {
86- is PsiJavaFile -> findUseSqlDaoMethod(daoJavaFile, methodName)
87- else -> null
88- }
89- return daoMethod
69+ // TODO Support Kotlin Project
70+ val daoFile = daoJavaFile.containingFile
71+ val daoMethod =
72+ when (daoFile) {
73+ is PsiJavaFile -> findUseSqlDaoMethod(daoFile, methodName)
74+ else -> null
9075 }
76+ return daoMethod
9177 } else {
9278 val fileType = getExtension(daoFile.fileType.name)
9379 val jarRootPath = virtualFile.path.substringBefore(" jar!" ).plus(" jar!" )
@@ -133,78 +119,36 @@ fun findDaoFile(
133119 if (contentRoot == null ) {
134120 return getJarRoot(virtualFile, sqlFile)
135121 }
136- return searchDaoFile(sqlFile.module, contentRoot, sqlFile)
122+ return getDaoPathFromSqlFilePath(
123+ sqlFile,
124+ contentRoot.path,
125+ )?.containingFile?.virtualFile
137126}
138127
139- /* *
140- * DAO file search for SQL file
141- */
142- private fun searchDaoFile (
143- module : Module ? ,
144- contentRoot : VirtualFile ? ,
145- sqlFile : PsiFile ,
146- ): VirtualFile ? {
147- val contentRootPath = contentRoot?.path ? : return null
148- val pathParams = module?.let { CommonPathParameterUtil .getModulePaths(it) } ? : return null
149- val moduleBaseName =
150- pathParams.moduleBasePaths
151- .find { baseName -> Paths .get(contentRootPath).startsWith(Paths .get(baseName.path)) }
152- ?.nameWithoutExtension ? : " "
153- // TODO: Add Support Kotlin
154- val relativeDaoFilePaths =
155- getDaoPathFromSqlFilePath(sqlFile, contentRoot.path)
156- val sources = CommonPathParameterUtil .getSources(module, sqlFile.virtualFile)
157-
158- if (contentRootPath.endsWith(moduleBaseName) == true ) {
159- sources.forEach { source ->
160- sourceExtensionNames.forEach { extension ->
161- val fileExtension = getExtension(extension)
162- val findDaoFile =
163- contentRoot.findFileByRelativePath(" ${source.nameWithoutExtension}$relativeDaoFilePaths .$fileExtension " )
164- if (findDaoFile != null ) return findDaoFile
165- }
166- }
167- }
168- return null
169- }
170-
171- private fun findDaoClass (
172- module : Module ,
173- includeTest : Boolean ,
174- daoClassName : String ,
175- ): PsiClass ? = module.getJavaClazz(includeTest, daoClassName)
176-
177128/* *
178129 * Generate DAO deployment path from SQL file path
179130 * @param sqlFile SQL File
180- * @param projectRootPath project content Root Path
131+ * @param contentRootPath project content Root Path
181132 * @return
182133 */
183134private fun getDaoPathFromSqlFilePath (
184135 sqlFile : PsiFile ,
185- projectRootPath : String ,
186- ): String {
136+ contentRootPath : String ,
137+ ): PsiClass ? {
187138 if (isInjectionSqlFile(sqlFile)) {
188- return " "
189- }
190- val module = sqlFile.module
191- val sqlPath = sqlFile.virtualFile?.path ? : return " "
192- var relativeFilePath = sqlPath.substring(projectRootPath.length)
193- if (! relativeFilePath.startsWith(" /" )) {
194- relativeFilePath = " /$relativeFilePath "
139+ return null
195140 }
196- val resources =
197- module?.let { CommonPathParameterUtil .getResources(it, sqlFile.virtualFile) }
198- ? : emptyList()
141+ val module = sqlFile.module ? : return null
142+ val sqlPath = sqlFile.virtualFile?.path ? : return null
143+ val relativePath =
144+ sqlPath.substringAfter(contentRootPath, " " ).replace(" /$RESOURCES_META_INF_PATH " , " " )
145+ val resourcesRootPath = module.project.getSourceRootDir(sqlFile.virtualFile)?.name ? : " "
146+ val isTest = CommonPathParameterUtil .isTest(module, sqlFile.virtualFile)
199147
200- return resources
201- .find { resource ->
202- relativeFilePath.startsWith(" /${resource.nameWithoutExtension} /" )
203- }?.let { resource ->
204- relativeFilePath
205- .replace(" ${resource.nameWithoutExtension} /$RESOURCES_META_INF_PATH /" , " " )
206- .replace(" /${sqlFile.name} " , " " )
207- } ? : " "
148+ val packageName = relativePath.replaceFirst(" /$resourcesRootPath /" , " " ).replace(" /${sqlFile.name} " , " " ).replace(" /" , " ." )
149+ val daoClassFile = module.getJavaClazz(isTest, packageName)
150+
151+ return daoClassFile
208152}
209153
210154/* *
@@ -219,30 +163,14 @@ fun getRelativeSqlFilePathFromDaoFilePath(
219163): String {
220164 if (module == null ) return " "
221165 val extension = daoFile.fileType.defaultExtension
166+ val sourceRoot = module.project.getSourceRootDir(daoFile) ? : return " "
167+
168+ val project = module.project
169+ val sourceRootParent = project.getContentRoot(daoFile) ? : return " "
222170 val daoFilePath = daoFile.path
223- val pathParams = CommonPathParameterUtil .getModulePaths(module)
224- val containsModuleBaseName =
225- pathParams.moduleBasePaths
226- .find { basePath -> daoFilePath.contains(" ${basePath.path} /" ) }
227- ?.path ? : return " "
228- var relativeSqlFilePath =
229- daoFilePath
230- .replaceFirst(containsModuleBaseName, " " )
231- .replace(" .$extension " , " " )
232- val sources = CommonPathParameterUtil .getSources(module, daoFile)
233- sources
234- .find {
235- daoFilePath
236- .startsWith(containsModuleBaseName.plus(" /${it.nameWithoutExtension} /" ))
237- }?.let { source ->
238- val startSourceName = " /${source.nameWithoutExtension} "
239- if (relativeSqlFilePath.startsWith(" $startSourceName /" )) {
240- relativeSqlFilePath =
241- relativeSqlFilePath.replaceFirst(
242- startSourceName,
243- RESOURCES_META_INF_PATH ,
244- )
245- }
246- }
247- return relativeSqlFilePath
171+
172+ return daoFilePath
173+ .substringAfter(sourceRootParent.path)
174+ .replaceFirst(" /${sourceRoot.name} " , RESOURCES_META_INF_PATH )
175+ .replace(" .$extension " , " " )
248176}
0 commit comments