update diff accept/reject logi

This commit is contained in:
Test 2024-10-09 14:22:17 -07:00
parent 26e536847c
commit b6b39dbf0a
7 changed files with 707 additions and 494 deletions

View file

@ -26,6 +26,13 @@ enum class DiffLineType {
SAME, NEW, OLD
}
data class VerticalDiffBlock(
val start: Int,
val numRed: Int,
val numGreen: Int,
val buttons: VerticalDiffActionButtons
)
class DiffStreamHandler(
private val project: Project,
private val editor: Editor,
@ -70,11 +77,13 @@ class DiffStreamHandler(
private var currentLineHighlighter: RangeHighlighter? = null
private val unfinishedHighlighters: MutableList<RangeHighlighter> = mutableListOf()
private var changeCount: Int = 0 // Kept track of so we can count steps to undo when cancelling
private var insertedInCurrentBlock: Int = 0;
private var running: Boolean = false
private var deletionBufferStartLine: Int = -1
private val deletionsBuffer: MutableList<String> = mutableListOf()
private val deletionInlays: MutableList<Disposable> = mutableListOf()
private val verticalDiffBlocks: MutableList<VerticalDiffBlock> = mutableListOf()
private fun removeDeletionInlays() {
deletionInlays.forEach {
@ -102,8 +111,45 @@ class DiffStreamHandler(
}
private fun insertDeletionBuffer() {
if (deletionsBuffer.isNotEmpty() || insertedInCurrentBlock > 0) {
val start = currentLine - insertedInCurrentBlock
val numGreen = insertedInCurrentBlock
val numRed = deletionsBuffer.size
val buttons = VerticalDiffActionButtons(
editor,
start,
onClickAccept = {
acceptRejectBlock(true, start, numGreen, numRed)
},
onClickReject = {
acceptRejectBlock(false, start, numGreen, numRed)
}
)
verticalDiffBlocks.add(
VerticalDiffBlock(
start = start,
numRed = deletionsBuffer.size,
numGreen = insertedInCurrentBlock,
buttons = buttons
)
)
editor.contentComponent.add(buttons.acceptButton)
editor.contentComponent.add(buttons.rejectButton)
editor.contentComponent.revalidate()
editor.contentComponent.repaint()
}
if (deletionsBuffer.isEmpty()) {
insertedInCurrentBlock = 0;
return;
}
// TODO: Handle decoration updates according to insertedInCurrentBlock
// Insert red highlighted code between the real lines in the editor
if (deletionBufferStartLine != -1 && deletionsBuffer.isNotEmpty()) {
if (deletionBufferStartLine != -1) {
val component = JTextArea().apply {
text = deletionsBuffer.joinToString("\n")
isEditable = false
@ -125,6 +171,8 @@ class DiffStreamHandler(
deletionsBuffer.clear()
deletionBufferStartLine = -1
}
insertedInCurrentBlock = 0;
}
private fun handleDiffLine(type: DiffLineType, line: String) {
@ -137,28 +185,25 @@ class DiffStreamHandler(
}
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++
insertedInCurrentBlock++;
}
DiffLineType.OLD -> {
// Remove old line
deleteLineAt(currentLine)
// Add to deletions buffer
deletionsBuffer.add(line)
if (deletionBufferStartLine == -1) {
deletionBufferStartLine = currentLine
}
@ -171,6 +216,7 @@ class DiffStreamHandler(
if (currentLineHighlighter != null) {
editor.markupModel.removeHighlighter(currentLineHighlighter!!)
}
currentLineHighlighter = editor.markupModel.addLineHighlighter(
currentLineKey,
min(currentLine, editor.document.lineCount - 1),
@ -188,6 +234,64 @@ class DiffStreamHandler(
}
}
private fun acceptRejectBlock(
accept: Boolean,
startLine: Int,
numGreen: Int,
numRed: Int
) {
// TODO: We use virtual lines for red so we should shouldnt be doing numRed addition
if (numGreen > 0) {
val highlightersToRemove = editor.markupModel.allHighlighters.filter { highlighter ->
val highlighterLine = editor.document.getLineNumber(highlighter.startOffset)
highlighterLine in startLine until (startLine + numRed)
}
if (highlightersToRemove.isNotEmpty()) {
editor.markupModel.removeHighlighter(highlightersToRemove.first())
}
if (!accept) {
// Delete the actual lines
WriteCommandAction.runWriteCommandAction(project) {
for (i in 0 until numGreen) {
deleteLineAt(startLine + numRed)
}
}
}
}
if (numRed > 0 && accept) {
// Delete the actual lines
WriteCommandAction.runWriteCommandAction(project) {
for (i in 0 until numRed) {
deleteLineAt(startLine)
}
}
}
// Shift everything below upward
val offset = -(if (accept) numRed else numGreen)
// Update the positions of the vertical diff blocks
verticalDiffBlocks.forEach { block ->
if (block.start > startLine) {
block.buttons.updatePosition(block.start + offset)
}
}
// Remove the processed block
verticalDiffBlocks.removeAll { it.start == startLine }
if (verticalDiffBlocks.isEmpty()) {
onClose()
}
// Refresh the editor
editor.contentComponent.revalidate()
editor.contentComponent.repaint()
}
private fun resetState() {
// Remove all highlighters
editor.markupModel.removeAllHighlighters()
@ -212,29 +316,36 @@ class DiffStreamHandler(
}
}
fun accept() {
private fun removeDiffBlock(block: VerticalDiffBlock) {
editor.contentComponent.remove(block.buttons.acceptButton)
editor.contentComponent.remove(block.buttons.rejectButton)
verticalDiffBlocks.remove(block)
editor.contentComponent.revalidate()
editor.contentComponent.repaint()
}
private fun removeAllDiffBlocks() {
verticalDiffBlocks.forEach(::removeDiffBlock)
}
fun acceptAll() {
// Accept the changes
editor.markupModel.removeAllHighlighters()
removeDeletionInlays()
onClose()
running = false
removeAllDiffBlocks()
}
fun reject() {
fun rejectAll() {
// Reject the changes
resetState()
removeDeletionInlays()
onClose()
running = false
}
fun toggleFocus() {
if (textArea.hasFocus()) {
textArea.transferFocus()
editor.contentComponent.requestFocus()
} else {
textArea.requestFocus()
}
removeAllDiffBlocks()
}
fun run(input: String, prefix: String, highlighted: String, suffix: String, modelTitle: String) {

View file

@ -9,19 +9,19 @@ class DiffStreamService {
fun register(handler: DiffStreamHandler, editor: Editor) {
if (handlers.containsKey(editor)) {
handlers[editor]?.reject()
handlers[editor]?.rejectAll()
}
handlers[editor] = handler
println("Registered handler for editor")
}
fun reject(editor: Editor) {
handlers[editor]?.reject()
handlers[editor]?.rejectAll()
handlers.remove(editor)
}
fun accept(editor: Editor) {
handlers[editor]?.accept()
handlers[editor]?.acceptAll()
handlers.remove(editor)
}

View file

@ -14,10 +14,8 @@ 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

View file

@ -0,0 +1,95 @@
package com.github.continuedev.continueintellijextension.editor
import com.github.continuedev.continueintellijextension.utils.getAltKeyLabel
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.ui.JBColor
import java.awt.*
import javax.swing.JButton
class VerticalDiffActionButtons(
private val editor: Editor,
private var lineNumber: Int,
private val onClickAccept: () -> Unit,
private val onClickReject: () -> Unit
) {
val acceptButton: JButton
val rejectButton: JButton
private fun removeButtons() {
editor.contentComponent.remove(acceptButton)
editor.contentComponent.remove(rejectButton)
}
init {
acceptButton = createButton("${getAltKeyLabel()}⇧Y", JBColor(0x9900FF00.toInt(), 0x9900FF00.toInt()))
rejectButton = createButton("${getAltKeyLabel()}⇧N", JBColor(0x99FF0000.toInt(), 0x99FF0000.toInt()))
acceptButton.addActionListener {
removeButtons()
onClickAccept()
}
rejectButton.addActionListener {
removeButtons()
onClickReject()
}
// Position the buttons
val visibleArea = editor.scrollingModel.visibleArea
val lineStartPosition = editor.logicalPositionToXY(LogicalPosition(lineNumber, 0))
val xPosition =
visibleArea.x + visibleArea.width - acceptButton.preferredSize.width - rejectButton.preferredSize.width - 20
val yPosition = lineStartPosition.y
acceptButton.setBounds(
xPosition,
yPosition,
acceptButton.preferredSize.width,
acceptButton.preferredSize.height
)
rejectButton.setBounds(
xPosition + acceptButton.preferredSize.width + 5,
yPosition,
rejectButton.preferredSize.width,
rejectButton.preferredSize.height
)
}
private fun createButton(text: String, backgroundColor: JBColor): JButton {
return object : JButton(text) {
override fun paintComponent(g: Graphics) {
val g2 = g.create() as Graphics2D
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.color = backgroundColor
g2.fillRoundRect(0, 0, width - 1, height - 1, 8, 8)
super.paintComponent(g2)
g2.dispose()
}
}.apply {
// This isn't working currently, font color is transparent
foreground = JBColor.WHITE
font = Font("Arial", Font.BOLD, 9)
isContentAreaFilled = false
isOpaque = false
border = null
preferredSize = Dimension(preferredSize.width, 16)
cursor = Cursor(Cursor.HAND_CURSOR)
}
}
fun updatePosition(newLineNumber: Int) {
this.lineNumber = newLineNumber
val visibleArea = editor.scrollingModel.visibleArea
val lineStartPosition = editor.logicalPositionToXY(LogicalPosition(lineNumber, 0))
val xPosition =
visibleArea.x + visibleArea.width - acceptButton.width - rejectButton.width - 40
val yPosition = lineStartPosition.y
acceptButton.location = Point(xPosition, yPosition)
rejectButton.location = Point(xPosition + acceptButton.width + 5, yPosition)
// Refresh the editor to reflect the changes
editor.component.repaint()
}
}

View file

@ -90,6 +90,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
private async insertDeletionBuffer() {
// Don't remove trailing whitespace line
const totalDeletedContent = this.deletionBuffer.join("\n");
if (
totalDeletedContent === "" &&
this.currentLineIndex >= this.endLine + this.newLinesAdded &&
@ -100,11 +101,13 @@ export class VerticalDiffHandler implements vscode.Disposable {
if (this.deletionBuffer.length || this.insertedInCurrentBlock > 0) {
const blocks = this.editorToVerticalDiffCodeLens.get(this.filepath) || [];
blocks.push({
start: this.currentLineIndex - this.insertedInCurrentBlock,
numRed: this.deletionBuffer.length,
numGreen: this.insertedInCurrentBlock,
});
this.editorToVerticalDiffCodeLens.set(this.filepath, blocks);
}
@ -118,10 +121,12 @@ export class VerticalDiffHandler implements vscode.Disposable {
this.currentLineIndex - this.insertedInCurrentBlock,
totalDeletedContent,
);
this.redDecorationManager.addLines(
this.currentLineIndex - this.insertedInCurrentBlock,
this.deletionBuffer.length,
);
// Shift green decorations downward
this.greenDecorationManager.shiftDownAfterLine(
this.currentLineIndex - this.insertedInCurrentBlock,
@ -132,9 +137,9 @@ export class VerticalDiffHandler implements vscode.Disposable {
for (let i = 0; i < this.deletionBuffer.length; i++) {
this.incrementCurrentLineIndex();
}
this.deletionBuffer = [];
this.insertedInCurrentBlock = 0;
this.refreshCodeLens();
}

View file

@ -152,7 +152,7 @@ function InputToolbar(props: InputToolbarProps) {
}
>
<span data-tooltip-id="add-codebase-context-tooltip">
@codebase
{getMetaKeyLabel()}⏎ @codebase
</span>
<ToolTip id="add-codebase-context-tooltip" place="top-end">
Submit with the codebase as context ({getMetaKeyLabel()}⏎)

File diff suppressed because it is too large Load diff