From 0c2d8880dc8edfa53296d7ef886cbf3a8f2ac5a6 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 31 Mar 2024 23:40:40 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20improve=20small=20interactions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/ContinuePluginActions.kt | 7 ++++-- .../editor/DiffStreamHandler.kt | 24 +++++++++++++++++-- .../editor/InlineEditAction.kt | 17 +++++++------ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt index ba6bbe472..5eaae2fc6 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt @@ -8,6 +8,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.PlatformDataKeys import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.components.service +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.Messages import com.intellij.openapi.wm.ToolWindowManager @@ -38,7 +39,8 @@ class AcceptDiffAction : AnAction() { } private fun acceptVerticalDiff(e: AnActionEvent) { - val editor = e.getData(PlatformDataKeys.EDITOR) ?: return + val project = e.project ?: return + val editor = e.getData(PlatformDataKeys.EDITOR) ?: FileEditorManager.getInstance(project).selectedTextEditor ?: return val diffStreamService = service() diffStreamService.accept(editor) } @@ -56,7 +58,8 @@ class RejectDiffAction : AnAction() { } private fun rejectVerticalDiff(e: AnActionEvent) { - val editor = e.getData(PlatformDataKeys.EDITOR) ?: return + val project = e.project ?: return + val editor = e.getData(PlatformDataKeys.EDITOR) ?: FileEditorManager.getInstance(project).selectedTextEditor ?: return val diffStreamService = service() diffStreamService.reject(editor) } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt index 9c41b9717..4bc251a79 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt @@ -44,7 +44,7 @@ class DiffStreamHandler( private val currentLineKey = run { val attributes = TextAttributes().apply { - backgroundColor = JBColor(0x20888888.toInt(), 0x20888888.toInt()) + backgroundColor = JBColor(0x40888888.toInt(), 0x40888888.toInt()) } val key = TextAttributesKey.createTextAttributesKey("CONTINUE_DIFF_CURRENT_LINE") key.let { editor.colorsScheme.setAttributes(it, attributes) } @@ -53,7 +53,7 @@ class DiffStreamHandler( private val unfinishedKey = run { val attributes = TextAttributes().apply { - backgroundColor = JBColor(0x10888888.toInt(), 0x10888888.toInt()) + backgroundColor = JBColor(0x20888888.toInt(), 0x20888888.toInt()) } val key = TextAttributesKey.createTextAttributesKey("CONTINUE_DIFF_UNFINISHED_LINE") key.let { editor.colorsScheme.setAttributes(it, attributes) } @@ -64,6 +64,17 @@ class DiffStreamHandler( private var currentLineHighlighter: RangeHighlighter? = null private val unfinishedHighlighters: MutableList = mutableListOf() private var changeCount: Int = 0 + private var running: Boolean = false + + init { + // Highlight the range with unfinished color + for (i in startLine..endLine) { + val highlighter = editor.markupModel.addLineHighlighter(unfinishedKey, min( + i, editor.document.lineCount - 1 + ), HighlighterLayer.FIRST) + unfinishedHighlighters.add(highlighter) + } + } private fun handleDiffLine(type: DiffLineType, line: String) { println("DiffStreamHandler: handleDiffLine: $currentLine, $type, $line") @@ -136,18 +147,23 @@ class DiffStreamHandler( fun accept() { // Accept the changes editor.markupModel.removeAllHighlighters() + onClose() + running = false } fun reject() { // Reject the changes resetState() onClose() + running = false } fun run(input : String, prefix : String, highlighted : String, suffix : String) { // Undo changes resetState() + running = true + // Highlight the range with unfinished color for (i in startLine..endLine) { val highlighter = editor.markupModel.addLineHighlighter(unfinishedKey, min( @@ -169,6 +185,10 @@ class DiffStreamHandler( "suffix" to suffix, "language" to virtualFile?.fileType?.name, ), null) { response -> + if (!running) { + return@request + } + val parsed = Gson().fromJson(response, Map::class.java) val done = parsed["done"] as? Boolean if (done == true) { diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt index be858a423..df6dc0239 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt @@ -94,7 +94,8 @@ class InlineEditAction : AnAction(), DumbAware { } val panel = makePanel(customPanelRef, textArea, inlayRef, leftInset, {onEnter()}, { - inlayRef.get().dispose() + diffStreamHandler.reject() + selectionModel.setSelection(start, end) }, { diffStreamService.accept(editor) inlayRef.get().dispose() @@ -112,7 +113,10 @@ class InlineEditAction : AnAction(), DumbAware { textArea.addKeyListener(object : KeyAdapter() { override fun keyPressed(e: KeyEvent) { if (e.keyCode == KeyEvent.VK_ESCAPE) { - inlayRef.get().dispose() + diffStreamHandler.reject() + + // Re-highlight the selected text + selectionModel.setSelection(start, end) } else if (e.keyCode == KeyEvent.VK_ENTER) { if (e.modifiersEx == KeyEvent.SHIFT_DOWN_MASK) { textArea.document.insertString(textArea.caretPosition, "\n", null) @@ -186,7 +190,7 @@ class InlineEditAction : AnAction(), DumbAware { setup() val shadow = DropShadowBorder() - shadow.shadowColor = JBColor(0xb0b0b0, 0x808080) + shadow.shadowColor = JBColor(0xb0b0b0, 0x505050) shadow.isShowRightShadow = true shadow.isShowBottomShadow = true shadow.shadowSize = SHADOW_SIZE @@ -216,7 +220,7 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit, foreground = Color(128, 128, 128, 200) background = defaultBackground } - val rightButton = CustomButton("⏎ Submit", {onEnter()}).apply { + val rightButton = CustomButton("Submit", {onEnter()}).apply { background = GetTheme().getHighlight() foreground = JBColor.WHITE } @@ -236,11 +240,11 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit, font = Font("Arial", Font.PLAIN, 11) } - val leftButton = CustomButton("${getAltKeyLabel()}⇧Y", {onReject()}).apply { + val leftButton = CustomButton("${getAltKeyLabel()}⇧N", {onReject()}).apply { background = Color(255, 0, 0, 64) } - val rightButton = CustomButton("${getAltKeyLabel()}⇧N", {onAccept()}).apply { + val rightButton = CustomButton("${getAltKeyLabel()}⇧Y", {onAccept()}).apply { background = Color(0, 255, 0, 64) } @@ -262,7 +266,6 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit, } fun enter() { - // Switch to subPanelB remove(subPanelA) add(subPanelB, "grow, gap 0!") revalidate()