Skip to content

Commit

Permalink
feat(biz): reorg code
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 7, 2023
1 parent 6068a35 commit e72683b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
Expand Up @@ -18,6 +18,7 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiMethod

Expand Down Expand Up @@ -47,22 +48,21 @@ class ExplainBusinessAction : AbstractChatIntention() {

val task: Task.Backgroundable = object : Task.Backgroundable(project, "Collect context") {
override fun run(indicator: ProgressIndicator) {
val instruction = actionType.instruction(lang)

// prepare context
val contextItems = when (element) {
is PsiMethod -> {
val javaMethodContextBuilder = JavaMethodContextBuilder()
val methodContext = ReadAction.compute<MethodContext?, Throwable> {
return@compute javaMethodContextBuilder.getMethodContext(
return@compute JavaMethodContextBuilder().getMethodContext(
creationContext.element as PsiMethod,
true,
gatherUsages = true
)
}
// collect usages should run in [ProgressIndicator]
methodContext?.let {
val toQuery = ReadAction.compute<String?, Throwable> { return@compute it.toQuery() }
.lines()
.joinToString("// $it\n")
val toQuery = ReadAction.compute<String?, Throwable> {
return@compute it.toQuery()
}
val contextItem = ChatContextItem(ExplainBusinessAction::class, toQuery)
listOf(contextItem)
} ?: emptyList()
Expand All @@ -73,26 +73,37 @@ class ExplainBusinessAction : AbstractChatIntention() {
}
}

var chatContext = ""
contextItems.forEach {
chatContext += it.text
}

if (chatContext.isEmpty()) {
chatContext = selectedText
} else {
chatContext = "// Compare this snippet $chatContext\norigin code:\n```$lang$selectedText```\n"
}

ApplicationManager.getApplication().invokeLater {
sendToChatWindow(project, getActionType()) { contentPanel, _ ->
contentPanel.setInput("\n$instruction\n```markdown\n$chatContext")
}
}
val instruction = actionType.instruction(lang)
sendToChat(contextItems, selectedText, lang, project, instruction)
}
}

ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
}

private fun sendToChat(
contextItems: List<ChatContextItem>,
selectedText: @NlsSafe String,
lang: @NlsSafe String,
project: Project,
instruction: String,
) {
var chatContext = "\n"
contextItems.forEach {
chatContext += it.text
}

ApplicationManager.getApplication().invokeLater {
if (chatContext.isEmpty()) {
chatContext = selectedText
} else {
chatContext = "// Compare this snippet $chatContext\norigin code:\n```$lang$selectedText```\n"
}

sendToChatWindow(project, getActionType()) { contentPanel, _ ->
contentPanel.setInput("\n$instruction\n```markdown\n$chatContext")
}
}
}
}

@@ -1,4 +1,4 @@
package cc.unitmesh.idea.flow.provider
package cc.unitmesh.idea.flow

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.context.model.DtClass
Expand All @@ -12,8 +12,6 @@ import cc.unitmesh.devti.parser.parseCodeFromString
import cc.unitmesh.devti.provider.DevFlowProvider
import cc.unitmesh.devti.provider.PromptStrategy
import cc.unitmesh.devti.runconfig.AutoDevRunProfileState
import cc.unitmesh.idea.flow.JavaCodeProcessor
import cc.unitmesh.idea.flow.JavaParseUtil
import cc.unitmesh.idea.spring.JavaSpringCodeCreator
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.components.service
Expand Down
@@ -1,4 +1,4 @@
package cc.unitmesh.idea.flow.provider
package cc.unitmesh.idea.promting

import cc.unitmesh.devti.context.chunks.SimilarChunksWithPaths
import cc.unitmesh.devti.gui.chat.ChatActionType
Expand Down
@@ -1,4 +1,4 @@
package cc.unitmesh.idea.flow.provider
package cc.unitmesh.idea.promting

import cc.unitmesh.devti.context.model.DtClass
import cc.unitmesh.devti.provider.PromptStrategy
Expand Down
6 changes: 3 additions & 3 deletions java/src/main/resources/cc.unitmesh.idea.xml
Expand Up @@ -27,13 +27,13 @@

<contextPrompter
language="JAVA"
implementation="cc.unitmesh.idea.flow.provider.JavaContextPrompter"/>
implementation="cc.unitmesh.idea.promting.JavaContextPrompter"/>
<devFlowProvider
language="JAVA"
implementation="cc.unitmesh.idea.flow.provider.JvmAutoDevFlow"/>
implementation="cc.unitmesh.idea.flow.JvmAutoDevFlow"/>
<promptStrategy
language="JAVA"
implementation="cc.unitmesh.idea.flow.provider.JvmPromptStrategy"/>
implementation="cc.unitmesh.idea.promting.JvmPromptStrategy"/>

<testContextProvider
language="JAVA"
Expand Down
@@ -1,6 +1,6 @@
package cc.unitmesh.idea.provider

import cc.unitmesh.idea.flow.provider.JvmAutoDevFlow
import cc.unitmesh.idea.flow.JvmAutoDevFlow
import org.junit.Test

class JvmAutoDevFlowTest {
Expand Down
@@ -1,6 +1,6 @@
package cc.unitmesh.idea.provider

import cc.unitmesh.idea.flow.provider.JvmPromptStrategy
import cc.unitmesh.idea.promting.JvmPromptStrategy
import com.intellij.lang.java.JavaLanguage
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElementFactory
Expand Down
@@ -1,6 +1,6 @@
package cc.unitmesh.kotlin.provider

import cc.unitmesh.idea.flow.provider.JavaContextPrompter
import cc.unitmesh.idea.promting.JavaContextPrompter

class KotlinContextPrompter: JavaContextPrompter() {
}

0 comments on commit e72683b

Please sign in to comment.