Skip to content

Commit 1432342

Browse files
authored
Merge pull request #181 from domaframework/feature/dynamic-retrieval-of-source-directory-names
Dynamic retrieval of source directory names
2 parents 837597d + 4e211a3 commit 1432342

File tree

18 files changed

+291
-215
lines changed

18 files changed

+291
-215
lines changed

src/main/java/org/domaframework/doma/intellij/psi/SqlElCommentExprImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ public SqlElCommentExprImpl(@NotNull ASTNode node) {
4141
super(node);
4242
}
4343

44-
@Override
45-
public @NotNull ASTNode getNode() {
46-
return super.getNode();
47-
}
48-
4944
@Override
5045
public @NotNull IElementType getTokenType() {
5146
return getNode().getElementType();

src/main/kotlin/org/domaframework/doma/intellij/action/sql/BindVariableElement.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class JumpToDaoFromSQLAction : AnAction() {
5656
val project = e.project ?: return
5757
val daoFile = findDaoFile(project, file) ?: return
5858

59-
val nameWithoutExtension = file.virtualFile?.nameWithoutExtension ?: return
60-
jumpToDaoMethod(project, nameWithoutExtension, daoFile)
59+
val sqlFileName = file.virtualFile?.nameWithoutExtension ?: return
60+
jumpToDaoMethod(project, sqlFileName, daoFile)
6161
PluginLoggerUtil.countLoggingByAction(
6262
this::class.java.simpleName,
6363
"JumpToDao",

src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameterHelper.kt

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

18-
// TODO Dynamically build the source directory path and retrieve subproject info
19-
// by inspecting file metadata instead of using string manipulation.
20-
object CommonPathParameterHelper {
21-
val SRC_MAIN_PATH: String
22-
get() = "/src/main"
18+
import com.intellij.openapi.module.Module
19+
import com.intellij.openapi.roots.ModuleRootManager
20+
import com.intellij.openapi.vfs.VirtualFile
21+
import org.jetbrains.jps.model.java.JavaResourceRootType
22+
import org.jetbrains.jps.model.java.JavaSourceRootType
2323

24-
val RESOURCES_PATH: String
25-
get() = "resources"
24+
val RESOURCES_META_INF_PATH: String
25+
get() = "META-INF"
2626

27-
val RESOURCES_META_INF_PATH: String
28-
get() = "META-INF"
27+
class CommonPathParameter(
28+
module: Module?,
29+
) {
30+
/**
31+
* module base path ex)Absolute path of "/src/main"
32+
*/
33+
var moduleBasePath: VirtualFile? = null
34+
35+
/**
36+
* module source directories ex) Absolute path of "/src/main/java","/src/main/kotlin"
37+
*/
38+
var moduleSourceDirectories: MutableList<VirtualFile> = mutableListOf()
39+
40+
/**
41+
* module resource directory ex) Absolute path of "/src/main/resources"
42+
*/
43+
var moduleResourceDirectories: MutableList<VirtualFile> = mutableListOf()
44+
45+
var moduleTestSourceDirectories: MutableList<VirtualFile> = mutableListOf()
46+
var moduleTestResourceDirectories: MutableList<VirtualFile> = mutableListOf()
47+
48+
init {
49+
setModuleSourcesFiles(module)
50+
}
51+
52+
private fun setModuleSourcesFiles(module: Module?) {
53+
if (module == null) return
54+
55+
val modulemanager = ModuleRootManager.getInstance(module)
56+
57+
moduleSourceDirectories.clear()
58+
modulemanager?.contentEntries?.firstOrNull()?.let { entry ->
59+
moduleBasePath = entry.file
60+
entry.sourceFolders.map { folder ->
61+
val file = folder.file
62+
if (file != null) {
63+
when (folder.rootType) {
64+
JavaSourceRootType.SOURCE -> {
65+
moduleSourceDirectories.add(file)
66+
}
67+
68+
JavaSourceRootType.TEST_SOURCE -> {
69+
moduleTestSourceDirectories.add(file)
70+
}
71+
72+
JavaResourceRootType.RESOURCE -> {
73+
moduleResourceDirectories.add(file)
74+
}
75+
76+
JavaResourceRootType.TEST_RESOURCE -> {
77+
moduleTestResourceDirectories.add(file)
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
fun isTest(file: VirtualFile): Boolean {
86+
val testSource =
87+
moduleTestSourceDirectories.firstOrNull { testSource ->
88+
file.path.contains(testSource.path)
89+
}
90+
if (testSource != null) return true
91+
92+
return moduleTestResourceDirectories.firstOrNull { testSource ->
93+
file.path.contains(testSource.path)
94+
} != null
95+
}
96+
97+
fun getResources(file: VirtualFile): MutableList<VirtualFile> =
98+
if (isTest(file)) {
99+
moduleTestResourceDirectories
100+
} else {
101+
moduleResourceDirectories
102+
}
103+
104+
fun getSources(file: VirtualFile): MutableList<VirtualFile> =
105+
if (isTest(file)) {
106+
moduleTestSourceDirectories
107+
} else {
108+
moduleSourceDirectories
109+
}
29110
}

src/main/kotlin/org/domaframework/doma/intellij/common/FileTypeCheck.kt

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package org.domaframework.doma.intellij.common
1717

1818
import com.intellij.openapi.fileTypes.FileTypeManager
19-
import com.intellij.openapi.vfs.VirtualFile
2019
import com.intellij.psi.PsiFile
21-
import org.domaframework.doma.intellij.common.CommonPathParameterHelper.SRC_MAIN_PATH
20+
21+
val sourceExtensionNames: List<String> = listOf("JAVA", "Kotlin", "CLASS")
2222

2323
/**
2424
* Get extension by file type identifier
@@ -66,36 +66,3 @@ fun isInjectionSqlFile(file: PsiFile): Boolean {
6666
} &&
6767
!(filePath.endsWith(".sql") || filePath.endsWith(".script"))
6868
}
69-
70-
/**
71-
* Dao file search for SQL files
72-
*/
73-
fun searchDaoFile(
74-
contentRoot: VirtualFile?,
75-
originFilePath: String,
76-
relativeDaoFilePath: String,
77-
): VirtualFile? {
78-
val projectRootPath = contentRoot?.path ?: return null
79-
if (projectRootPath.endsWith(SRC_MAIN_PATH)) {
80-
return contentRoot.findFileByRelativePath(relativeDaoFilePath)
81-
}
82-
83-
if (projectRootPath.length > originFilePath.length) {
84-
return null
85-
}
86-
87-
// TODO Dynamically build the source directory path and retrieve subproject info
88-
// by inspecting file metadata instead of using string manipulation.
89-
val index = originFilePath.indexOf(SRC_MAIN_PATH)
90-
val projectRootPathBefore = projectRootPath.substringBefore(SRC_MAIN_PATH)
91-
if (index < 0 || projectRootPathBefore.length < index) return null
92-
val subProjectName =
93-
originFilePath.substring(projectRootPathBefore.length, index)
94-
95-
val daoFile =
96-
contentRoot
97-
.findFileByRelativePath(subProjectName)
98-
?.findFileByRelativePath(relativeDaoFilePath)
99-
100-
return daoFile
101-
}

src/main/kotlin/org/domaframework/doma/intellij/common/JarFileSearch.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.domaframework.doma.intellij.common
1818
import com.intellij.openapi.vfs.StandardFileSystems
1919
import com.intellij.openapi.vfs.VirtualFile
2020
import com.intellij.psi.PsiFile
21-
import org.domaframework.doma.intellij.common.CommonPathParameterHelper.RESOURCES_META_INF_PATH
2221

2322
fun getJarRoot(
2423
virtualFile: VirtualFile,

0 commit comments

Comments
 (0)