From 92f689071ab7ac2eda8032a054d1a484312bb26d Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 15:00:51 -0700 Subject: [PATCH 1/5] inline diff red --- core/diff/streamDiff.ts | 1 + .../editor/DiffStreamHandler.kt | 69 ++++++++++++++++--- .../editor/EditorComponentInlaysManager.kt | 12 ++-- .../editor/InlineEditAction.kt | 6 +- 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/core/diff/streamDiff.ts b/core/diff/streamDiff.ts index 42c8409b9..2761b5d97 100644 --- a/core/diff/streamDiff.ts +++ b/core/diff/streamDiff.ts @@ -8,6 +8,7 @@ import { LineStream, matchLine } from "./util.js"; * - old + same = oldLinesCopy.length * ^ (above two guarantee that all lines get represented) * - Lines are always output in order, at least among old and new separately + * - Old lines in a hunk are always output before the new lines */ export async function* streamDiff( oldLines: string[], 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 f30347150..46e37dc7c 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 @@ -7,6 +7,7 @@ import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.command.undo.UndoManager import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.openapi.editor.colors.TextAttributesKey import com.intellij.openapi.editor.markup.HighlighterLayer import com.intellij.openapi.editor.markup.RangeHighlighter @@ -17,6 +18,7 @@ import com.intellij.openapi.fileEditor.TextEditor import com.intellij.openapi.project.Project import com.intellij.ui.JBColor import java.awt.event.KeyEvent +import javax.swing.BorderFactory import javax.swing.JTextArea import kotlin.math.min @@ -34,6 +36,7 @@ class DiffStreamHandler( private val onClose: () -> Unit, private val onFinish: () -> Unit ) { + // Text attributes keys private val greenKey = run { val attributes = TextAttributes().apply { backgroundColor = JBColor(0x3000FF00.toInt(), 0x3000FF00.toInt()) @@ -61,12 +64,18 @@ class DiffStreamHandler( key } + private val editorComponentInlaysManager = EditorComponentInlaysManager.from(editor, false) + + // State variables private var currentLine = startLine private var currentLineHighlighter: RangeHighlighter? = null private val unfinishedHighlighters: MutableList = mutableListOf() - private var changeCount: Int = 0 + private var changeCount: Int = 0 // Kept track of so we can count steps to undo when cancelling private var running: Boolean = false + private var deletionBufferStartLine: Int = -1 + private val deletionsBuffer: MutableList = mutableListOf() + fun setup() { // Highlight the range with unfinished color for (i in startLine..endLine) { @@ -77,11 +86,44 @@ class DiffStreamHandler( } } + private fun deleteLineAt(index: Int) { + val startOffset = editor.document.getLineStartOffset(index) + val endOffset = editor.document.getLineEndOffset(index) + 1 + editor.document.deleteString(startOffset, min(endOffset, editor.document.textLength)) + } + + private fun insertDeletionBuffer() { + // Insert red highlighted code between the real lines in the editor + if (deletionBufferStartLine != -1 && deletionsBuffer.isNotEmpty()) { + val component = JTextArea().apply { + text = deletionsBuffer.joinToString(" \n") + isEditable = false + background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt()) + foreground = JBColor.GRAY + border = BorderFactory.createEmptyBorder(0, 0, 0, 0) + lineWrap = false + wrapStyleWord = false + columns = Int.MAX_VALUE // This ensures the component takes up the full width + margin = java.awt.Insets(0, -100, 0, 0) + + font = editor.colorsScheme.getFont(EditorFontType.PLAIN) + preferredSize = java.awt.Dimension(editor.contentComponent.width, preferredSize.height) + } + + editorComponentInlaysManager.insertAfter(deletionBufferStartLine - 1, component) + + // Clear the buffer + deletionsBuffer.clear() + deletionBufferStartLine = -1 + } + } + private fun handleDiffLine(type: DiffLineType, line: String) { println("DiffStreamHandler: handleDiffLine: $currentLine, $type, $line") try { when (type) { DiffLineType.SAME -> { + insertDeletionBuffer() currentLine++ } DiffLineType.NEW -> { @@ -95,14 +137,21 @@ class DiffStreamHandler( // Highlight the new line green editor.markupModel.addLineHighlighter(greenKey, currentLine, HighlighterLayer.LAST) + insertDeletionBuffer() + currentLine++ changeCount++ } DiffLineType.OLD -> { // Remove old line - val startOffset = editor.document.getLineStartOffset(currentLine) - val endOffset = editor.document.getLineEndOffset(currentLine) + 1 - editor.document.deleteString(startOffset, min(endOffset, editor.document.textLength)) + deleteLineAt(currentLine) + + // Add to deletions buffer + deletionsBuffer.add(line) + if (deletionBufferStartLine == -1) { + deletionBufferStartLine = currentLine + } + changeCount++ } } @@ -175,12 +224,7 @@ class DiffStreamHandler( running = true // 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) - } + setup() // Request diff stream from core val continuePluginService = ServiceManager.getService( @@ -206,12 +250,15 @@ class DiffStreamHandler( onFinish() ApplicationManager.getApplication().invokeLater { - // Clean up highlighters + // Clean up progress highlighters if (currentLineHighlighter != null) { editor.markupModel.removeHighlighter(currentLineHighlighter!!) } unfinishedHighlighters.forEach { editor.markupModel.removeHighlighter(it) } + // Flush deletion buffer + insertDeletionBuffer() + // Add ", " to the text area textArea.document.insertString(textArea.caretPosition, ", ", null) textArea.requestFocus() diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt index c5b089d7a..f8277d383 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt @@ -26,7 +26,7 @@ import kotlin.math.min * Copied from com.intellij.util.ui.codereview.diff.EditorComponentInlaysManager * via https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/EditorComponentInlaysManager.kt */ -class EditorComponentInlaysManager(val editor: EditorImpl) : Disposable { +class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneInlay: Boolean) : Disposable { private val managedInlays = mutableMapOf() private val editorWidthWatcher = EditorTextWidthWatcher() @@ -44,8 +44,10 @@ class EditorComponentInlaysManager(val editor: EditorImpl) : Disposable { fun insertAfter(lineIndex: Int, component: JComponent): Disposable? { if (Disposer.isDisposed(this)) return null - // Dispose all other inlays - managedInlays.values.forEach(Disposer::dispose) + if (onlyOneInlay) { + // Dispose all other inlays + managedInlays.values.forEach(Disposer::dispose) + } val wrappedComponent = ComponentWrapper(component) val offset = editor.document.getLineEndOffset(lineIndex) @@ -144,11 +146,11 @@ class EditorComponentInlaysManager(val editor: EditorImpl) : Disposable { companion object { val INLAYS_KEY: Key = Key.create("EditorComponentInlaysManager") - fun from(editor: Editor): EditorComponentInlaysManager { + fun from(editor: Editor, onlyOneInlay: Boolean): EditorComponentInlaysManager { return synchronized(editor) { val manager = editor.getUserData(INLAYS_KEY) if (manager == null) { - val newManager = EditorComponentInlaysManager(editor as EditorImpl) + val newManager = EditorComponentInlaysManager(editor as EditorImpl, false) editor.putUserData(INLAYS_KEY, newManager) newManager } else manager 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 f94214c34..5b77f8165 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 @@ -103,7 +103,7 @@ fun openInlineEdit(project: Project?, editor: Editor) { return } - val manager = EditorComponentInlaysManager.from(editor) + val manager = EditorComponentInlaysManager.from(editor, true) // Get list of model titles val continuePluginService = project.service() @@ -192,7 +192,9 @@ fun openInlineEdit(project: Project?, editor: Editor) { diffStreamService.reject(editor) // Re-highlight the selected text - selectionModel.setSelection(start, end) + val documentLength = editor.document.textLength + val safeEnd = minOf(end, documentLength) + selectionModel.setSelection(start, safeEnd) } else if (e.keyCode == KeyEvent.VK_ENTER) { if (e.modifiersEx == KeyEvent.SHIFT_DOWN_MASK) { textArea.document.insertString(textArea.caretPosition, "\n", null) From b48a77bd68a6929a901c7497c90398a0d38647e6 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 16:46:15 -0700 Subject: [PATCH 2/5] fix model selectedIndex err --- .../continueintellijextension/editor/InlineEditAction.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 5b77f8165..aea1b8c6b 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 @@ -278,8 +278,10 @@ class CustomPanel(layout: MigLayout, project: Project, modelTitles: List horizontalAlignment = SwingConstants.RIGHT } selectedIndex = continueSettingsService.continueState.lastSelectedInlineEditModel?.let { - val index = modelTitles.indexOf(it) - if (index != -1) index else 0 + if (modelTitles.isEmpty()) -1 else { + val index = modelTitles.indexOf(it) + if (index != -1) index else 0 + } } ?: 0 addActionListener { From f37884dee81130bc20a8e2ab9498112b3f8efed1 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 17:14:54 -0700 Subject: [PATCH 3/5] remove deletion inlays --- .../editor/DiffStreamHandler.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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 46e37dc7c..7de4ec6ec 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 @@ -2,6 +2,7 @@ package com.github.continuedev.continueintellijextension.editor import com.github.continuedev.continueintellijextension.services.ContinuePluginService import com.google.gson.Gson +import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.command.undo.UndoManager @@ -75,6 +76,14 @@ class DiffStreamHandler( private var deletionBufferStartLine: Int = -1 private val deletionsBuffer: MutableList = mutableListOf() + private val deletionInlays: MutableList = mutableListOf() + + private fun removeDeletionInlays() { + deletionInlays.forEach { + it.dispose() + } + deletionInlays.clear() + } fun setup() { // Highlight the range with unfinished color @@ -110,7 +119,10 @@ class DiffStreamHandler( preferredSize = java.awt.Dimension(editor.contentComponent.width, preferredSize.height) } - editorComponentInlaysManager.insertAfter(deletionBufferStartLine - 1, component) + val disposable = editorComponentInlaysManager.insertAfter(deletionBufferStartLine - 1, component) + if (disposable != null) { + deletionInlays.add(disposable) + } // Clear the buffer deletionsBuffer.clear() @@ -177,6 +189,9 @@ class DiffStreamHandler( // Remove all highlighters editor.markupModel.removeAllHighlighters() + // Remove all of the deletion inlays + removeDeletionInlays() + // Undo changes just by using builtin undo WriteCommandAction.runWriteCommandAction(project) { val undoManager = UndoManager.getInstance(project) @@ -197,6 +212,7 @@ class DiffStreamHandler( fun accept() { // Accept the changes editor.markupModel.removeAllHighlighters() + removeDeletionInlays() onClose() running = false } @@ -204,6 +220,7 @@ class DiffStreamHandler( fun reject() { // Reject the changes resetState() + removeDeletionInlays() onClose() running = false } From d1267877f4e9ee668c044c165d1bf6998bfca212 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 17:27:04 -0700 Subject: [PATCH 4/5] insert inlays above --- .../continueintellijextension/editor/DiffStreamHandler.kt | 4 +--- .../editor/EditorComponentInlaysManager.kt | 4 ++-- .../continueintellijextension/editor/InlineEditAction.kt | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) 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 7de4ec6ec..d34603482 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 @@ -1,7 +1,6 @@ package com.github.continuedev.continueintellijextension.editor import com.github.continuedev.continueintellijextension.services.ContinuePluginService -import com.google.gson.Gson import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.command.WriteCommandAction @@ -18,7 +17,6 @@ import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.fileEditor.TextEditor import com.intellij.openapi.project.Project import com.intellij.ui.JBColor -import java.awt.event.KeyEvent import javax.swing.BorderFactory import javax.swing.JTextArea import kotlin.math.min @@ -119,7 +117,7 @@ class DiffStreamHandler( preferredSize = java.awt.Dimension(editor.contentComponent.width, preferredSize.height) } - val disposable = editorComponentInlaysManager.insertAfter(deletionBufferStartLine - 1, component) + val disposable = editorComponentInlaysManager.insert(deletionBufferStartLine, component, true) if (disposable != null) { deletionInlays.add(disposable) } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt index f8277d383..6ed4df136 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt @@ -41,7 +41,7 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn } @RequiresEdt - fun insertAfter(lineIndex: Int, component: JComponent): Disposable? { + fun insert(lineIndex: Int, component: JComponent, showAbove: Boolean = false): Disposable? { if (Disposer.isDisposed(this)) return null if (onlyOneInlay) { @@ -57,7 +57,7 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn EditorEmbeddedComponentManager.Properties(EditorEmbeddedComponentManager.ResizePolicy.none(), null, true, - false, + showAbove, 0, offset))?.also { managedInlays[wrappedComponent] = it 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 aea1b8c6b..318afda59 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 @@ -179,7 +179,7 @@ fun openInlineEdit(project: Project?, editor: Editor) { diffStreamService.reject(editor) inlayRef.get().dispose() }) - val inlay = manager.insertAfter(lineNumber, panel) + val inlay = manager.insert(startLineNum, panel, true) panel.revalidate() inlayRef.set(inlay) val viewport = (editor as? EditorImpl)?.scrollPane?.viewport From 10a5ec18a9df189d58c60886be296f1107a1dfc2 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 3 Oct 2024 15:50:50 -0700 Subject: [PATCH 5/5] feat: make deletion line highlight wider --- .../editor/DiffStreamHandler.kt | 130 ++++++++++-------- .../editor/EditorComponentInlaysManager.kt | 35 +++-- 2 files changed, 90 insertions(+), 75 deletions(-) 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 d34603482..ee5633447 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 @@ -9,6 +9,7 @@ import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.openapi.editor.colors.TextAttributesKey +import com.intellij.openapi.editor.markup.EffectType import com.intellij.openapi.editor.markup.HighlighterLayer import com.intellij.openapi.editor.markup.RangeHighlighter import com.intellij.openapi.editor.markup.TextAttributes @@ -27,13 +28,13 @@ enum class DiffLineType { } class DiffStreamHandler( - private val project: Project, - private val editor: Editor, - private val textArea: JTextArea, - private val startLine: Int, - private val endLine: Int, - private val onClose: () -> Unit, - private val onFinish: () -> Unit + private val project: Project, + private val editor: Editor, + private val textArea: JTextArea, + private val startLine: Int, + private val endLine: Int, + private val onClose: () -> Unit, + private val onFinish: () -> Unit ) { // Text attributes keys private val greenKey = run { @@ -86,9 +87,11 @@ class DiffStreamHandler( fun setup() { // Highlight the range with unfinished color for (i in startLine..endLine) { - val highlighter = editor.markupModel.addLineHighlighter(unfinishedKey, min( + val highlighter = editor.markupModel.addLineHighlighter( + unfinishedKey, min( i, editor.document.lineCount - 1 - ), HighlighterLayer.FIRST) + ), HighlighterLayer.LAST + ) unfinishedHighlighters.add(highlighter) } } @@ -103,21 +106,18 @@ class DiffStreamHandler( // Insert red highlighted code between the real lines in the editor if (deletionBufferStartLine != -1 && deletionsBuffer.isNotEmpty()) { val component = JTextArea().apply { - text = deletionsBuffer.joinToString(" \n") + text = deletionsBuffer.joinToString("\n") isEditable = false background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt()) foreground = JBColor.GRAY border = BorderFactory.createEmptyBorder(0, 0, 0, 0) lineWrap = false wrapStyleWord = false - columns = Int.MAX_VALUE // This ensures the component takes up the full width - margin = java.awt.Insets(0, -100, 0, 0) - font = editor.colorsScheme.getFont(EditorFontType.PLAIN) - preferredSize = java.awt.Dimension(editor.contentComponent.width, preferredSize.height) } val disposable = editorComponentInlaysManager.insert(deletionBufferStartLine, component, true) + if (disposable != null) { deletionInlays.add(disposable) } @@ -131,53 +131,59 @@ class DiffStreamHandler( private fun handleDiffLine(type: DiffLineType, line: String) { println("DiffStreamHandler: handleDiffLine: $currentLine, $type, $line") try { - when (type) { - DiffLineType.SAME -> { - insertDeletionBuffer() - currentLine++ - } - DiffLineType.NEW -> { - // Insert new line - if (currentLine == editor.document.lineCount) { - editor.document.insertString(editor.document.textLength, "\n") - } - val offset = editor.document.getLineStartOffset(currentLine) - editor.document.insertString(offset, line + "\n") - - // Highlight the new line green - editor.markupModel.addLineHighlighter(greenKey, currentLine, HighlighterLayer.LAST) - - insertDeletionBuffer() - - currentLine++ - changeCount++ - } - DiffLineType.OLD -> { - // Remove old line - deleteLineAt(currentLine) - - // Add to deletions buffer - deletionsBuffer.add(line) - if (deletionBufferStartLine == -1) { - deletionBufferStartLine = currentLine + when (type) { + DiffLineType.SAME -> { + insertDeletionBuffer() + currentLine++ } - changeCount++ - } - } + DiffLineType.NEW -> { + // Insert new line + if (currentLine == editor.document.lineCount) { + editor.document.insertString(editor.document.textLength, "\n") + } + val offset = editor.document.getLineStartOffset(currentLine) + editor.document.insertString(offset, line + "\n") - // Highlight the current line - if (currentLineHighlighter != null) { - editor.markupModel.removeHighlighter(currentLineHighlighter!!) - } - currentLineHighlighter = editor.markupModel.addLineHighlighter(currentLineKey, min(currentLine, editor.document.lineCount - 1), HighlighterLayer.LAST) + // Highlight the new line green + editor.markupModel.addLineHighlighter(greenKey, currentLine, HighlighterLayer.LAST) - // Remove the unfinished highlighter top line - if (type != DiffLineType.OLD) { - if (unfinishedHighlighters.isNotEmpty()) { - editor.markupModel.removeHighlighter(unfinishedHighlighters.removeAt(0)) + insertDeletionBuffer() + + currentLine++ + changeCount++ + } + + DiffLineType.OLD -> { + // Remove old line + deleteLineAt(currentLine) + + // Add to deletions buffer + deletionsBuffer.add(line) + if (deletionBufferStartLine == -1) { + deletionBufferStartLine = currentLine + } + + changeCount++ + } + } + + // Highlight the current line + if (currentLineHighlighter != null) { + editor.markupModel.removeHighlighter(currentLineHighlighter!!) + } + currentLineHighlighter = editor.markupModel.addLineHighlighter( + currentLineKey, + min(currentLine, editor.document.lineCount - 1), + HighlighterLayer.LAST + ) + + // Remove the unfinished highlighter top line + if (type != DiffLineType.OLD) { + if (unfinishedHighlighters.isNotEmpty()) { + editor.markupModel.removeHighlighter(unfinishedHighlighters.removeAt(0)) + } } - } } catch (e: Exception) { println("Error handling diff line: $currentLine, $type, $line, $e.message") } @@ -232,7 +238,7 @@ class DiffStreamHandler( } } - fun run(input : String, prefix : String, highlighted : String, suffix : String, modelTitle : String) { + fun run(input: String, prefix: String, highlighted: String, suffix: String, modelTitle: String) { // Undo changes resetState() @@ -243,18 +249,20 @@ class DiffStreamHandler( // Request diff stream from core val continuePluginService = ServiceManager.getService( - this.project, - ContinuePluginService::class.java + this.project, + ContinuePluginService::class.java ) val virtualFile = FileDocumentManager.getInstance().getFile(editor.document) - continuePluginService.coreMessenger?.request("streamDiffLines", mapOf( + continuePluginService.coreMessenger?.request( + "streamDiffLines", mapOf( "input" to input, "prefix" to prefix, "highlighted" to highlighted, "suffix" to suffix, "language" to virtualFile?.fileType?.name, "modelTitle" to modelTitle - ), null) { response -> + ), null + ) { response -> if (!running) { return@request } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt index 6ed4df136..20d31a519 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorComponentInlaysManager.kt @@ -14,8 +14,10 @@ import com.intellij.util.concurrency.annotations.RequiresEdt import com.intellij.util.ui.JBUI import java.awt.Dimension import java.awt.Font +import java.awt.Insets import java.awt.event.ComponentAdapter import java.awt.event.ComponentEvent +import javax.swing.BorderFactory import javax.swing.JComponent import javax.swing.ScrollPaneConstants import kotlin.math.ceil @@ -53,16 +55,20 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn val offset = editor.document.getLineEndOffset(lineIndex) return EditorEmbeddedComponentManager.getInstance() - .addComponent(editor, wrappedComponent, - EditorEmbeddedComponentManager.Properties(EditorEmbeddedComponentManager.ResizePolicy.none(), - null, - true, - showAbove, - 0, - offset))?.also { - managedInlays[wrappedComponent] = it - Disposer.register(it, Disposable { managedInlays.remove(wrappedComponent) }) - } + .addComponent( + editor, wrappedComponent, + EditorEmbeddedComponentManager.Properties( + EditorEmbeddedComponentManager.ResizePolicy.none(), + null, + true, + showAbove, + 0, + offset + ) + )?.also { + managedInlays[wrappedComponent] = it + Disposer.register(it, Disposable { managedInlays.remove(wrappedComponent) }) + } } private inner class ComponentWrapper(private val component: JComponent) : JBScrollPane(component) { @@ -79,12 +85,12 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn component.addComponentListener(object : ComponentAdapter() { override fun componentResized(e: ComponentEvent) = - dispatchEvent(ComponentEvent(component, ComponentEvent.COMPONENT_RESIZED)) + dispatchEvent(ComponentEvent(component, ComponentEvent.COMPONENT_RESIZED)) }) } override fun getPreferredSize(): Dimension { - return Dimension(editorWidthWatcher.editorTextWidth, component.preferredSize.height) + return Dimension(editor.contentComponent.width, component.preferredSize.height) } } @@ -107,7 +113,7 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn val scrollbarFlip = editor.scrollPane.getClientProperty(JBScrollPane.Flip::class.java) verticalScrollbarFlipped = - scrollbarFlip == JBScrollPane.Flip.HORIZONTAL || scrollbarFlip == JBScrollPane.Flip.BOTH + scrollbarFlip == JBScrollPane.Flip.HORIZONTAL || scrollbarFlip == JBScrollPane.Flip.BOTH } override fun componentResized(e: ComponentEvent) = updateWidthForAllInlays() @@ -126,7 +132,8 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn } private fun calcWidth(): Int { - val visibleEditorTextWidth = editor.scrollPane.viewport.width - getVerticalScrollbarWidth() - getGutterTextGap() + val visibleEditorTextWidth = + editor.scrollPane.viewport.width - getVerticalScrollbarWidth() - getGutterTextGap() return min(max(visibleEditorTextWidth, 0), maximumEditorTextWidth) }