Skip to content

Commit 7e7d594

Browse files
committed
Implemented an action to generate SQL templates mutually between SQL files and Sql annotations.
1 parent 2185adb commit 7e7d594

File tree

8 files changed

+496
-1
lines changed

8 files changed

+496
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.action.dao
17+
18+
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
19+
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
20+
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
21+
import com.intellij.openapi.command.WriteCommandAction
22+
import com.intellij.openapi.editor.Editor
23+
import com.intellij.openapi.project.Project
24+
import com.intellij.psi.PsiElement
25+
import com.intellij.psi.PsiFile
26+
import com.intellij.psi.PsiMethod
27+
import com.intellij.psi.util.PsiTreeUtil
28+
import org.domaframework.doma.intellij.bundle.MessageBundle
29+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
30+
import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
31+
32+
/**
33+
* Intention action to convert @Sql annotation to SQL file
34+
*/
35+
class ConvertSqlAnnotationToFileAction : PsiElementBaseIntentionAction() {
36+
override fun getFamilyName(): String = MessageBundle.message("convert.sql.annotation.to.file.family")
37+
38+
override fun getText(): String = MessageBundle.message("convert.sql.annotation.to.file.text")
39+
40+
override fun isAvailable(
41+
project: Project,
42+
editor: Editor?,
43+
element: PsiElement,
44+
): Boolean {
45+
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return false
46+
val psiDaoMethod = PsiDaoMethod(project, method)
47+
48+
// Check if method has @Sql annotation
49+
if (!psiDaoMethod.useSqlAnnotation()) {
50+
return false
51+
}
52+
53+
// Check if method has @Insert, @Update, or @Delete annotation
54+
val supportedTypes =
55+
listOf(
56+
DomaAnnotationType.Select,
57+
DomaAnnotationType.Insert,
58+
DomaAnnotationType.Update,
59+
DomaAnnotationType.Delete,
60+
DomaAnnotationType.BatchInsert,
61+
DomaAnnotationType.BatchUpdate,
62+
DomaAnnotationType.BatchDelete,
63+
)
64+
65+
return supportedTypes.any { it.getPsiAnnotation(method) != null }
66+
}
67+
68+
override fun generatePreview(
69+
project: Project,
70+
editor: Editor,
71+
file: PsiFile,
72+
): IntentionPreviewInfo = IntentionPreviewInfo.EMPTY
73+
74+
override fun invoke(
75+
project: Project,
76+
editor: Editor?,
77+
element: PsiElement,
78+
) {
79+
// Do nothing when previewing
80+
if (IntentionPreviewUtils.isIntentionPreviewActive()) return
81+
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return
82+
val converter = SqlAnnotationConverter(project, method)
83+
WriteCommandAction.runWriteCommandAction(project) {
84+
converter.convertToSqlFile()
85+
}
86+
}
87+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.action.dao
17+
18+
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
19+
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
20+
import com.intellij.openapi.command.WriteCommandAction
21+
import com.intellij.openapi.editor.Editor
22+
import com.intellij.openapi.project.Project
23+
import com.intellij.psi.PsiElement
24+
import org.domaframework.doma.intellij.bundle.MessageBundle
25+
import org.domaframework.doma.intellij.common.dao.findDaoMethod
26+
import org.domaframework.doma.intellij.common.isSupportFileType
27+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
28+
import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
29+
30+
/**
31+
* Intention action to convert SQL file to @Sql annotation
32+
*/
33+
class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
34+
override fun getFamilyName(): String = MessageBundle.message("convert.sql.file.to.annotation.family")
35+
36+
override fun getText(): String = MessageBundle.message("convert.sql.file.to.annotation.text")
37+
38+
override fun isAvailable(
39+
project: Project,
40+
editor: Editor?,
41+
element: PsiElement,
42+
): Boolean {
43+
if (!isSupportFileType(element.containingFile)) return false
44+
45+
val daoMethod = findDaoMethod(element.containingFile) ?: return false
46+
val psiDaoMethod = PsiDaoMethod(project, daoMethod)
47+
48+
// Check if method doesn't have @Sql annotation
49+
if (psiDaoMethod.useSqlAnnotation()) {
50+
return false
51+
}
52+
53+
// Check if method has @Insert, @Update, or @Delete annotation with sqlFile=true
54+
val supportedTypes =
55+
listOf(
56+
DomaAnnotationType.Select,
57+
DomaAnnotationType.Insert,
58+
DomaAnnotationType.Update,
59+
DomaAnnotationType.Delete,
60+
DomaAnnotationType.BatchInsert,
61+
DomaAnnotationType.BatchUpdate,
62+
DomaAnnotationType.BatchDelete,
63+
)
64+
65+
val hasAnnotation =
66+
supportedTypes.any { type ->
67+
val annotation = type.getPsiAnnotation(daoMethod)
68+
annotation != null
69+
}
70+
71+
// Must have annotation with sqlFile=true and SQL file must exist
72+
return hasAnnotation && psiDaoMethod.sqlFile != null
73+
}
74+
75+
override fun invoke(
76+
project: Project,
77+
editor: Editor?,
78+
element: PsiElement,
79+
) {
80+
// Do nothing when previewing
81+
if (IntentionPreviewUtils.isIntentionPreviewActive()) return
82+
if (!isSupportFileType(element.containingFile)) return
83+
84+
val daoMethod = findDaoMethod(element.containingFile) ?: return
85+
val converter = SqlAnnotationConverter(project, daoMethod)
86+
WriteCommandAction.runWriteCommandAction(project) {
87+
converter.convertToSqlAnnotation()
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)