1515 */
1616package org.domaframework.doma.intellij.common
1717
18+ import com.intellij.compiler.CompilerConfiguration
1819import com.intellij.openapi.module.Module
20+ import com.intellij.openapi.project.Project
1921import com.intellij.openapi.roots.ModuleRootManager
2022import com.intellij.openapi.vfs.VirtualFile
2123import org.jetbrains.jps.model.java.JavaResourceRootType
@@ -32,14 +34,12 @@ object CommonPathParameterUtil {
3234 /* *
3335 * Holds directory information for a module.
3436 *
35- * @property moduleBasePath The base path of the module.
3637 * @property moduleSourceDirectories List of source directories.
3738 * @property moduleResourceDirectories List of resource directories.
3839 * @property moduleTestSourceDirectories List of test source directories.
3940 * @property moduleTestResourceDirectories List of test resource directories.
4041 */
4142 data class ModulePaths (
42- val moduleBasePath : VirtualFile ? ,
4343 val moduleSourceDirectories : List <VirtualFile >,
4444 val moduleResourceDirectories : List <VirtualFile >,
4545 val moduleTestSourceDirectories : List <VirtualFile >,
@@ -51,12 +51,30 @@ object CommonPathParameterUtil {
5151
5252 /* *
5353 * Returns the directory information for the specified module (uses cache if available).
54- * If the module's directory structure has changed, call [refreshModulePaths] to update the cache.
5554 *
5655 * @param module The module to retrieve directory information for.
5756 * @return The cached or newly computed ModulePaths.
5857 */
59- fun getModulePaths (module : Module ): ModulePaths = modulePathCache[module] ? : refreshModulePaths(module)
58+ fun getModulePaths (module : Module ): ModulePaths ? = modulePathCache[module]
59+
60+ /* *
61+ * Checks if a given path is a generated directory based on annotation processor settings.
62+ *
63+ * @param module The module to check.
64+ * @param path The path to check.
65+ * @return True if the path is a generated directory, false otherwise.
66+ */
67+ private fun isGeneratedDirectory (
68+ module : Module ,
69+ path : String ,
70+ ): Boolean {
71+ val project: Project = module.project
72+ val compilerConfiguration = CompilerConfiguration .getInstance(project).getAnnotationProcessingConfiguration(module)
73+ val annotationProcessingConfiguration = compilerConfiguration.getGeneratedSourcesDirectoryName(false )
74+
75+ // Check if the path matches any of the generated source directories
76+ return path.contains(" /build/$annotationProcessingConfiguration /" )
77+ }
6078
6179 /* *
6280 * Refreshes the directory information for the specified module and updates the cache.
@@ -65,38 +83,53 @@ object CommonPathParameterUtil {
6583 * @param module The module to refresh.
6684 * @return The updated ModulePaths.
6785 */
68- fun refreshModulePaths (module : Module ): ModulePaths {
69- var basePath: VirtualFile ? = null
86+ fun refreshModulePaths (module : Module ) {
7087 val sourceDirs = mutableListOf<VirtualFile >()
7188 val resourceDirs = mutableListOf<VirtualFile >()
7289 val testSourceDirs = mutableListOf<VirtualFile >()
7390 val testResourceDirs = mutableListOf<VirtualFile >()
7491
7592 val moduleManager = ModuleRootManager .getInstance(module)
76- moduleManager.contentEntries.firstOrNull()?.let { entry ->
77- basePath = entry.file
78- entry.sourceFolders.forEach { folder ->
79- val file = folder.file
80- if (file != null ) {
81- when (folder.rootType) {
82- JavaSourceRootType .SOURCE -> sourceDirs.add(file)
83- JavaSourceRootType .TEST_SOURCE -> testSourceDirs.add(file)
84- JavaResourceRootType .RESOURCE -> resourceDirs.add(file)
85- JavaResourceRootType .TEST_RESOURCE -> testResourceDirs.add(file)
93+ moduleManager.contentEntries.forEach { entry ->
94+ val entryFile = entry.file
95+ if (entryFile != null && ! isGeneratedDirectory(module, entryFile.path)) {
96+ entry.sourceFolders.forEach { folder ->
97+ val file = folder.file
98+ if (file != null ) {
99+ when (folder.rootType) {
100+ JavaSourceRootType .SOURCE ->
101+ if (! sourceDirs.contains(file)) {
102+ sourceDirs.add(file)
103+ }
104+
105+ JavaSourceRootType .TEST_SOURCE ->
106+ if (! testSourceDirs.contains(file)) {
107+ testSourceDirs.add(file)
108+ }
109+
110+ JavaResourceRootType .RESOURCE ->
111+ if (! resourceDirs.contains(file)) {
112+ resourceDirs.add(file)
113+ }
114+
115+ JavaResourceRootType .TEST_RESOURCE ->
116+ if (! testResourceDirs.contains(file)) {
117+ testResourceDirs.add(file)
118+ }
119+ }
86120 }
87121 }
88122 }
89123 }
124+
90125 val paths =
91126 ModulePaths (
92- basePath,
93127 sourceDirs,
94128 resourceDirs,
95129 testSourceDirs,
96130 testResourceDirs,
97131 )
98132 modulePathCache[module] = paths
99- return paths
100133 }
101134
102135 /* *
@@ -110,7 +143,7 @@ object CommonPathParameterUtil {
110143 module : Module ,
111144 file : VirtualFile ,
112145 ): Boolean {
113- val paths = getModulePaths(module)
146+ val paths = getModulePaths(module) ? : return false
114147 if (paths.moduleTestSourceDirectories.any { file.path.contains(it.path) }) return true
115148 if (paths.moduleTestResourceDirectories.any { file.path.contains(it.path) }) return true
116149 return false
@@ -127,30 +160,14 @@ object CommonPathParameterUtil {
127160 fun getResources (
128161 module : Module ,
129162 file : VirtualFile ,
130- ): List <VirtualFile > =
131- if (isTest(module, file)) {
132- getModulePaths(module).moduleTestResourceDirectories
133- } else {
134- getModulePaths(module).moduleResourceDirectories
135- }
136-
137- /* *
138- * Returns the source directories for the given file in the specified module.
139- * If the file is in a test directory, test source directories are returned.
140- *
141- * @param module The module to check.
142- * @param file The file to check.
143- * @return List of source directories.
144- */
145- fun getSources (
146- module : Module ,
147- file : VirtualFile ,
148- ): List <VirtualFile > =
149- if (isTest(module, file)) {
150- getModulePaths(module).moduleTestSourceDirectories
163+ ): List <VirtualFile > {
164+ val modulePaths = getModulePaths(module) ? : return emptyList()
165+ return if (isTest(module, file)) {
166+ modulePaths.moduleTestResourceDirectories
151167 } else {
152- getModulePaths(module).moduleSourceDirectories
168+ modulePaths.moduleResourceDirectories
153169 }
170+ }
154171
155172 /* *
156173 * Clears the module directory cache. Call this if the module structure changes.
0 commit comments