Skip to content

Commit 74952dd

Browse files
committed
feat(database): add GenerateFunctionAction for testing PL/SQL
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.
1 parent 9f030fa commit 74952dd

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cc.unitmesh.database.actions
2+
3+
import cc.unitmesh.devti.gui.sendToChatWindow
4+
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
5+
import cc.unitmesh.devti.provider.ContextPrompter
6+
import cc.unitmesh.devti.template.TemplateRender
7+
import com.intellij.openapi.diagnostic.logger
8+
import com.intellij.openapi.editor.Editor
9+
import com.intellij.openapi.project.Project
10+
import com.intellij.psi.PsiFile
11+
import com.intellij.sql.dialects.oracle.OraDialect
12+
13+
class GenerateFunctionAction : AbstractChatIntention() {
14+
override fun priority() = 901
15+
16+
override fun getFamilyName(): String = "Generate Java Function"
17+
18+
override fun getText(): String = "Generate Java Function"
19+
20+
val logger = logger<GenerateFunctionAction>()
21+
22+
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
23+
if (editor == null || file == null) return false
24+
val isOracle = file.language is OraDialect
25+
val selectedText = editor.selectionModel
26+
val element = file.findElementAt(selectedText.selectionStart)
27+
28+
if (element != null) {
29+
logger.info("element: ${element.text}")
30+
}
31+
32+
return isOracle
33+
}
34+
35+
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
36+
if (editor == null || file == null) return
37+
val selectedText = editor.selectionModel.selectedText ?: return
38+
39+
val templateRender = TemplateRender("genius/migration")
40+
val template = templateRender.getTemplate("gen-function.vm")
41+
templateRender.context = GenFunctionContext(
42+
lang = file.language.displayName ?: "",
43+
sql = selectedText,
44+
)
45+
46+
val prompter = templateRender.renderTemplate(template)
47+
48+
logger.info("Prompt: $prompter")
49+
50+
sendToChatWindow(project, getActionType()) { panel, service ->
51+
service.handlePromptAndResponse(panel, object : ContextPrompter() {
52+
override fun displayPrompt(): String = prompter
53+
override fun requestPrompt(): String = prompter
54+
}, null, false)
55+
}
56+
}
57+
58+
}
59+
60+
data class GenFunctionContext(val lang: String, val sql: String)
61+

exts/database/src/main/resources/cc.unitmesh.database.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<bundleName>messages.AutoDevBundle</bundleName>
1111
<categoryKey>intention.category.llm</categoryKey>
1212
</autoDevIntention>
13+
<autoDevIntention>
14+
<className>cc.unitmesh.database.actions.GenerateFunctionAction</className>
15+
<bundleName>messages.AutoDevBundle</bundleName>
16+
<categoryKey>intention.category.llm</categoryKey>
17+
</autoDevIntention>
1318

1419
<livingDocumentation
1520
language="SQL"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
You are a professional application migration programmer.
2+
Based on the following Oracle PL/SQL code to Java function,
3+
4+
— When some function is missing in Java, just skip it.
5+
- If you find some function is not correct, please fix it.
6+
- Follow the Java coding style.
7+
8+
9+
```${context.lang}
10+
${context.sql}
11+
```
12+
13+

0 commit comments

Comments
 (0)