Skip to content

Commit 6039c86

Browse files
committed
Enable invoking the “convert to Sql annotation” action from the method side.
1 parent eaf8778 commit 6039c86

File tree

4 files changed

+115
-39
lines changed

4 files changed

+115
-39
lines changed

src/main/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlAnnotationToFileAction.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.domaframework.doma.intellij.action.dao
1717

18-
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
1918
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
2019
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
2120
import com.intellij.openapi.command.WriteCommandAction
@@ -28,12 +27,11 @@ import com.intellij.psi.util.PsiTreeUtil
2827
import org.domaframework.doma.intellij.bundle.MessageBundle
2928
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
3029
import org.domaframework.doma.intellij.common.util.PluginLoggerUtil
31-
import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
3230

3331
/**
3432
* Intention action to convert @Sql annotation to SQL file
3533
*/
36-
class ConvertSqlAnnotationToFileAction : PsiElementBaseIntentionAction() {
34+
class ConvertSqlAnnotationToFileAction : ConvertSqlIntentionAction() {
3735
override fun getFamilyName(): String = MessageBundle.message("convert.sql.annotation.to.file.family")
3836

3937
override fun getText(): String = MessageBundle.message("convert.sql.annotation.to.file.text")
@@ -47,24 +45,12 @@ class ConvertSqlAnnotationToFileAction : PsiElementBaseIntentionAction() {
4745
val psiDaoMethod = PsiDaoMethod(project, method)
4846

4947
// Check if method has @Sql annotation
50-
if (!psiDaoMethod.useSqlAnnotation()) {
48+
// When a Sql annotation is present, a virtual SQL file is associated;
49+
// therefore, check the parent and exclude the injected (inline) SQL.
50+
if (!psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFile != null && psiDaoMethod.sqlFile?.parent != null) {
5151
return false
5252
}
5353

54-
// Check if method has @Insert, @Update, or @Delete annotation
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-
6854
return supportedTypes.any { it.getPsiAnnotation(method) != null }
6955
}
7056

src/main/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlFileToAnnotationAction.kt

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@
1515
*/
1616
package org.domaframework.doma.intellij.action.dao
1717

18-
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
1918
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
2019
import com.intellij.openapi.command.WriteCommandAction
2120
import com.intellij.openapi.editor.Editor
2221
import com.intellij.openapi.project.Project
2322
import com.intellij.psi.PsiElement
23+
import com.intellij.psi.PsiMethod
24+
import com.intellij.psi.util.PsiTreeUtil
2425
import org.domaframework.doma.intellij.bundle.MessageBundle
2526
import org.domaframework.doma.intellij.common.dao.findDaoMethod
27+
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2628
import org.domaframework.doma.intellij.common.isSupportFileType
2729
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2830
import 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
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
20+
21+
abstract class ConvertSqlIntentionAction : PsiElementBaseIntentionAction() {
22+
protected val supportedTypes =
23+
setOf(
24+
DomaAnnotationType.Select,
25+
DomaAnnotationType.Script,
26+
DomaAnnotationType.SqlProcessor,
27+
DomaAnnotationType.Insert,
28+
DomaAnnotationType.Update,
29+
DomaAnnotationType.Delete,
30+
DomaAnnotationType.BatchInsert,
31+
DomaAnnotationType.BatchUpdate,
32+
DomaAnnotationType.BatchDelete,
33+
)
34+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
<category>Doma tools</category>
107107
<skipBeforeAfter>true</skipBeforeAfter>
108108
</intentionAction>
109+
<intentionAction>
110+
<language>JAVA</language>
111+
<className>org.domaframework.doma.intellij.action.dao.ConvertSqlFileToAnnotationAction</className>
112+
<category>Doma tools</category>
113+
<skipBeforeAfter>true</skipBeforeAfter>
114+
</intentionAction>
109115
</extensions>
110116

111117
<extensions defaultExtensionNs="org.intellij.intelliLang">

0 commit comments

Comments
 (0)