From 51342a26c9ffaca625e5d8e18343cefd3f7c0083 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 29 Jul 2023 23:24:02 +0800 Subject: [PATCH] feat(ui): etract for new method --- ...{LLMInputField.kt => AutoDevInputField.kt} | 54 +++++++++++-------- .../devti/gui/chat/ChatCodingComponent.kt | 13 +++-- 2 files changed, 41 insertions(+), 26 deletions(-) rename src/main/kotlin/cc/unitmesh/devti/gui/chat/{LLMInputField.kt => AutoDevInputField.kt} (77%) diff --git a/src/main/kotlin/cc/unitmesh/devti/gui/chat/LLMInputField.kt b/src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputField.kt similarity index 77% rename from src/main/kotlin/cc/unitmesh/devti/gui/chat/LLMInputField.kt rename to src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputField.kt index 1ac8cea92a..fe56d0dcaa 100644 --- a/src/main/kotlin/cc/unitmesh/devti/gui/chat/LLMInputField.kt +++ b/src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputField.kt @@ -1,11 +1,11 @@ package cc.unitmesh.devti.gui.chat import cc.unitmesh.devti.AutoDevBundle -import cc.unitmesh.devti.provider.ContextPrompter import com.intellij.openapi.Disposable import com.intellij.openapi.actionSystem.* import com.intellij.openapi.actionSystem.ex.AnActionListener import com.intellij.openapi.command.CommandProcessor +import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.EditorModificationUtil import com.intellij.openapi.editor.actions.EnterAction import com.intellij.openapi.editor.actions.IncrementalFindAction @@ -15,19 +15,32 @@ import com.intellij.openapi.fileTypes.FileTypes import com.intellij.openapi.project.DumbAwareAction import com.intellij.openapi.project.Project import com.intellij.ui.EditorTextField +import com.intellij.util.EventDispatcher import com.intellij.util.messages.MessageBusConnection import com.intellij.util.ui.JBUI import java.awt.Color -import java.awt.event.ActionEvent import java.awt.event.KeyEvent +import java.util.* import javax.swing.KeyStroke -class LLMInputField( + +enum class AutoDevInputTrigger { + Button, + Key +} + +interface AutoDevInputListener : EventListener { + fun editorAdded(editor: EditorEx) {} + fun onSubmit(component: AutoDevInputField, trigger: AutoDevInputTrigger) {} +} + +class AutoDevInputField( project: Project, - chatCodingService: ChatCodingService, - component: ChatCodingComponent, private val listeners: List, ) : EditorTextField(project, FileTypes.PLAIN_TEXT), Disposable { + private val editorListeners: EventDispatcher = + EventDispatcher.create(AutoDevInputListener::class.java) + init { isOneLineMode = false updatePlaceholderText() @@ -44,7 +57,7 @@ class LLMInputField( DumbAwareAction.create { object : AnAction() { override fun actionPerformed(e1: AnActionEvent) { - val editor = this@LLMInputField.editor ?: return + val editor = this@AutoDevInputField.editor ?: return CommandProcessor.getInstance().executeCommand(project, { val eol = "\n" @@ -68,24 +81,19 @@ class LLMInputField( connect.subscribe(topic, object : AnActionListener { override fun afterActionPerformed(action: AnAction, event: AnActionEvent, result: AnActionResult) { - if (event.dataContext.getData(CommonDataKeys.EDITOR) === this@LLMInputField.editor && action is EnterAction) { - // todo: move this to a service - val listener: (ActionEvent) -> Unit = { - val prompt = this@LLMInputField.text - this@LLMInputField.text = "" - - val context = ChatContext(null, "", "") - chatCodingService.actionType = ChatActionType.REFACTOR - chatCodingService.handlePromptAndResponse(component, object : ContextPrompter() { - override fun displayPrompt() = prompt - override fun requestPrompt() = prompt - }, context) - } - - listener.invoke(ActionEvent(this@LLMInputField, 0, "")) + if (event.dataContext.getData(CommonDataKeys.EDITOR) === this@AutoDevInputField.editor && action is EnterAction) { + editorListeners.multicaster.onSubmit(this@AutoDevInputField, AutoDevInputTrigger.Key) } } }) + + listeners.forEach { listener -> + document.addDocumentListener(listener) + } + } + + override fun onEditorAdded(editor: Editor) { + editorListeners.multicaster.editorAdded((editor as EditorEx)) } private fun updatePlaceholderText() { @@ -116,4 +124,8 @@ class LLMInputField( override fun dispose() { listeners.forEach { editor?.document?.removeDocumentListener(it) } } + + fun addListener(listener: AutoDevInputListener) { + editorListeners.addListener(listener) + } } \ No newline at end of file diff --git a/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt b/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt index e1db0f06bb..604ace0a69 100644 --- a/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt +++ b/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt @@ -5,7 +5,6 @@ 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.ui.EditorTextField import com.intellij.ui.Gray import com.intellij.ui.JBColor import com.intellij.ui.OnePixelSplitter @@ -142,10 +141,8 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB private fun addQuestionArea() { val actionPanel = JPanel(BorderLayout()) - val searchTextArea = LLMInputField( + val searchTextArea = AutoDevInputField( chatCodingService.project, - chatCodingService, - this, listOf() ) @@ -154,13 +151,19 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB searchTextArea.text = "" val context = ChatContext(null, "", "") - chatCodingService.actionType = ChatActionType.REFACTOR + chatCodingService.actionType = ChatActionType.CHAT chatCodingService.handlePromptAndResponse(this, object : ContextPrompter() { override fun displayPrompt() = prompt override fun requestPrompt() = prompt }, context) } + searchTextArea.addListener(object : AutoDevInputListener { + override fun onSubmit(component: AutoDevInputField, trigger: AutoDevInputTrigger) { + listener.invoke(ActionEvent(component, 0, trigger.name)) + } + }) + actionPanel.add(searchTextArea, BorderLayout.CENTER) val actionButtons = JPanel(BorderLayout())