Skip to content

Commit 2b4dc3a

Browse files
committed
Add support for additional Doma annotation types in SQL conversion actions
1 parent 7e7d594 commit 2b4dc3a

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class ConvertSqlAnnotationToFileAction : PsiElementBaseIntentionAction() {
5454
val supportedTypes =
5555
listOf(
5656
DomaAnnotationType.Select,
57+
DomaAnnotationType.Script,
58+
DomaAnnotationType.SqlProcessor,
5759
DomaAnnotationType.Insert,
5860
DomaAnnotationType.Update,
5961
DomaAnnotationType.Delete,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
5454
val supportedTypes =
5555
listOf(
5656
DomaAnnotationType.Select,
57+
DomaAnnotationType.Script,
58+
DomaAnnotationType.SqlProcessor,
5759
DomaAnnotationType.Insert,
5860
DomaAnnotationType.Update,
5961
DomaAnnotationType.Delete,

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.intellij.psi.PsiLiteralExpression
2929
import com.intellij.psi.PsiMethod
3030
import com.intellij.psi.PsiNameValuePair
3131
import com.intellij.psi.codeStyle.JavaCodeStyleManager
32+
import org.domaframework.doma.intellij.common.dao.jumpToDaoMethod
3233
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
3334
import org.domaframework.doma.intellij.common.util.InjectionSqlUtil
3435
import org.domaframework.doma.intellij.common.util.StringUtil
@@ -97,6 +98,8 @@ class SqlAnnotationConverter(
9798
val supportedTypes =
9899
listOf(
99100
DomaAnnotationType.Select,
101+
DomaAnnotationType.Script,
102+
DomaAnnotationType.SqlProcessor,
100103
DomaAnnotationType.Insert,
101104
DomaAnnotationType.Update,
102105
DomaAnnotationType.Delete,
@@ -119,8 +122,6 @@ class SqlAnnotationConverter(
119122
annotation: PsiAnnotation,
120123
value: Boolean,
121124
) {
122-
// Add new attribute
123-
val attributeText = "sqlFile = $value"
124125
val useSqlFileOptionAnnotationList =
125126
listOf(
126127
DomaAnnotationType.Insert,
@@ -130,23 +131,33 @@ class SqlAnnotationConverter(
130131
DomaAnnotationType.BatchUpdate,
131132
DomaAnnotationType.BatchDelete,
132133
)
133-
val newAttribute =
134-
if (useSqlFileOptionAnnotationList.contains(psiDaoMethod.daoType)) {
134+
135+
if (useSqlFileOptionAnnotationList.contains(psiDaoMethod.daoType)) {
136+
val existingAttribute =
137+
annotation.parameterList.attributes
138+
.find { it.name == "sqlFile" }
139+
140+
if (value) {
141+
// Add or update sqlFile = true
142+
val attributeText = "sqlFile = true"
135143
val dummyAnnotation =
136144
elementFactory.createAnnotationFromText(
137145
"@Dummy($attributeText)",
138146
null,
139147
)
140-
dummyAnnotation.parameterList.attributes[0]
148+
val newAttribute = dummyAnnotation.parameterList.attributes[0]
149+
150+
if (existingAttribute != null) {
151+
existingAttribute.replace(newAttribute)
152+
} else {
153+
annotation.parameterList.add(newAttribute)
154+
}
141155
} else {
142-
null
156+
// Remove sqlFile parameter when value is false
157+
existingAttribute?.delete()
143158
}
144-
if (newAttribute != null) {
145-
annotation.parameterList.attributes
146-
.find { it.name == "sqlFile" }
147-
?.replace(newAttribute)
148-
?: annotation.parameterList.add(newAttribute)
149159
}
160+
150161
val psiFile = annotation.containingFile
151162
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile)
152163
if (document != null) {
@@ -183,6 +194,7 @@ class SqlAnnotationConverter(
183194
}
184195
// Add import if needed
185196
val containingFile = method.containingFile
197+
// TODO Support Kotlin files in the future
186198
if (containingFile is PsiJavaFile) {
187199
val importList = containingFile.importList
188200
val sqlImport = DomaAnnotationType.Sql.fqdn
@@ -201,8 +213,21 @@ class SqlAnnotationConverter(
201213
) ?: return,
202214
)
203215
importList?.add(importStatement)
216+
val psiFile = method.containingFile
217+
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile)
218+
if (document != null) {
219+
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document)
220+
}
204221
}
205222
}
223+
224+
// Jump to method
225+
val newDaoFile = method.containingFile
226+
val newDocument = PsiDocumentManager.getInstance(project).getDocument(newDaoFile)
227+
if (newDocument != null) {
228+
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(newDocument)
229+
}
230+
jumpToDaoMethod(project, psiDaoMethod.sqlFile?.name ?: return, newDaoFile.virtualFile)
206231
}
207232

208233
private fun generateSqlFileWithContent(content: String) {

0 commit comments

Comments
 (0)