Skip to content

Commit

Permalink
feat: add find bug support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 23, 2023
1 parent 07d693a commit f971135
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/actions/FindBugAction.kt
@@ -1,13 +1,56 @@
package cc.unitmesh.devti.actions

import cc.unitmesh.devti.DevtiIcons
import cc.unitmesh.devti.connector.openai.OpenCodeCopilot
import cc.unitmesh.devti.gui.createSuggestionPopup
import cc.unitmesh.devti.runconfig.AutoCRUDState
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.util.NlsSafe
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMethod

class FindBugAction(methodName: @NlsSafe String, method: PsiMethod) : AnAction({ "Find bug for $methodName" }, DevtiIcons.AI_COPILOT) {
class FindBugAction(methodName: @NlsSafe String, val method: PsiMethod) :
AnAction({ "Find bug for $methodName" }, DevtiIcons.AI_COPILOT) {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return

val task = object : Task.Backgroundable(project, "Find bug", true) {
override fun run(indicator: ProgressIndicator) {
indicator.fraction = 0.2
indicator.text = "Preparing code complete prompt"

val apiExecutor = OpenCodeCopilot()

indicator.fraction = 0.5
indicator.text = "Call OpenAI API..."

val suggestion = apiExecutor.findBug(method.text).trimIndent()

indicator.fraction = 0.8
indicator.text = "Start replacing method"

val popup = createSuggestionPopup(suggestion)
ApplicationManager.getApplication().invokeLater() {
popup.showCenteredInCurrentWindow(project)
}

indicator.fraction = 1.0
}
}

ApplicationManager.getApplication().invokeLater {
ProgressManager.getInstance().run(task)
}
}

companion object {
private val log: Logger = logger<AutoCRUDState>()
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/cc/unitmesh/devti/connector/CodeCopilot.kt
Expand Up @@ -5,4 +5,5 @@ interface CodeCopilot {

fun autoComment(text: String): String
fun codeReviewFor(text: String, className: String): String
fun findBug(text: String): String
}
Expand Up @@ -19,4 +19,8 @@ class CustomConnector(val url: String, val key: String) : CodeCopilot {
return ""
}

override fun findBug(text: String): String {
return ""
}

}
Expand Up @@ -131,6 +131,15 @@ class OpenCodeCopilot : CodeCopilot, DevtiFlowAction {
}
}

override fun findBug(text: String): String {
val promptText = promptGenerator.findBug(text)
logger.warn("findBug prompt text: $promptText")
return runBlocking {
val result = prompt(promptText)
return@runBlocking result
}
}

companion object {
private val logger: Logger = logger<OpenCodeCopilot>()
}
Expand Down
Expand Up @@ -56,4 +56,11 @@ class PromptGenerator() {
.replace("{code}", text)
.replace("{className}", className)
}

fun findBug(text: String): String {
val promptText: InputStream = this::class.java.classLoader.getResourceAsStream("prompts/openai/copilot/find_bug.txt")!!
val promptTextString = promptText.bufferedReader().use { it.readText() }
return promptTextString
.replace("{code}", text)
}
}
5 changes: 5 additions & 0 deletions src/main/resources/prompts/openai/copilot/find_bug.txt
@@ -0,0 +1,5 @@
请作为软件质量保证测试员, 分析下面的代码,找出其中是否有潜在的 bug,如果有,说明 bug 的原因,如何修正。

###
{code}
###

0 comments on commit f971135

Please sign in to comment.