Skip to content

Commit 8cb806b

Browse files
committed
Refactor SQL file path handling to use module resources and improve configuration retrieval
1 parent 6038e90 commit 8cb806b

File tree

11 files changed

+59
-82
lines changed

11 files changed

+59
-82
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/config/DomaCompileConfigUtil.kt

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package org.domaframework.doma.intellij.common.config
1717

18-
import com.intellij.openapi.project.Project
19-
import com.intellij.openapi.vfs.VirtualFile
20-
import org.domaframework.doma.intellij.common.CommonPathParameterUtil
18+
import com.intellij.openapi.module.Module
19+
import org.domaframework.doma.intellij.extension.getResourcesFile
2120
import java.util.concurrent.ConcurrentHashMap
2221

2322
object DomaCompileConfigUtil {
@@ -30,40 +29,34 @@ object DomaCompileConfigUtil {
3029

3130
/**
3231
* Get the value of the specified key from doma.compile.config
33-
* @param project active project
34-
* @param resourcePaths the path to the resource directories
32+
* @param module the module to retrieve the configuration from
33+
* @param isTest true if the configuration is for test sources, false for main sources
3534
* @param key the key to retrieve the value for
3635
* @return the value associated with the key, or null if not found
3736
*/
3837
fun getConfigValue(
39-
project: Project,
40-
resourcePaths: List<VirtualFile>,
38+
module: Module,
39+
isTest: Boolean,
4140
key: String,
4241
): String? {
43-
resourcePaths.forEach { resourcePath ->
44-
if (resourcePath.isValid) {
45-
val configVFile = resourcePath.findChild("doma.compile.config")
46-
val cacheKey = "${project.basePath}/${resourcePath.path}/doma.compile.config"
47-
val lastModified = configVFile?.timeStamp ?: 0L
48-
val cached = configCache[cacheKey]
42+
val settingFileName = "doma.compile.config"
43+
val settingFile = module.getResourcesFile(settingFileName, isTest)
44+
val cacheKey = "${settingFile?.path}/$settingFileName"
45+
val lastModified = settingFile?.timeStamp ?: 0L
46+
val cached = configCache[cacheKey]
4947

50-
val props =
51-
if (cached == null || cached.second != lastModified) {
52-
val loadedProps =
53-
configVFile?.inputStream?.use { input ->
54-
java.util.Properties().apply { load(input) }
55-
} ?: java.util.Properties()
56-
configCache[cacheKey] = loadedProps to lastModified
57-
loadedProps
58-
} else {
59-
cached.first
60-
}
61-
val propProperty = props.getProperty(key)
62-
if (propProperty != null) return propProperty
48+
val props =
49+
if (cached == null || cached.second != lastModified) {
50+
val loadedProps =
51+
settingFile?.inputStream?.use { input ->
52+
java.util.Properties().apply { load(input) }
53+
} ?: java.util.Properties()
54+
configCache[cacheKey] = loadedProps to lastModified
55+
loadedProps
6356
} else {
64-
CommonPathParameterUtil.clearCache()
57+
cached.first
6558
}
66-
}
67-
return null
59+
val propProperty = props.getProperty(key)
60+
return propProperty
6861
}
6962
}

src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import org.domaframework.doma.intellij.common.getExtension
3939
import org.domaframework.doma.intellij.extension.findFile
4040
import org.domaframework.doma.intellij.extension.getContentRoot
4141
import org.domaframework.doma.intellij.extension.getModule
42-
import org.domaframework.doma.intellij.extension.getResourcesSQLFile
42+
import org.domaframework.doma.intellij.extension.getResourcesFile
4343
import org.domaframework.doma.intellij.extension.getSourceRootDir
4444
import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
4545
import org.domaframework.doma.intellij.setting.SqlLanguage
@@ -142,14 +142,10 @@ class PsiDaoMethod(
142142
return
143143
} else {
144144
sqlFile =
145-
if (sqlFilePath.isNotEmpty()) {
146-
module.getResourcesSQLFile(
147-
sqlFilePath,
148-
isTest,
149-
)
150-
} else {
151-
null
152-
}
145+
module.getResourcesFile(
146+
sqlFilePath,
147+
isTest,
148+
)
153149
return
154150
}
155151
}
@@ -185,7 +181,7 @@ class PsiDaoMethod(
185181
val isTest = CommonPathParameterUtil.isTest(module, daoFile)
186182

187183
val sqlDir = sqlFilePath.replace("/$sqlFileName", "")
188-
val existSqlDir = module.getResourcesSQLFile("$RESOURCES_META_INF_PATH/$sqlDir", isTest)
184+
val existSqlDir = module.getResourcesFile(sqlDir, isTest)
189185
val resourceDir = existSqlDir ?.let { psiProject.getSourceRootDir(it) }
190186
val resourcesDirPath = resourceDir?.nameWithoutExtension ?: "resources"
191187

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/collector/FunctionCallCollector.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,15 @@ class FunctionCallCollector(
3636
public override fun collect(): List<LookupElement>? {
3737
var functions = mutableSetOf<PsiMethod>()
3838
val project = file?.project
39-
val module = file?.module
40-
val resourcePaths =
41-
if (file?.virtualFile != null && module != null) {
42-
CommonPathParameterUtil
43-
.getResources(module, file.virtualFile)
44-
} else {
45-
emptyList()
46-
}
39+
val module = file?.module ?: return null
40+
val isTest = CommonPathParameterUtil.isTest(module, file.virtualFile)
4741

4842
val customFunctionClassName =
49-
project?.let {
50-
DomaCompileConfigUtil.getConfigValue(
51-
it,
52-
resourcePaths,
53-
"doma.expr.functions",
54-
)
55-
}
43+
DomaCompileConfigUtil.getConfigValue(
44+
module,
45+
isTest,
46+
"doma.expr.functions",
47+
)
5648

5749
val expressionFunctionInterface =
5850
project?.let { ExpressionFunctionsHelper.setExpressionFunctionsInterface(it) }

src/main/kotlin/org/domaframework/doma/intellij/extension/ModuleExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun Module.getPackagePathFromDaoPath(daoFile: VirtualFile): VirtualFile? {
3434
getRelativeSqlFilePathFromDaoFilePath(daoFile, this)
3535
} ?: ""
3636
val isTest = CommonPathParameterUtil.isTest(this, daoFile)
37-
return this.getResourcesSQLFile(
37+
return this.getResourcesFile(
3838
packagePath,
3939
isTest,
4040
)
@@ -57,7 +57,7 @@ fun Module.getJavaClazz(
5757
* @param includeTest true: test source, false: main source
5858
* @return SQL file
5959
*/
60-
fun Module.getResourcesSQLFile(
60+
fun Module.getResourcesFile(
6161
relativePath: String,
6262
includeTest: Boolean,
6363
): VirtualFile? =

src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/processor/InspectionFunctionCallVisitorProcessor.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ class InspectionFunctionCallVisitorProcessor(
3434
) : InspectionVisitorProcessor(shortName) {
3535
fun check(holder: ProblemsHolder) {
3636
val project = element.project
37+
val module = element.module ?: return
3738
val expressionHelper = ExpressionFunctionsHelper
3839
val expressionFunctionalInterface = expressionHelper.setExpressionFunctionsInterface(project)
3940
val functionName = element.elIdExpr
4041

41-
val resources =
42-
element.module
43-
?.let { CommonPathParameterUtil.getResources(it, element.containingFile.virtualFile) }
44-
?: emptyList()
45-
val customFunctionClassName = DomaCompileConfigUtil.getConfigValue(project, resources, "doma.expr.functions")
42+
val isTest = CommonPathParameterUtil.isTest(module, element.containingFile.virtualFile)
43+
val customFunctionClassName = DomaCompileConfigUtil.getConfigValue(module, isTest, "doma.expr.functions")
4644

4745
var result: ValidationResult =
4846
ValidationInvalidFunctionCallResult(

src/main/kotlin/org/domaframework/doma/intellij/refactoring/dao/DaoPackageRenameListenerProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import org.domaframework.doma.intellij.common.CommonPathParameterUtil
3333
import org.domaframework.doma.intellij.common.RESOURCES_META_INF_PATH
3434
import org.domaframework.doma.intellij.common.dao.getDaoClass
3535
import org.domaframework.doma.intellij.common.util.PluginLoggerUtil
36-
import org.domaframework.doma.intellij.extension.getResourcesSQLFile
36+
import org.domaframework.doma.intellij.extension.getResourcesFile
3737
import org.jetbrains.kotlin.idea.base.util.module
3838
import java.io.IOException
3939

@@ -149,7 +149,7 @@ class DaoPackageRenameListenerProcessor : RefactoringElementListenerProvider {
149149
try {
150150
val newDir = VfsUtil.createDirectories(newPath)
151151
val oldResourcePath =
152-
module.getResourcesSQLFile(
152+
module.getResourcesFile(
153153
RESOURCES_META_INF_PATH + "/" + oldQualifiedName.replace(".", "/"),
154154
isTest,
155155
)

src/main/kotlin/org/domaframework/doma/intellij/reference/SqlElFunctionCallExprReference.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,20 @@ class SqlElFunctionCallExprReference(
3737
val variableName = functionCallExpr.elIdExpr.text ?: ""
3838

3939
val project = element.project
40-
val module = file.module
40+
val module = file.module ?: return null
4141
val expressionFunctionsInterface =
4242
ExpressionFunctionsHelper.setExpressionFunctionsInterface(project)
4343
?: return null
4444

45-
val resourcePaths =
46-
module?.let {
47-
CommonPathParameterUtil
48-
.getResources(it, file.virtualFile)
49-
} ?: emptyList()
50-
51-
val customFunctionClassName = DomaCompileConfigUtil.getConfigValue(project, resourcePaths, "doma.expr.functions")
45+
val isTest =
46+
CommonPathParameterUtil
47+
.isTest(module, file.virtualFile)
48+
val customFunctionClassName = DomaCompileConfigUtil.getConfigValue(module, isTest, "doma.expr.functions")
5249

5350
val implementsClass =
5451
if (customFunctionClassName != null) {
55-
val expressionFunction = project.getJavaClazz(customFunctionClassName)
52+
val customFunctionClass = customFunctionClassName
53+
val expressionFunction = project.getJavaClazz(customFunctionClass)
5654
if (ExpressionFunctionsHelper.isInheritor(expressionFunction)) {
5755
expressionFunction
5856
} else {

src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolStartupActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.setting
1717

1818
import com.intellij.openapi.application.PathManager
1919
import com.intellij.openapi.project.Project
20+
import com.intellij.openapi.roots.ModuleRootListener
2021
import com.intellij.openapi.startup.ProjectActivity
2122
import org.domaframework.doma.intellij.common.util.PluginUtil
2223

@@ -36,6 +37,9 @@ class DomaToolStartupActivity : ProjectActivity {
3637
}
3738

3839
private fun registerModuleRootListener(project: Project) {
39-
DomaToolsModuleRootListener.updateModuleDirectoryCache(project)
40+
project.messageBus.connect().subscribe(
41+
ModuleRootListener.TOPIC,
42+
DomaToolsModuleRootListener(project),
43+
)
4044
}
4145
}

src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsModuleRootListener.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import com.intellij.openapi.roots.ModuleRootEvent
2121
import com.intellij.openapi.roots.ModuleRootListener
2222
import org.domaframework.doma.intellij.common.CommonPathParameterUtil
2323

24-
object DomaToolsModuleRootListener : ModuleRootListener {
24+
class DomaToolsModuleRootListener(
25+
private val project: Project,
26+
) : ModuleRootListener {
2527
override fun rootsChanged(event: ModuleRootEvent) {
26-
val project = event.source as? Project ?: return
2728
updateModuleDirectoryCache(project)
2829
}
2930

src/main/resources/META-INF/plugin.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@
9393
<supportsKotlinPluginMode supportsK2="true"/>
9494
</extensions>
9595

96-
<projectListeners>
97-
<listener class="org.domaframework.doma.intellij.setting.DomaToolsModuleRootListener"
98-
topic="com.intellij.openapi.roots.ModuleRootListener" />
99-
</projectListeners>
100-
10196
<!-- Action -->
10297
<actions>
10398
<group

0 commit comments

Comments
 (0)