Skip to content

Commit

Permalink
feat(database): add GenerateFunctionAction for testing PL/SQL
Browse files Browse the repository at this point in the history
Add a new class `GenerateFunctionAction` to the database module. This class is responsible for generating Java functions based on Oracle PL/SQL code. It includes methods for checking if the action is available, invoking the action, and rendering the template for generating the function. The class also includes a data class `GenFunctionContext` to hold the language and SQL code for the function.
  • Loading branch information
phodal committed Jan 21, 2024
1 parent 9f030fa commit 74952dd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
@@ -0,0 +1,61 @@
package cc.unitmesh.database.actions

import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.sql.dialects.oracle.OraDialect

class GenerateFunctionAction : AbstractChatIntention() {
override fun priority() = 901

override fun getFamilyName(): String = "Generate Java Function"

override fun getText(): String = "Generate Java Function"

val logger = logger<GenerateFunctionAction>()

override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
if (editor == null || file == null) return false
val isOracle = file.language is OraDialect
val selectedText = editor.selectionModel
val element = file.findElementAt(selectedText.selectionStart)

if (element != null) {
logger.info("element: ${element.text}")
}

return isOracle
}

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null) return
val selectedText = editor.selectionModel.selectedText ?: return

val templateRender = TemplateRender("genius/migration")
val template = templateRender.getTemplate("gen-function.vm")
templateRender.context = GenFunctionContext(
lang = file.language.displayName ?: "",
sql = selectedText,
)

val prompter = templateRender.renderTemplate(template)

logger.info("Prompt: $prompter")

sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompter
override fun requestPrompt(): String = prompter
}, null, false)
}
}

}

data class GenFunctionContext(val lang: String, val sql: String)

5 changes: 5 additions & 0 deletions exts/database/src/main/resources/cc.unitmesh.database.xml
Expand Up @@ -10,6 +10,11 @@
<bundleName>messages.AutoDevBundle</bundleName>
<categoryKey>intention.category.llm</categoryKey>
</autoDevIntention>
<autoDevIntention>
<className>cc.unitmesh.database.actions.GenerateFunctionAction</className>
<bundleName>messages.AutoDevBundle</bundleName>
<categoryKey>intention.category.llm</categoryKey>
</autoDevIntention>

<livingDocumentation
language="SQL"
Expand Down
13 changes: 13 additions & 0 deletions exts/database/src/main/resources/genius/migration/gen-function.vm
@@ -0,0 +1,13 @@
You are a professional application migration programmer.
Based on the following Oracle PL/SQL code to Java function,

— When some function is missing in Java, just skip it.
- If you find some function is not correct, please fix it.
- Follow the Java coding style.


```${context.lang}
${context.sql}
```


0 comments on commit 74952dd

Please sign in to comment.