@@ -27,18 +27,19 @@ import com.intellij.psi.PsiManager
2727import com.intellij.psi.PsiMethod
2828import com.intellij.psi.search.GlobalSearchScope
2929import com.intellij.psi.util.PsiTreeUtil
30- import org.domaframework.doma.intellij.common.CommonPathParameter.Companion.RESOURCES_META_INF_PATH
31- import org.domaframework.doma.intellij.common.CommonPathParameter.Companion.RESOURCES_PATH
30+ import org.domaframework.doma.intellij.common.CommonPathParameter
31+ import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH
3232import org.domaframework.doma.intellij.common.getExtension
3333import 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.searchDaoFile
37+ import org.domaframework.doma.intellij.common.sourceExtensionNames
3838import org.domaframework.doma.intellij.extension.findFile
3939import org.domaframework.doma.intellij.extension.getContentRoot
4040import org.domaframework.doma.intellij.extension.getJavaClazz
4141import org.domaframework.doma.intellij.extension.getModule
42+ import org.jetbrains.kotlin.idea.base.util.module
4243
4344/* *
4445 * Get Dao method corresponding to SQL file
@@ -57,28 +58,27 @@ fun findDaoMethod(
5758 }
5859 } else if (isSupportFileType(originalFile)) {
5960 // TODO: Add Support Kotlin
60- val fileTypeName = " JAVA"
6161 val methodName = virtualFile.nameWithoutExtension
6262 val daoFile = daoFile ? : findDaoFile(project, originalFile) ? : return null
6363 if (module != null ) {
6464 val relativePath =
65- formatDaoPathFromSqlFilePath (
65+ getDaoPathFromSqlFilePath (
6666 originalFile,
6767 project.getContentRoot(virtualFile)?.path ? : " " ,
68- fileTypeName,
6968 )
7069 // get ClassPath with package name
7170 val daoClassName: String =
7271 relativePath
7372 .substringBefore(" ." )
7473 .replace(" /" , " ." )
7574 .replace(" \\ " , " ." )
76- .substringAfter(" .${getExtension(fileTypeName)} " )
7775 .replace(" .." , " ." )
7876 .trim(' .' )
77+
7978 val daoJavaFile = project.findFile(daoFile)
8079 findDaoClass(module, daoClassName)?.let { daoClass ->
8180 val daoMethod =
81+ // TODO Support Kotlin Project
8282 when (daoJavaFile) {
8383 is PsiJavaFile -> findUseSqlDaoMethod(daoJavaFile, methodName)
8484 else -> null
@@ -130,10 +130,36 @@ fun findDaoFile(
130130 if (contentRoot == null ) {
131131 return getJarRoot(virtualFile, sqlFile)
132132 }
133+ return searchDaoFile(sqlFile.module, contentRoot, sqlFile)
134+ }
135+
136+ /* *
137+ * Dao file search for SQL file
138+ */
139+ private fun searchDaoFile (
140+ module : Module ? ,
141+ contentRoot : VirtualFile ? ,
142+ sqlFile : PsiFile ,
143+ ): VirtualFile ? {
144+ val contentRootPath = contentRoot?.path ? : return null
145+ val pathParams = CommonPathParameter (module)
146+ val moduleBaseName = pathParams.moduleBasePath?.nameWithoutExtension ? : " "
133147 // TODO: Add Support Kotlin
134- val relativeFilePath =
135- formatDaoPathFromSqlFilePath(sqlFile, contentRoot.path, " JAVA" )
136- return searchDaoFile(contentRoot, virtualFile.path, relativeFilePath)
148+ val relativeDaoFilePaths =
149+ getDaoPathFromSqlFilePath(sqlFile, contentRoot.path)
150+ val sources = pathParams.getSources(sqlFile.virtualFile)
151+
152+ if (contentRootPath.endsWith(moduleBaseName) == true ) {
153+ sources.forEach { source ->
154+ sourceExtensionNames.forEach { extension ->
155+ val fileExtension = getExtension(extension)
156+ val findDaoFile =
157+ contentRoot.findFileByRelativePath(" ${source.nameWithoutExtension}$relativeDaoFilePaths .$fileExtension " )
158+ if (findDaoFile != null ) return findDaoFile
159+ }
160+ }
161+ }
162+ return null
137163}
138164
139165private fun findDaoClass (
@@ -143,40 +169,70 @@ private fun findDaoClass(
143169
144170/* *
145171 * Generate Dao deployment path from SQL file path
172+ * @param sqlFile SQL File
173+ * @param projectRootPath project content Root Path
174+ * @return
146175 */
147- fun formatDaoPathFromSqlFilePath (
148- relativeBaseSqlFile : PsiFile ,
176+ private fun getDaoPathFromSqlFilePath (
177+ sqlFile : PsiFile ,
149178 projectRootPath : String ,
150- extension : String ,
151179): String {
152- if (isInjectionSqlFile(relativeBaseSqlFile )) {
180+ if (isInjectionSqlFile(sqlFile )) {
153181 return " "
154182 }
155- val sqlPath = relativeBaseSqlFile.virtualFile?.path ? : return " "
183+ val module = sqlFile.module
184+ val sqlPath = sqlFile.virtualFile?.path ? : return " "
156185 var relativeFilePath = sqlPath.substring(projectRootPath.length)
157186 if (! relativeFilePath.startsWith(" /" )) {
158187 relativeFilePath = " /$relativeFilePath "
159188 }
160- val extensionType = getExtension(extension.uppercase())
161- return relativeFilePath
162- .replace(" /$RESOURCES_PATH " , " " )
163- .replace(RESOURCES_META_INF_PATH , extension.lowercase())
164- .replace(" /${relativeBaseSqlFile.name} " , " " )
165- .plus(" .$extensionType " )
189+ val pathParams = CommonPathParameter (module)
190+ val resources = pathParams.getResources(sqlFile.virtualFile)
191+
192+ return resources
193+ .firstOrNull { resource ->
194+ relativeFilePath.startsWith(" /" + resource.nameWithoutExtension)
195+ }?.let { resource ->
196+ relativeFilePath
197+ .replace(" ${resource.nameWithoutExtension} /$RESOURCES_META_INF_PATH /" , " " )
198+ .replace(" /${sqlFile.name} " , " " )
199+ } ? : " "
166200}
167201
168202/* *
169203 * Generate SqlFile path from Dao file path
204+ * @param daoFile Dao File
205+ * @param module The module to which the Dao file belongs
206+ * @return SqlFile path ex) META-INF/package/dao/DaoClassName/
170207 */
171- fun formatSqlPathFromDaoPath (
172- contentRootPath : String ,
208+ fun getRelativeSqlFilePathFromDaoFilePath (
173209 daoFile : VirtualFile ,
210+ module : Module ? ,
174211): String {
175- val fileType = daoFile.fileType.name
212+ if (module == null ) return " "
176213 val extension = daoFile.fileType.defaultExtension
177- val daoFilePath = daoFile.path
178- return daoFilePath
179- .replace(contentRootPath, RESOURCES_META_INF_PATH )
180- .replace(" /${fileType.lowercase()} /" , " /" )
181- .replace(" .$extension " , " " )
214+ val pathParams = CommonPathParameter (module)
215+ var relativeSqlFilePath =
216+ daoFile.path
217+ .replace(pathParams.moduleBasePath?.path ? : " " , " " )
218+ .replace(" .$extension " , " " )
219+ val isTest = pathParams.moduleTestSourceDirectories.firstOrNull { dir -> daoFile.path.contains(dir.path) } != null
220+ if (isTest) {
221+ pathParams.moduleTestSourceDirectories.forEach { source ->
222+ relativeSqlFilePath =
223+ relativeSqlFilePath.replace(
224+ " /" + source.nameWithoutExtension,
225+ RESOURCES_META_INF_PATH ,
226+ )
227+ }
228+ } else {
229+ pathParams.moduleSourceDirectories.forEach { source ->
230+ relativeSqlFilePath =
231+ relativeSqlFilePath.replace(
232+ " /" + source.nameWithoutExtension,
233+ RESOURCES_META_INF_PATH ,
234+ )
235+ }
236+ }
237+ return relativeSqlFilePath
182238}
0 commit comments