|
| 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