1515 */
1616package org.domaframework.doma.intellij.action.dao
1717
18- import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
1918import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
2019import com.intellij.openapi.command.WriteCommandAction
2120import com.intellij.openapi.editor.Editor
2221import com.intellij.openapi.project.Project
2322import com.intellij.psi.PsiElement
23+ import com.intellij.psi.PsiMethod
24+ import com.intellij.psi.util.PsiTreeUtil
2425import org.domaframework.doma.intellij.bundle.MessageBundle
2526import org.domaframework.doma.intellij.common.dao.findDaoMethod
27+ import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2628import org.domaframework.doma.intellij.common.isSupportFileType
2729import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2830import org.domaframework.doma.intellij.common.util.PluginLoggerUtil
29- import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
3031
3132/* *
3233 * Intention action to convert SQL file to @Sql annotation
3334 */
34- class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction () {
35+ class ConvertSqlFileToAnnotationAction : ConvertSqlIntentionAction () {
3536 override fun getFamilyName (): String = MessageBundle .message(" convert.sql.file.to.annotation.family" )
3637
3738 override fun getText (): String = MessageBundle .message(" convert.sql.file.to.annotation.text" )
@@ -41,30 +42,45 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
4142 editor : Editor ? ,
4243 element : PsiElement ,
4344 ): Boolean {
44- if (! isSupportFileType(element.containingFile)) return false
45+ val file = element.containingFile
46+ if (isJavaOrKotlinFileType(file)) {
47+ return checkOnMethod(element, project)
48+ }
49+
50+ if (isSupportFileType(file)) {
51+ return checkOnSqlFile(element, project)
52+ }
4553
54+ return false
55+ }
56+
57+ private fun checkOnMethod (
58+ element : PsiElement ,
59+ project : Project ,
60+ ): Boolean {
61+ val daoMethod = PsiTreeUtil .getParentOfType(element, PsiMethod ::class .java) ? : return false
62+ return checkAvailable(project, daoMethod)
63+ }
64+
65+ private fun checkOnSqlFile (
66+ element : PsiElement ,
67+ project : Project ,
68+ ): Boolean {
4669 val daoMethod = findDaoMethod(element.containingFile) ? : return false
70+ return checkAvailable(project, daoMethod)
71+ }
72+
73+ private fun checkAvailable (
74+ project : Project ,
75+ daoMethod : PsiMethod ,
76+ ): Boolean {
4777 val psiDaoMethod = PsiDaoMethod (project, daoMethod)
4878
4979 // Check if method doesn't have @Sql annotation
50- if (psiDaoMethod.useSqlAnnotation()) {
80+ if (psiDaoMethod.sqlFile == null || psiDaoMethod. useSqlAnnotation()) {
5181 return false
5282 }
5383
54- // Check if method has @Insert, @Update, or @Delete annotation with sqlFile=true
55- val supportedTypes =
56- listOf (
57- DomaAnnotationType .Select ,
58- DomaAnnotationType .Script ,
59- DomaAnnotationType .SqlProcessor ,
60- DomaAnnotationType .Insert ,
61- DomaAnnotationType .Update ,
62- DomaAnnotationType .Delete ,
63- DomaAnnotationType .BatchInsert ,
64- DomaAnnotationType .BatchUpdate ,
65- DomaAnnotationType .BatchDelete ,
66- )
67-
6884 val hasAnnotation =
6985 supportedTypes.any { type ->
7086 val annotation = type.getPsiAnnotation(daoMethod)
@@ -82,8 +98,42 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
8298 ) {
8399 // Do nothing when previewing
84100 if (IntentionPreviewUtils .isIntentionPreviewActive()) return
85- if (! isSupportFileType(element.containingFile)) return
86101
102+ val file = element.containingFile
103+ if (isJavaOrKotlinFileType(file)) {
104+ return processOnMethod(element, project)
105+ }
106+
107+ // Process if the file type is SQL
108+ if (isSupportFileType(file)) {
109+ return processOnSqlFile(element, project)
110+ }
111+ }
112+
113+ private fun processOnMethod (
114+ element : PsiElement ,
115+ project : Project ,
116+ ) {
117+ val daoMethod = PsiTreeUtil .getParentOfType(element, PsiMethod ::class .java) ? : return
118+
119+ val startTime = System .nanoTime()
120+ val converter = SqlAnnotationConverter (project, daoMethod)
121+ WriteCommandAction .runWriteCommandAction(project) {
122+ converter.convertToSqlAnnotation()
123+ }
124+
125+ PluginLoggerUtil .countLogging(
126+ className = this ::class .java.simpleName,
127+ actionName = " convertSqlFileToAnnotationOnMethod" ,
128+ inputName = " IntentionAction" ,
129+ start = startTime,
130+ )
131+ }
132+
133+ private fun processOnSqlFile (
134+ element : PsiElement ,
135+ project : Project ,
136+ ) {
87137 val daoMethod = findDaoMethod(element.containingFile) ? : return
88138
89139 val startTime = System .nanoTime()
@@ -94,7 +144,7 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
94144
95145 PluginLoggerUtil .countLogging(
96146 className = this ::class .java.simpleName,
97- actionName = " convertSqlFileToAnnotation " ,
147+ actionName = " convertSqlFileToAnnotationOnSQL " ,
98148 inputName = " IntentionAction" ,
99149 start = startTime,
100150 )
0 commit comments