From 3078cd9280863888d62623257e17178ca8b5cf1f Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 28 Oct 2025 16:31:04 -0700 Subject: [PATCH] fix: instant reject all for VS Code --- .../vscode/src/diff/vertical/handler.ts | 66 +++++++++++++++++-- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index e6f239007ee..4420af5ec84 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -136,12 +136,7 @@ export class VerticalDiffHandler implements vscode.Disposable { // Accept all: delete all the red ranges and clear green decorations await this.deleteRangeLines(removedRanges.map((r) => r.range)); } else { - // Reject all: Re-insert red lines, delete green ones - for (const r of removedRanges) { - await this.deleteRangeLines([r.range]); - await this.insertTextAboveLine(r.range.start.line, r.line); - } - await this.deleteRangeLines(this.addedLineDecorations.ranges); + await this.unifiedRejectAll(); } this.clearDecorations(); @@ -604,6 +599,63 @@ export class VerticalDiffHandler implements vscode.Disposable { } /** - * Gets the first line number that was changed in a diff + * Rejects all diffs in a single edit operation. */ + private async unifiedRejectAll() { + await this.ensureCurrentFileIsFocused(); + + const removedRanges = this.removedLineDecorations.ranges; + const addedRanges = this.addedLineDecorations.ranges; + + interface LineOperation { + type: "removed" | "added"; + line?: string; // Only for removed lines + range: vscode.Range; + } + + const operations: LineOperation[] = []; + for (const r of removedRanges) { + operations.push({ + type: "removed", + line: r.line, + range: r.range, + }); + } + for (const range of addedRanges) { + operations.push({ + type: "added", + range, + }); + } + + operations.sort((a, b) => b.range.start.line - a.range.start.line); + + const document = this.editor.document; + const lines = document.getText().split("\n"); + + for (const op of operations) { + const lineNum = op.range.start.line; + + if (op.type === "removed") { + // Replace the placeholder line with the original content + lines[lineNum] = op.line!; + } else if (op.type === "added") { + // Delete the added lines + const startLine = op.range.start.line; + const endLine = op.range.end.line; + const numLinesToDelete = endLine - startLine + 1; + lines.splice(startLine, numLinesToDelete); + } + } + + const finalContent = lines.join("\n"); + + await this.editor.edit( + (editBuilder) => { + const fullRange = new vscode.Range(0, 0, document.lineCount, 0); + editBuilder.replace(fullRange, finalContent); + }, + { undoStopAfter: false, undoStopBefore: false }, + ); + } }