Skip to content

Commit

Permalink
feat(ui): etract for new method
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 29, 2023
1 parent 61d822d commit 51342a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
@@ -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
Expand All @@ -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<DocumentListener>,
) : EditorTextField(project, FileTypes.PLAIN_TEXT), Disposable {
private val editorListeners: EventDispatcher<AutoDevInputListener> =
EventDispatcher.create(AutoDevInputListener::class.java)

init {
isOneLineMode = false
updatePlaceholderText()
Expand All @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -116,4 +124,8 @@ class LLMInputField(
override fun dispose() {
listeners.forEach { editor?.document?.removeDocumentListener(it) }
}

fun addListener(listener: AutoDevInputListener) {
editorListeners.addListener(listener)
}
}
Expand Up @@ -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
Expand Down Expand Up @@ -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()
)

Expand All @@ -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())
Expand Down

0 comments on commit 51342a2

Please sign in to comment.