Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,40 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiTreeUtil
import org.domaframework.doma.intellij.bundle.MessageBundle
import org.domaframework.doma.intellij.common.dao.findDaoMethod
import org.domaframework.doma.intellij.common.dao.getDaoClass
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
import org.domaframework.doma.intellij.common.isSupportFileType
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
import org.domaframework.doma.intellij.common.util.PluginLoggerUtil

/**
* Intention action to convert SQL file to @Sql annotation
* Abstract base class for converting SQL file to @Sql annotation
*/
class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
override fun getFamilyName(): String = MessageBundle.message("convert.sql.file.to.annotation.family")

override fun getText(): String = MessageBundle.message("convert.sql.file.to.annotation.text")

abstract class AbstractConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
override fun isAvailable(
project: Project,
editor: Editor?,
element: PsiElement,
): Boolean {
val file = element.containingFile ?: return false
if (isJavaOrKotlinFileType(file) && getDaoClass(file) != null) {
return checkOnMethod(element, project)
}

if (isSupportFileType(file)) {
return checkOnSqlFile(element, project)
if (!isTargetFile(element)) {
return false
}

return false
}

private fun checkOnMethod(
element: PsiElement,
project: Project,
): Boolean {
val daoMethod = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return false
val daoMethod = getDaoMethod(element) ?: return false
return checkAvailable(project, daoMethod)
}

private fun checkOnSqlFile(
element: PsiElement,
project: Project,
): Boolean {
val daoMethod = findDaoMethod(element.containingFile) ?: return false
return checkAvailable(project, daoMethod)
}
/**
* Check if the element is in a target file type for this action
*/
protected abstract fun isTargetFile(element: PsiElement): Boolean

/**
* Get the DAO method from the element
*/
protected abstract fun getDaoMethod(element: PsiElement): PsiMethod?

/**
* Get the action name for logging
*/
protected abstract fun getActionName(): String

private fun checkAvailable(
project: Project,
Expand Down Expand Up @@ -101,42 +86,7 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {
// Do nothing when previewing
if (IntentionPreviewUtils.isIntentionPreviewActive()) return

val file = element.containingFile
if (isJavaOrKotlinFileType(file)) {
return processOnMethod(element, project)
}

// Process if the file type is SQL
if (isSupportFileType(file)) {
return processOnSqlFile(element, project)
}
}

private fun processOnMethod(
element: PsiElement,
project: Project,
) {
val daoMethod = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return

val startTime = System.nanoTime()
val converter = SqlAnnotationConverter(project, daoMethod)
WriteCommandAction.runWriteCommandAction(project) {
converter.convertToSqlAnnotation()
}

PluginLoggerUtil.countLogging(
className = this::class.java.simpleName,
actionName = "convertSqlFileToAnnotationOnMethod",
inputName = "IntentionAction",
start = startTime,
)
}

private fun processOnSqlFile(
element: PsiElement,
project: Project,
) {
val daoMethod = findDaoMethod(element.containingFile) ?: return
val daoMethod = getDaoMethod(element) ?: return

val startTime = System.nanoTime()
val converter = SqlAnnotationConverter(project, daoMethod)
Expand All @@ -146,7 +96,7 @@ class ConvertSqlFileToAnnotationAction : PsiElementBaseIntentionAction() {

PluginLoggerUtil.countLogging(
className = this::class.java.simpleName,
actionName = "convertSqlFileToAnnotationOnSQL",
actionName = getActionName(),
inputName = "IntentionAction",
start = startTime,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Doma Tools Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.action.dao

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiTreeUtil
import org.domaframework.doma.intellij.bundle.MessageBundle
import org.domaframework.doma.intellij.common.dao.getDaoClass
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType

/**
* Intention action to convert SQL file to @Sql annotation from DAO files
*/
class ConvertSqlFileToAnnotationFromDaoAction : AbstractConvertSqlFileToAnnotationAction() {
override fun getFamilyName(): String = MessageBundle.message("convert.sql.file.to.annotation.from.dao.family")

override fun getText(): String = MessageBundle.message("convert.sql.file.to.annotation.from.dao.text")

override fun isTargetFile(element: PsiElement): Boolean {
val file = element.containingFile ?: return false
return isJavaOrKotlinFileType(file) && getDaoClass(file) != null
}

override fun getDaoMethod(element: PsiElement): PsiMethod? = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java)

override fun getActionName(): String = "convertSqlFileToAnnotationFromDao"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Doma Tools Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.domaframework.doma.intellij.action.sql

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import org.domaframework.doma.intellij.action.dao.AbstractConvertSqlFileToAnnotationAction
import org.domaframework.doma.intellij.bundle.MessageBundle
import org.domaframework.doma.intellij.common.dao.findDaoMethod
import org.domaframework.doma.intellij.common.isSupportFileType

/**
* Intention action to convert SQL file to @Sql annotation from SQL files
*/
class ConvertSqlFileToAnnotationFromSqlAction : AbstractConvertSqlFileToAnnotationAction() {
override fun getFamilyName(): String = MessageBundle.message("convert.sql.file.to.annotation.from.sql.family")

override fun getText(): String = MessageBundle.message("convert.sql.file.to.annotation.from.sql.text")

override fun isTargetFile(element: PsiElement): Boolean {
val file = element.containingFile ?: return false
return isSupportFileType(file)
}

override fun getDaoMethod(element: PsiElement): PsiMethod? = findDaoMethod(element.containingFile)

override fun getActionName(): String = "convertSqlFileToAnnotationFromSql"
}
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@
</intentionAction>
<intentionAction>
<language>DomaSql</language>
<className>org.domaframework.doma.intellij.action.dao.ConvertSqlFileToAnnotationAction</className>
<className>org.domaframework.doma.intellij.action.sql.ConvertSqlFileToAnnotationFromSqlAction</className>
<category>Doma tools</category>
<skipBeforeAfter>true</skipBeforeAfter>
</intentionAction>
<intentionAction>
<language>JAVA</language>
<className>org.domaframework.doma.intellij.action.dao.ConvertSqlFileToAnnotationAction</className>
<className>org.domaframework.doma.intellij.action.dao.ConvertSqlFileToAnnotationFromDaoAction</className>
<category>Doma tools</category>
<skipBeforeAfter>true</skipBeforeAfter>
</intentionAction>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html lang="en">
<body>
<p>
This action converts SQL statements from SQL files into Sql annotations for DAO methods.<br>
By selecting an DAO method and executing the action, the corresponding SQL will be embedded as an annotation in the appropriate DAO method.<br>
This makes it easier to manage SQL directly within the source code and improves code readability and maintainability.
</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html lang="en">
<body>
<p>
This action converts SQL statements from SQL files into Sql annotations for DAO methods.<br>
By selecting an SQL file and executing the action, the corresponding SQL will be embedded as an annotation in the appropriate DAO method.<br>
This makes it easier to manage SQL directly within the source code and improves code readability and maintainability.
</p>
</body>
</html>
6 changes: 4 additions & 2 deletions src/main/resources/messages/DomaToolsBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ inspection.invalid.dao.annotation.option.embeddable=Field [{0}] specified in [{1
inspection.invalid.dao.annotation.option.primitive=Field path [{0}] specified in [{2}] option is invalid. Field [{1}] is a primitive type and does not have nested properties
convert.sql.annotation.to.file.family=Convert @Sql annotation to SQL file
convert.sql.annotation.to.file.text=Convert to SQL file (set sqlFile=true)
convert.sql.file.to.annotation.family=Convert SQL file to @Sql annotation
convert.sql.file.to.annotation.text=Convert to @Sql annotation (set sqlFile=false)
convert.sql.file.to.annotation.from.dao.family=Convert SQL file to @Sql annotation
convert.sql.file.to.annotation.from.dao.text=Convert SQL file to @Sql annotation
convert.sql.file.to.annotation.from.sql.family=Convert to @Sql annotation
convert.sql.file.to.annotation.from.sql.text=Convert to @Sql annotation in DAO method
inspection.invalid.sql.function.call.parameter.count=Function {0} definition with {1} parameters not found
inspection.invalid.sql.function.call.parameter.type.mismatch=<html>Function {0} parameter type mismatch. Actual types: ({1})<br><small>Definition candidates:</small><br>{2}</html>
6 changes: 4 additions & 2 deletions src/main/resources/messages/DomaToolsBundle_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ inspection.invalid.dao.annotation.option.embeddable=[{1}]\u30AA\u30D7\u30B7\u30E
inspection.invalid.dao.annotation.option.primitive=[{2}]\u30AA\u30D7\u30B7\u30E7\u30F3\u3067\u6307\u5B9A\u3055\u308C\u305F\u30D5\u30A3\u30FC\u30EB\u30C9\u30D1\u30B9[{0}]\u306F\u7121\u52B9\u3067\u3059\u3002\u30D5\u30A3\u30FC\u30EB\u30C9[{1}]\u306F\u30D7\u30EA\u30DF\u30C6\u30A3\u30D6\u578B\u3067\u3042\u308A\u3001\u30CD\u30B9\u30C8\u3055\u308C\u305F\u30D7\u30ED\u30D1\u30C6\u30A3\u3092\u6301\u3061\u307E\u305B\u3093
convert.sql.annotation.to.file.family=@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u3092SQL\u30D5\u30A1\u30A4\u30EB\u306B\u5909\u63DB
convert.sql.annotation.to.file.text=SQL\u30D5\u30A1\u30A4\u30EB\u306B\u5909\u63DB (sqlFile=true\u306B\u8A2D\u5B9A)
convert.sql.file.to.annotation.family=SQL\u30D5\u30A1\u30A4\u30EB\u3092@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB
convert.sql.file.to.annotation.text=@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB (sqlFile=false\u306B\u8A2D\u5B9A)
convert.sql.file.to.annotation.from.dao.family=SQL\u30D5\u30A1\u30A4\u30EB\u3092@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB
convert.sql.file.to.annotation.from.dao.text=@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB (sqlFile=false\u306B\u8A2D\u5B9A)
convert.sql.file.to.annotation.from.sql.family=SQL\u30D5\u30A1\u30A4\u30EB\u3092@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB
convert.sql.file.to.annotation.from.sql.text=@Sql\u30A2\u30CE\u30C6\u30FC\u30B7\u30E7\u30F3\u306B\u5909\u63DB (sqlFile=false\u306B\u8A2D\u5B9A)
inspection.invalid.sql.function.call.parameter.count=\u95a2\u6570{0}\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u6570\u304c{1}\u306e\u5b9a\u7fa9\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093
inspection.invalid.sql.function.call.parameter.type.mismatch=<html>\u95a2\u6570{0}\u306e\u5f15\u6570\u306e\u578b\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002\u5b9f\u969b\u306e\u578b: ({1})<br><small>\u5b9a\u7fa9\u5019\u88dc:</small><br>{2}</html>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.psi.PsiDocumentManager
import org.domaframework.doma.intellij.DomaSqlTest
import kotlin.reflect.KClass

abstract class ConvertSqlActionTest : DomaSqlTest() {
protected fun doConvertAction(
Expand All @@ -45,17 +46,18 @@ abstract class ConvertSqlActionTest : DomaSqlTest() {
myFixture.checkResultByFile("java/doma/example/dao/$sqlConversionPackage/$daoName.after.java")
}

protected fun doConvertActionTest(
protected fun <T : AbstractConvertSqlFileToAnnotationAction> doConvertActionTest(
daoName: String,
sqlToAnnotationPackage: String,
convertFamilyName: String,
convertActionClass: KClass<T>,
) {
addDaoJavaFile("$sqlToAnnotationPackage/$daoName.java")
val daoClass = findDaoClass("$sqlToAnnotationPackage.$daoName")
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)

val intentions = myFixture.availableIntentions
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationAction }
val convertIntention = intentions.find { convertActionClass.java.isInstance(it) }

assertNull("$convertFamilyName intention should NOT be available without @Sql annotation", convertIntention)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ class ConvertSqlAnnotationToFileActionTest : ConvertSqlActionTest() {

fun testIntentionNotAvailableForMethodWithoutSqlAnnotation() {
val daoName = "NoSqlAnnotationDao"
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName)
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName, ConvertSqlFileToAnnotationFromDaoAction::class)
}

fun testIntentionNotAvailableForUnsupportedAnnotation() {
val daoName = "UnsupportedAnnotationDao"
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName)
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName, ConvertSqlFileToAnnotationFromDaoAction::class)
}

private fun doTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.action.dao

import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.psi.PsiDocumentManager
import org.domaframework.doma.intellij.action.sql.ConvertSqlFileToAnnotationFromSqlAction

class ConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
private val sqlToAnnotationPackage = "sqltoannotation"
Expand Down Expand Up @@ -84,7 +85,7 @@ class ConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)

val intentions = myFixture.availableIntentions
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationAction }
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationFromSqlAction }

assertNull(
"$convertFamilyName intention should NOT be available when @Sql annotation already exists",
Expand All @@ -99,7 +100,7 @@ class ConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)

val intentions = myFixture.availableIntentions
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationAction }
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationFromSqlAction }

assertNull("$convertFamilyName intention should NOT be available when SQL file doesn't exist", convertIntention)
}
Expand All @@ -113,7 +114,7 @@ class ConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
fun testSelectWithSqlFileConvertAnnotation() {
val daoName = "SelectWithSqlFileConvertAnnotationDao"
val sqlFileName = "selectEmployee.sql"
doConvertActionTest(daoName, sqlToAnnotationPackage, convertFamilyName)
doConvertActionTest(daoName, sqlToAnnotationPackage, convertFamilyName, ConvertSqlFileToAnnotationFromDaoAction::class)
// Test SQL File Removed
val generatedSql = findSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName")
assertTrue("SQL File [$sqlFileName] should exists ", generatedSql == null)
Expand Down
Loading