Skip to content

Commit

Permalink
feat: add chat with this
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 31, 2023
1 parent 48e1f26 commit ed44a9d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
@@ -0,0 +1,42 @@
package cc.unitmesh.devti.actions.chat

import cc.unitmesh.devti.gui.DevtiFlowToolWindowFactory
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.gui.chat.ChatCodingComponent
import cc.unitmesh.devti.gui.chat.ChatCodingService
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.wm.ToolWindowManager

class ChatWithThisAction: ChatBaseAction() {
override fun getActionType(): ChatActionType {
return ChatActionType.CHAT
}

override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val document = event.getData(CommonDataKeys.EDITOR)?.document

val caretModel = event.getData(CommonDataKeys.EDITOR)?.caretModel
var prefixText = caretModel?.currentCaret?.selectedText ?: ""

val toolWindowManager = ToolWindowManager.getInstance(project).getToolWindow(DevtiFlowToolWindowFactory.Util.id)
val contentManager = toolWindowManager?.contentManager

val chatCodingService = ChatCodingService(getActionType(), project)
val contentPanel = ChatCodingComponent(chatCodingService)
val content = contentManager?.factory?.createContent(contentPanel, "Chat with this", false)
val language = event.getData(CommonDataKeys.PSI_FILE)?.language?.displayName ?: ""

contentManager?.removeAllContents(true)
contentManager?.addContent(content!!)

toolWindowManager?.activate {
contentPanel.setContent("""
|```$language
|$prefixText
|```""".trimMargin())
}

}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.ui.NullableComponent
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.ui.Gray
import com.intellij.ui.JBColor
import com.intellij.ui.OnePixelSplitter
Expand Down Expand Up @@ -41,6 +42,7 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
myList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
)
private var inputField: AutoDevInputField = AutoDevInputField(chatCodingService.project, listOf())

init {
val splitter = OnePixelSplitter(true, .98f)
Expand Down Expand Up @@ -148,6 +150,8 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
listOf()
)

this.inputField = searchTextArea

val listener: (ActionEvent) -> Unit = {
val prompt = searchTextArea.text
searchTextArea.text = ""
Expand Down Expand Up @@ -188,4 +192,10 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB

mainPanel.add(actionPanel, BorderLayout.SOUTH)
}

fun setContent(trimMargin: String) {
val focusManager = IdeFocusManager.getInstance(chatCodingService.project)
focusManager.requestFocus(inputField, true)
this.inputField.text = trimMargin
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/autodev-core.xml
Expand Up @@ -151,6 +151,11 @@
description="Ask AutoDev about this code">
</action>

<action id="cc.unitmesh.devti.actions.chat.ChatWithThisAction"
class="cc.unitmesh.devti.actions.chat.ChatWithThisAction" text="Chat with This"
description="Chat with this code">
</action>

<action id="cc.unitmesh.devti.actions.chat.WriteTestAction"
class="cc.unitmesh.devti.actions.chat.WriteTestAction" text="Write Test for This"
description="Ask AutoDev about this code">
Expand Down

0 comments on commit ed44a9d

Please sign in to comment.