Skip to content

Commit

Permalink
feat(ui): make chat works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 29, 2023
1 parent 7466800 commit 61d822d
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/LLMInputField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CustomShortcutSet
import com.intellij.openapi.actionSystem.KeyboardShortcut
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.EditorModificationUtil
import com.intellij.openapi.editor.actions.EnterAction
import com.intellij.openapi.editor.actions.IncrementalFindAction
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.editor.ex.EditorEx
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.messages.MessageBusConnection
import com.intellij.util.ui.JBUI
import java.awt.Color
import java.awt.event.KeyAdapter
import java.awt.event.ActionEvent
import java.awt.event.KeyEvent
import javax.swing.KeyStroke

Expand All @@ -25,19 +27,21 @@ class LLMInputField(
chatCodingService: ChatCodingService,
component: ChatCodingComponent,
private val listeners: List<DocumentListener>,
) : EditorTextField(project, FileTypes.PLAIN_TEXT) {
) : EditorTextField(project, FileTypes.PLAIN_TEXT), Disposable {
init {
isOneLineMode = false
updatePlaceholderText()
setFontInheritedFromLAF(true)
addSettingsProvider {
it.putUserData(IncrementalFindAction.SEARCH_DISABLED, true)
it.colorsScheme.lineSpacing = 1.0f
it.settings.isUseSoftWraps = true
it.settings.isPaintSoftWraps = false
it.isEmbeddedIntoDialogWrapper = true
it.contentComponent.setOpaque(false)
}

DumbAwareAction.create { e: AnActionEvent? ->
DumbAwareAction.create {
object : AnAction() {
override fun actionPerformed(e1: AnActionEvent) {
val editor = this@LLMInputField.editor ?: return
Expand All @@ -57,6 +61,31 @@ class LLMInputField(
KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.META_DOWN_MASK), null)
), this
)


val connect: MessageBusConnection = project.messageBus.connect(this)
val topic = AnActionListener.TOPIC

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, ""))
}
}
})
}

private fun updatePlaceholderText() {
Expand All @@ -83,4 +112,8 @@ class LLMInputField(
}
return super.getBackground()
}

override fun dispose() {
listeners.forEach { editor?.document?.removeDocumentListener(it) }
}
}

0 comments on commit 61d822d

Please sign in to comment.