Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where up/down arrow keys always navigate chat history #1323

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/java/com/sourcegraph/cody/PromptPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import javax.swing.KeyStroke
import javax.swing.border.EmptyBorder
import javax.swing.event.AncestorEvent
import javax.swing.event.AncestorListener
import javax.swing.text.DefaultEditorKit

class PromptPanel(project: Project, private val chatSession: ChatSession) : JLayeredPane() {

Expand All @@ -39,6 +40,11 @@ class PromptPanel(project: Project, private val chatSession: ChatSession) : JLay
private var contextFilesListViewModel = DefaultListModel<DisplayedContextFile>()
private val contextFilesListView = JBList(contextFilesListViewModel)
private val contextFilesContainer = JBScrollPane(contextFilesListView)
// When "history mode" is enabled, pressing Up/Down arrow keys replaces the chat input with the
// previous/next chat message (via CodyChatMessageHistory). History mode can only activated when
// the chat input is empty, and it's deactivated on the first key action that is not an Up/Down
// arrow key.
private var isInHistoryMode = true

/** Externally updated state */
private val selectedContextItems: ArrayList<ContextItem> = ArrayList()
Expand Down Expand Up @@ -91,6 +97,15 @@ class PromptPanel(project: Project, private val chatSession: ChatSession) : JLay
refreshSendButton()
didUserInputChange()
}
textArea.addKeyListener(
object : KeyAdapter() {
override fun keyReleased(e: KeyEvent) {
if (e.keyCode != KeyEvent.VK_UP && e.keyCode != KeyEvent.VK_DOWN) {
isInHistoryMode = textArea.getText().isEmpty()
}
}
})

contextFilesListView.addMouseListener(
object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
Expand Down Expand Up @@ -125,8 +140,20 @@ class PromptPanel(project: Project, private val chatSession: ChatSession) : JLay
}
when (shortcut) {
ENTER -> if (sendButton.isEnabled) didSubmitChatMessage()
UP -> promptMessageHistory.popUpperMessage(textArea)
DOWN -> promptMessageHistory.popLowerMessage(textArea)
UP ->
if (isInHistoryMode) {
promptMessageHistory.popUpperMessage(textArea)
} else {
val defaultAction = textArea.actionMap[DefaultEditorKit.upAction]
defaultAction.actionPerformed(null)
}
DOWN ->
if (isInHistoryMode) {
promptMessageHistory.popLowerMessage(textArea)
} else {
val defaultAction = textArea.actionMap[DefaultEditorKit.downAction]
defaultAction.actionPerformed(null)
}
}
}

Expand All @@ -138,6 +165,7 @@ class PromptPanel(project: Project, private val chatSession: ChatSession) : JLay
// Reset text
promptMessageHistory.messageSent(text)
textArea.text = ""
isInHistoryMode = true
selectedContextItems.clear()

chatSession.sendMessage(text, cf)
Expand Down
Loading