Skip to content

Commit

Permalink
feat(gui): add support for auto-completion popup in chat input #51
Browse files Browse the repository at this point in the history
The chat input now features an auto-completion popup, which is triggered when the user inputs a specific character (e.g., '$' or '¥'). This enhancement improves the user experience by providing suggestions to the user, making the chat input more intuitive and efficient. The popup is implemented using the `JBPopupFactory` and is created dynamically when needed.
  • Loading branch information
phodal committed Mar 6, 2024
1 parent 88b4633 commit 741dda8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Expand Up @@ -14,6 +14,7 @@ import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

@Service(Service.Level.PROJECT)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInput.kt
@@ -1,7 +1,6 @@
package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import com.intellij.temporary.gui.block.findDocument
import cc.unitmesh.devti.util.parser.Code.Companion.findLanguage
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.*
Expand All @@ -19,12 +18,14 @@ import com.intellij.openapi.fileTypes.FileTypes
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.temporary.gui.block.findDocument
import com.intellij.testFramework.LightVirtualFile
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.KeyAdapter
import java.awt.event.KeyEvent
import java.util.*
import javax.swing.KeyStroke
Expand Down Expand Up @@ -81,7 +82,6 @@ class AutoDevInput(
), this
)


val connect: MessageBusConnection = project.messageBus.connect(disposable ?: this)
val topic = AnActionListener.TOPIC
connect.subscribe(topic, object : AnActionListener {
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt
Expand Up @@ -21,6 +21,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.ComponentValidator
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.ui.popup.JBPopup
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.openapi.wm.impl.InternalDecorator
import com.intellij.temporary.gui.block.AutoDevCoolBorder
Expand All @@ -44,6 +46,7 @@ import java.awt.event.MouseEvent
import java.util.function.Supplier
import javax.swing.Box
import javax.swing.JComponent
import javax.swing.JTextField
import kotlin.math.max
import kotlin.math.min

Expand All @@ -61,6 +64,8 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab

private val logger = logger<AutoDevInputSection>()

private var popup: JBPopup? = null

val editorListeners = EventDispatcher.create(AutoDevInputListener::class.java)
private var tokenizer: Tokenizer? = null
var text: String
Expand Down Expand Up @@ -97,8 +102,20 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
if (i != input.height) {
revalidate()
}

// check new input == $
if (event.newFragment.contentEquals("$") || event.newFragment.contentEquals("¥")) {
popup?.show(this@AutoDevInputSection)
}
}
}

popup = JBPopupFactory.getInstance().createComponentPopupBuilder(JTextField(""), null)
.setRequestFocus(false)
.setMinSize(
Dimension(200, 200)
).createPopup()

input.addDocumentListener(documentListener)
input.recreateDocument()

Expand Down

0 comments on commit 741dda8

Please sign in to comment.