From 1d1fbe4d70a033c9a55167b56c556a321036a6ce Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Wed, 21 Aug 2024 14:19:12 +0300 Subject: [PATCH] fix: prompt text field inlay offset and up/down keys --- .../codegpt/ui/textarea/PromptTextField.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/PromptTextField.kt b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/PromptTextField.kt index d2b86b05..4c64d9a6 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/PromptTextField.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/PromptTextField.kt @@ -18,7 +18,6 @@ import ee.carlrobert.codegpt.ui.textarea.suggestion.SuggestionsPopupManager import ee.carlrobert.codegpt.ui.textarea.suggestion.item.SuggestionActionItem import ee.carlrobert.codegpt.ui.textarea.suggestion.item.SuggestionItem import java.awt.AWTEvent -import java.awt.Color import java.awt.Dimension import java.awt.KeyboardFocusManager import java.awt.event.InputEvent @@ -31,6 +30,8 @@ data class AppliedActionInlay( val inlay: Inlay, ) +const val AT_CHAR = '@' + class PromptTextField( project: Project, onTextChanged: (String) -> Unit, @@ -59,7 +60,10 @@ class PromptTextField( fun addInlayElement(actionPrefix: String, text: String?, actionItem: SuggestionActionItem) { editor?.let { - val startOffset = it.caretModel.offset - 1 + val startOffset = it.document.text.lastIndexOf(AT_CHAR) + if (startOffset == -1) { + throw IllegalStateException("No '@' symbol found in the text") + } runUndoTransparentWriteAction { it.document.deleteString(startOffset, it.document.textLength) @@ -128,10 +132,6 @@ class PromptTextFieldEventDispatcher( private val onSubmit: () -> Unit ) : IdeEventQueue.EventDispatcher { - companion object { - const val AT_CHAR = '@' - } - override fun dispatch(e: AWTEvent): Boolean { val owner = findParentByCondition(KeyboardFocusManager.getCurrentKeyboardFocusManager().focusOwner) { component -> @@ -192,13 +192,17 @@ class PromptTextFieldEventDispatcher( } private fun selectNextSuggestion(event: KeyEvent) { - suggestionsPopupManager.selectNext() - event.consume() + if (suggestionsPopupManager.isPopupVisible()) { + suggestionsPopupManager.selectNext() + event.consume() + } } private fun selectPreviousSuggestion(event: KeyEvent) { - suggestionsPopupManager.selectPrevious() - event.consume() + if (suggestionsPopupManager.isPopupVisible()) { + suggestionsPopupManager.selectPrevious() + event.consume() + } } private fun showPopup(event: KeyEvent) {