From 891e8b32ff6f77cb312b4bb016350852597a4291 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 1 May 2025 16:21:43 -0700 Subject: [PATCH 1/5] red ghost text for apply --- .../vscode/src/diff/vertical/decorations.ts | 126 +++++++++++++++--- .../vscode/src/diff/vertical/handler.ts | 63 +++++---- 2 files changed, 136 insertions(+), 53 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/decorations.ts b/extensions/vscode/src/diff/vertical/decorations.ts index 5d3ae7525..c05e488e1 100644 --- a/extensions/vscode/src/diff/vertical/decorations.ts +++ b/extensions/vscode/src/diff/vertical/decorations.ts @@ -1,14 +1,19 @@ import * as vscode from "vscode"; -export const redDecorationType = vscode.window.createTextEditorDecorationType({ - isWholeLine: true, - backgroundColor: { id: "diffEditor.removedLineBackground" }, - color: "#808080", - outlineWidth: "1px", - outlineStyle: "solid", - outlineColor: { id: "diffEditor.removedTextBorder" }, - rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, -}); +export const redDecorationType = (line: string) => + vscode.window.createTextEditorDecorationType({ + isWholeLine: true, + backgroundColor: { id: "diffEditor.removedLineBackground" }, + // color: "#808080", + outlineWidth: "1px", + outlineStyle: "solid", + outlineColor: { id: "diffEditor.removedTextBorder" }, + rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, + after: { + contentText: line, + color: "#808080", + }, + }); export const greenDecorationType = vscode.window.createTextEditorDecorationType( { @@ -35,6 +40,13 @@ export const belowIndexDecorationType = rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, }); +function translateRange(range: vscode.Range, lineOffset: number): vscode.Range { + return new vscode.Range( + range.start.translate(lineOffset), + range.end.translate(lineOffset), + ); +} + export class DecorationTypeRangeManager { constructor( private decorationType: vscode.TextEditorDecorationType, @@ -82,20 +94,10 @@ export class DecorationTypeRangeManager { return this.ranges; } - private translateRange( - range: vscode.Range, - lineOffset: number, - ): vscode.Range { - return new vscode.Range( - range.start.translate(lineOffset), - range.end.translate(lineOffset), - ); - } - shiftDownAfterLine(afterLine: number, offset: number) { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].start.line >= afterLine) { - this.ranges[i] = this.translateRange(this.ranges[i], offset); + this.ranges[i] = translateRange(this.ranges[i], offset); } } this.editor.setDecorations(this.decorationType, this.ranges); @@ -109,3 +111,87 @@ export class DecorationTypeRangeManager { } } } + +export class RedDecorationsManager { + constructor(private editor: vscode.TextEditor) {} + + private ranges: { + range: vscode.Range; + decoration: vscode.TextEditorDecorationType; + }[] = []; + + applyToNewEditor(newEditor: vscode.TextEditor) { + this.editor = newEditor; + this.applyDecorations(); + } + + addLines(startIndex: number, lines: string[]) { + let i = 0; + for (const line of lines) { + this.ranges.push({ + range: new vscode.Range( + startIndex + i, + 0, + startIndex + i, + Number.MAX_SAFE_INTEGER, + ), + decoration: redDecorationType(line), + }); + i++; + } + this.applyDecorations(); + } + + addLine(index: number, line: string) { + this.addLines(index, [line]); + } + + private getDecorationsMap() { + const decorationsMap = new Map< + vscode.TextEditorDecorationType, + vscode.Range[] + >(); + for (const range of this.ranges) { + const ranges = decorationsMap.get(range.decoration) ?? []; + ranges.push(range.range); + decorationsMap.set(range.decoration, ranges); + } + return decorationsMap; + } + + applyDecorations() { + const decorationsMap = this.getDecorationsMap(); + for (const [decoration, ranges] of decorationsMap) { + this.editor.setDecorations(decoration, ranges); + } + } + + clear() { + const decorationsMap = this.getDecorationsMap(); + for (const [decoration, _] of decorationsMap) { + this.editor.setDecorations(decoration, []); + } + this.ranges = []; + } + + getRanges() { + return this.ranges.map((r) => r.range); + } + + shiftDownAfterLine(afterLine: number, offset: number) { + for (let i = 0; i < this.ranges.length; i++) { + if (this.ranges[i].range.start.line >= afterLine) { + this.ranges[i].range = translateRange(this.ranges[i].range, offset); + } + } + this.applyDecorations(); + } + + deleteRangeStartingAt(line: number) { + for (let i = 0; i < this.ranges.length; i++) { + if (this.ranges[i].range.start.line === line) { + return this.ranges.splice(i, 1)[0]; + } + } + } +} diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 2e90de6e6..2a9764bbd 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -4,10 +4,10 @@ import * as vscode from "vscode"; import { DecorationTypeRangeManager, + RedDecorationsManager, belowIndexDecorationType, greenDecorationType, indexDecorationType, - redDecorationType, } from "./decorations"; import type { ApplyState, DiffLine } from "core"; @@ -28,7 +28,7 @@ export class VerticalDiffHandler implements vscode.Disposable { private cancelled = false; private newLinesAdded = 0; private deletionBuffer: string[] = []; - private redDecorationManager: DecorationTypeRangeManager; + private redDecorationManager: RedDecorationsManager; private get fileUri() { return this.editor.document.uri.toString(); } @@ -56,10 +56,7 @@ export class VerticalDiffHandler implements vscode.Disposable { ) { this.currentLineIndex = startLine; - this.redDecorationManager = new DecorationTypeRangeManager( - redDecorationType, - this.editor, - ); + this.redDecorationManager = new RedDecorationsManager(this.editor); this.greenDecorationManager = new DecorationTypeRangeManager( greenDecorationType, @@ -86,8 +83,6 @@ export class VerticalDiffHandler implements vscode.Disposable { } private async insertDeletionBuffer() { - const totalDeletedContent = this.deletionBuffer.join("\n"); - if (this.deletionBuffer.length || this.insertedInCurrentBlock > 0) { const blocks = this.editorToVerticalDiffCodeLens.get(this.fileUri) || []; @@ -105,15 +100,15 @@ export class VerticalDiffHandler implements vscode.Disposable { return; } - // Insert the block of deleted lines + // Insert the block of deleted lines as empty new lines await this.insertTextAboveLine( this.currentLineIndex - this.insertedInCurrentBlock, - totalDeletedContent, + "\n".repeat(this.deletionBuffer.length), ); this.redDecorationManager.addLines( this.currentLineIndex - this.insertedInCurrentBlock, - this.deletionBuffer.length, + this.deletionBuffer, ); // Shift green decorations downward @@ -215,21 +210,21 @@ export class VerticalDiffHandler implements vscode.Disposable { this.editor.setDecorations(indexDecorationType, []); } - public getLineDeltaBeforeLine(line: number) { - // Returns the number of lines removed from a file when the diff currently active is closed - let totalLineDelta = 0; - for (const range of this.greenDecorationManager - .getRanges() - .sort((a, b) => a.start.line - b.start.line)) { - if (range.start.line > line) { - break; - } + // public getLineDeltaBeforeLine(line: number) { + // // Returns the number of lines removed from a file when the diff currently active is closed + // let totalLineDelta = 0; + // for (const range of this.greenDecorationManager + // .getRanges() + // .sort((a, b) => a.start.line - b.start.line)) { + // if (range.start.line > line) { + // break; + // } - totalLineDelta -= range.end.line - range.start.line + 1; - } + // totalLineDelta -= range.end.line - range.start.line + 1; + // } - return totalLineDelta; - } + // return totalLineDelta; + // } async clear(accept: boolean) { vscode.commands.executeCommand( @@ -500,15 +495,17 @@ export class VerticalDiffHandler implements vscode.Disposable { // ["A"] => "A" and ["A", ""] => "A\n" to be the same single line. // "A\n" and "A\n\n" are unambiguous. // - const oldFileContent = diffLines - .filter((line) => line.type === "same" || line.type === "old") - .map((line) => line.line) - .join("\n") + "\n"; + const oldFileContent = + diffLines + .filter((line) => line.type === "same" || line.type === "old") + .map((line) => line.line) + .join("\n") + "\n"; - const newFileContent = diffLines - .filter((line) => line.type === "same" || line.type === "new") - .map((line) => line.line) - .join("\n") + "\n"; + const newFileContent = + diffLines + .filter((line) => line.type === "same" || line.type === "new") + .map((line) => line.line) + .join("\n") + "\n"; const diffs = myersDiff(oldFileContent, newFileContent); @@ -531,7 +528,7 @@ export class VerticalDiffHandler implements vscode.Disposable { diffs.forEach((diff, index) => { if (diff.type === "old") { - this.redDecorationManager.addLine(this.startLine + index); + this.redDecorationManager.addLine(this.startLine + index, diff.line); numRed++; } else if (diff.type === "new") { this.greenDecorationManager.addLine(this.startLine + index); From 678bc7ae4c67440032ef2e045600e910f1196968 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 1 May 2025 21:06:40 -0700 Subject: [PATCH 2/5] sensible renames --- .../vscode/src/diff/vertical/decorations.ts | 83 ++++++------ .../vscode/src/diff/vertical/handler.ts | 118 ++++++++++-------- 2 files changed, 106 insertions(+), 95 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/decorations.ts b/extensions/vscode/src/diff/vertical/decorations.ts index c05e488e1..f7c09f26d 100644 --- a/extensions/vscode/src/diff/vertical/decorations.ts +++ b/extensions/vscode/src/diff/vertical/decorations.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode"; -export const redDecorationType = (line: string) => +const removedLineDecorationType = (line: string) => vscode.window.createTextEditorDecorationType({ isWholeLine: true, backgroundColor: { id: "diffEditor.removedLineBackground" }, @@ -12,19 +12,19 @@ export const redDecorationType = (line: string) => after: { contentText: line, color: "#808080", + textDecoration: "none; white-space: pre", }, + textDecoration: "none; display: none", }); -export const greenDecorationType = vscode.window.createTextEditorDecorationType( - { - isWholeLine: true, - backgroundColor: { id: "diffEditor.insertedLineBackground" }, - outlineWidth: "1px", - outlineStyle: "solid", - outlineColor: { id: "diffEditor.insertedTextBorder" }, - rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, - }, -); +const addedLineDecorationType = vscode.window.createTextEditorDecorationType({ + isWholeLine: true, + backgroundColor: { id: "diffEditor.insertedLineBackground" }, + outlineWidth: "1px", + outlineStyle: "solid", + outlineColor: { id: "diffEditor.insertedTextBorder" }, + rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, +}); export const indexDecorationType = vscode.window.createTextEditorDecorationType( { @@ -47,13 +47,11 @@ function translateRange(range: vscode.Range, lineOffset: number): vscode.Range { ); } -export class DecorationTypeRangeManager { - constructor( - private decorationType: vscode.TextEditorDecorationType, - private editor: vscode.TextEditor, - ) {} +export class AddedLineDecorationManager { + constructor(private editor: vscode.TextEditor) {} private ranges: vscode.Range[] = []; + decorationType = addedLineDecorationType; applyToNewEditor(newEditor: vscode.TextEditor) { this.editor = newEditor; @@ -109,13 +107,15 @@ export class DecorationTypeRangeManager { return this.ranges.splice(i, 1)[0]; } } + this.editor.setDecorations(this.decorationType, this.ranges); } } -export class RedDecorationsManager { +export class RemovedLineDecorationManager { constructor(private editor: vscode.TextEditor) {} private ranges: { + line: string; range: vscode.Range; decoration: vscode.TextEditorDecorationType; }[] = []; @@ -129,13 +129,14 @@ export class RedDecorationsManager { let i = 0; for (const line of lines) { this.ranges.push({ + line, range: new vscode.Range( startIndex + i, 0, startIndex + i, Number.MAX_SAFE_INTEGER, ), - decoration: redDecorationType(line), + decoration: removedLineDecorationType(line), }); i++; } @@ -146,36 +147,22 @@ export class RedDecorationsManager { this.addLines(index, [line]); } - private getDecorationsMap() { - const decorationsMap = new Map< - vscode.TextEditorDecorationType, - vscode.Range[] - >(); - for (const range of this.ranges) { - const ranges = decorationsMap.get(range.decoration) ?? []; - ranges.push(range.range); - decorationsMap.set(range.decoration, ranges); - } - return decorationsMap; - } - applyDecorations() { - const decorationsMap = this.getDecorationsMap(); - for (const [decoration, ranges] of decorationsMap) { - this.editor.setDecorations(decoration, ranges); - } + this.ranges.forEach((r) => { + this.editor.setDecorations(r.decoration, [r.range]); + }); } + // Red decorations are always unique, so we'll always dispose clear() { - const decorationsMap = this.getDecorationsMap(); - for (const [decoration, _] of decorationsMap) { - this.editor.setDecorations(decoration, []); - } + this.ranges.forEach((r) => { + r.decoration.dispose(); + }); this.ranges = []; } getRanges() { - return this.ranges.map((r) => r.range); + return this.ranges; } shiftDownAfterLine(afterLine: number, offset: number) { @@ -187,10 +174,22 @@ export class RedDecorationsManager { this.applyDecorations(); } - deleteRangeStartingAt(line: number) { + // Red ranges are always single-line, so to delete group, delete sequential ranges + deleteRangesStartingAt(line: number) { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].range.start.line === line) { - return this.ranges.splice(i, 1)[0]; + this.ranges[i].decoration.dispose(); + // Find how many sequential ranges we have starting from this line + let count = 1; + while ( + i + count < this.ranges.length && + this.ranges[i + count].range.start.line === line + count + ) { + count++; + this.ranges[i + count].decoration.dispose(); + } + // Remove all sequential ranges and return them + return this.ranges.splice(i, count); } } } diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 2a9764bbd..9adec095d 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -3,10 +3,9 @@ import * as URI from "uri-js"; import * as vscode from "vscode"; import { - DecorationTypeRangeManager, - RedDecorationsManager, + AddedLineDecorationManager, + RemovedLineDecorationManager, belowIndexDecorationType, - greenDecorationType, indexDecorationType, } from "./decorations"; @@ -28,7 +27,9 @@ export class VerticalDiffHandler implements vscode.Disposable { private cancelled = false; private newLinesAdded = 0; private deletionBuffer: string[] = []; - private redDecorationManager: RedDecorationsManager; + private removedLineDecorations: RemovedLineDecorationManager; + private addedLineDecorations: AddedLineDecorationManager; + private get fileUri() { return this.editor.document.uri.toString(); } @@ -56,12 +57,8 @@ export class VerticalDiffHandler implements vscode.Disposable { ) { this.currentLineIndex = startLine; - this.redDecorationManager = new RedDecorationsManager(this.editor); - - this.greenDecorationManager = new DecorationTypeRangeManager( - greenDecorationType, - this.editor, - ); + this.removedLineDecorations = new RemovedLineDecorationManager(this.editor); + this.addedLineDecorations = new AddedLineDecorationManager(this.editor); const disposable = vscode.window.onDidChangeActiveTextEditor((editor) => { if (!editor) { @@ -70,8 +67,8 @@ export class VerticalDiffHandler implements vscode.Disposable { // When we switch away and back to this editor, need to re-draw decorations if (URI.equal(editor.document.uri.toString(), this.fileUri)) { this.editor = editor; - this.redDecorationManager.applyToNewEditor(editor); - this.greenDecorationManager.applyToNewEditor(editor); + this.removedLineDecorations.applyToNewEditor(editor); + this.addedLineDecorations.applyToNewEditor(editor); this.updateIndexLineDecorations(); this.refreshCodeLens(); @@ -103,16 +100,16 @@ export class VerticalDiffHandler implements vscode.Disposable { // Insert the block of deleted lines as empty new lines await this.insertTextAboveLine( this.currentLineIndex - this.insertedInCurrentBlock, - "\n".repeat(this.deletionBuffer.length), + "\n".repeat(this.deletionBuffer.length - 1), ); - this.redDecorationManager.addLines( + this.removedLineDecorations.addLines( this.currentLineIndex - this.insertedInCurrentBlock, this.deletionBuffer, ); // Shift green decorations downward - this.greenDecorationManager.shiftDownAfterLine( + this.addedLineDecorations.shiftDownAfterLine( this.currentLineIndex - this.insertedInCurrentBlock, this.deletionBuffer.length, ); @@ -132,8 +129,6 @@ export class VerticalDiffHandler implements vscode.Disposable { this.updateIndexLineDecorations(); } - private greenDecorationManager: DecorationTypeRangeManager; - private async insertTextAboveLine(index: number, text: string) { await this.editor.edit( (editBuilder) => { @@ -160,7 +155,7 @@ export class VerticalDiffHandler implements vscode.Disposable { private async insertLineAboveIndex(index: number, line: string) { await this.insertTextAboveLine(index, line); - this.greenDecorationManager.addLine(index); + this.addedLineDecorations.addLine(index); this.newLinesAdded++; } @@ -213,7 +208,7 @@ export class VerticalDiffHandler implements vscode.Disposable { // public getLineDeltaBeforeLine(line: number) { // // Returns the number of lines removed from a file when the diff currently active is closed // let totalLineDelta = 0; - // for (const range of this.greenDecorationManager + // for (const range of this.addedLineDecorations // .getRanges() // .sort((a, b) => a.start.line - b.start.line)) { // if (range.start.line > line) { @@ -232,29 +227,43 @@ export class VerticalDiffHandler implements vscode.Disposable { "continue.streamingDiff", false, ); - const rangesToDelete = accept - ? this.redDecorationManager.getRanges() - : this.greenDecorationManager.getRanges(); + + const redRanges = this.removedLineDecorations.getRanges(); + + const deleteRangeLines = async (ranges: vscode.Range[]) => { + await this.editor.edit( + (editBuilder) => { + for (const range of ranges) { + editBuilder.delete( + new vscode.Range( + range.start, + new vscode.Position(range.end.line + 1, 0), + ), + ); + } + }, + { + undoStopAfter: false, + undoStopBefore: false, + }, + ); + }; + + if (accept) { + // Accept all: delete all the red ranges and clear green decorations + await deleteRangeLines(redRanges.map((r) => r.range)); + } else { + // Reject all: Re-insert red lines, delete green ones + for (const r of redRanges) { + await deleteRangeLines([r.range]); + await this.insertTextAboveLine(r.range.start.line, r.line); + } + const greenRanges = this.addedLineDecorations.getRanges(); + await deleteRangeLines(greenRanges); + } this.clearDecorations(); - await this.editor.edit( - (editBuilder) => { - for (const range of rangesToDelete) { - editBuilder.delete( - new vscode.Range( - range.start, - new vscode.Position(range.end.line + 1, 0), - ), - ); - } - }, - { - undoStopAfter: false, - undoStopBefore: false, - }, - ); - this.options.onStatusUpdate( "closed", this.editorToVerticalDiffCodeLens.get(this.fileUri)?.length ?? 0, @@ -376,7 +385,7 @@ export class VerticalDiffHandler implements vscode.Disposable { ) { if (numGreen > 0) { // Delete the editor decoration - this.greenDecorationManager.deleteRangeStartingAt(startLine + numRed); + this.addedLineDecorations.deleteRangeStartingAt(startLine + numRed); if (!accept) { // Delete the actual lines await this.deleteLinesAt(startLine + numRed, numGreen); @@ -384,20 +393,23 @@ export class VerticalDiffHandler implements vscode.Disposable { } if (numRed > 0) { - const rangeToDelete = - this.redDecorationManager.deleteRangeStartingAt(startLine); + const deleted = + this.removedLineDecorations.deleteRangesStartingAt(startLine); - if (accept) { - // Delete the actual lines - await this.deleteLinesAt(startLine, numRed); + await this.deleteLinesAt(startLine, numRed); + if (deleted && !accept) { + await this.insertTextAboveLine( + startLine, + deleted.map((r) => r.line).join("\n"), + ); } } // Shift everything below upward const offset = -(accept ? numRed : numGreen); - this.redDecorationManager.shiftDownAfterLine(startLine, offset); - this.greenDecorationManager.shiftDownAfterLine(startLine, offset); + this.removedLineDecorations.shiftDownAfterLine(startLine, offset); + this.addedLineDecorations.shiftDownAfterLine(startLine, offset); // Shift the codelens objects this.shiftCodeLensObjects(startLine, offset); @@ -445,8 +457,8 @@ export class VerticalDiffHandler implements vscode.Disposable { } //update decorations - this.redDecorationManager.shiftDownAfterLine(startLine, lineDelta); - this.greenDecorationManager.shiftDownAfterLine(startLine, lineDelta); + this.removedLineDecorations.shiftDownAfterLine(startLine, lineDelta); + this.addedLineDecorations.shiftDownAfterLine(startLine, lineDelta); //update code lens this.shiftCodeLensObjects(startLine, lineDelta); @@ -458,8 +470,8 @@ export class VerticalDiffHandler implements vscode.Disposable { } clearDecorations() { - this.redDecorationManager.clear(); - this.greenDecorationManager.clear(); + this.removedLineDecorations.clear(); + this.addedLineDecorations.clear(); this.clearIndexLineDecorations(); this.editorToVerticalDiffCodeLens.delete(this.fileUri); this.refreshCodeLens(); @@ -528,10 +540,10 @@ export class VerticalDiffHandler implements vscode.Disposable { diffs.forEach((diff, index) => { if (diff.type === "old") { - this.redDecorationManager.addLine(this.startLine + index, diff.line); + this.removedLineDecorations.addLine(this.startLine + index, diff.line); numRed++; } else if (diff.type === "new") { - this.greenDecorationManager.addLine(this.startLine + index); + this.addedLineDecorations.addLine(this.startLine + index); numGreen++; } else if (diff.type === "same" && (numRed > 0 || numGreen > 0)) { codeLensBlocks.push({ From 61132a9acfa029150358812fd1390f9e31fab791 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 1 May 2025 21:17:59 -0700 Subject: [PATCH 3/5] working diff lines, clean up --- .../vscode/src/diff/vertical/decorations.ts | 24 ++++++++----------- .../vscode/src/diff/vertical/handler.ts | 10 ++++---- .../vscode/src/diff/vertical/manager.ts | 4 ++-- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/decorations.ts b/extensions/vscode/src/diff/vertical/decorations.ts index f7c09f26d..02f5daca4 100644 --- a/extensions/vscode/src/diff/vertical/decorations.ts +++ b/extensions/vscode/src/diff/vertical/decorations.ts @@ -4,7 +4,6 @@ const removedLineDecorationType = (line: string) => vscode.window.createTextEditorDecorationType({ isWholeLine: true, backgroundColor: { id: "diffEditor.removedLineBackground" }, - // color: "#808080", outlineWidth: "1px", outlineStyle: "solid", outlineColor: { id: "diffEditor.removedTextBorder" }, @@ -14,6 +13,9 @@ const removedLineDecorationType = (line: string) => color: "#808080", textDecoration: "none; white-space: pre", }, + // NOTE this has the effect of hiding text the user enters into a red line, which may cause linting errors + // But probably worth saving the ugly effect of having the ghost text after entered text + // And resolved upon accept/reject when line deleted anyways textDecoration: "none; display: none", }); @@ -47,10 +49,11 @@ function translateRange(range: vscode.Range, lineOffset: number): vscode.Range { ); } +// Class for managing highlight decorations for added lines (e.g. GREEN) export class AddedLineDecorationManager { constructor(private editor: vscode.TextEditor) {} - private ranges: vscode.Range[] = []; + ranges: vscode.Range[] = []; decorationType = addedLineDecorationType; applyToNewEditor(newEditor: vscode.TextEditor) { @@ -88,10 +91,6 @@ export class AddedLineDecorationManager { this.editor.setDecorations(this.decorationType, this.ranges); } - getRanges() { - return this.ranges; - } - shiftDownAfterLine(afterLine: number, offset: number) { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].start.line >= afterLine) { @@ -111,10 +110,13 @@ export class AddedLineDecorationManager { } } +// Class for managing ghost-text decorations for removed lines (e.g. RED) +// Behavior is slightly different all around +// because each line will have a unique decoration type export class RemovedLineDecorationManager { constructor(private editor: vscode.TextEditor) {} - private ranges: { + ranges: { line: string; range: vscode.Range; decoration: vscode.TextEditorDecorationType; @@ -153,7 +155,7 @@ export class RemovedLineDecorationManager { }); } - // Red decorations are always unique, so we'll always dispose + // Removed decorations are always unique, so we'll always dispose clear() { this.ranges.forEach((r) => { r.decoration.dispose(); @@ -161,10 +163,6 @@ export class RemovedLineDecorationManager { this.ranges = []; } - getRanges() { - return this.ranges; - } - shiftDownAfterLine(afterLine: number, offset: number) { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].range.start.line >= afterLine) { @@ -179,7 +177,6 @@ export class RemovedLineDecorationManager { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].range.start.line === line) { this.ranges[i].decoration.dispose(); - // Find how many sequential ranges we have starting from this line let count = 1; while ( i + count < this.ranges.length && @@ -188,7 +185,6 @@ export class RemovedLineDecorationManager { count++; this.ranges[i + count].decoration.dispose(); } - // Remove all sequential ranges and return them return this.ranges.splice(i, count); } } diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 9adec095d..4f1635926 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -228,8 +228,6 @@ export class VerticalDiffHandler implements vscode.Disposable { false, ); - const redRanges = this.removedLineDecorations.getRanges(); - const deleteRangeLines = async (ranges: vscode.Range[]) => { await this.editor.edit( (editBuilder) => { @@ -249,17 +247,17 @@ export class VerticalDiffHandler implements vscode.Disposable { ); }; + const removedRanges = this.removedLineDecorations.ranges; if (accept) { // Accept all: delete all the red ranges and clear green decorations - await deleteRangeLines(redRanges.map((r) => r.range)); + await deleteRangeLines(removedRanges.map((r) => r.range)); } else { // Reject all: Re-insert red lines, delete green ones - for (const r of redRanges) { + for (const r of removedRanges) { await deleteRangeLines([r.range]); await this.insertTextAboveLine(r.range.start.line, r.line); } - const greenRanges = this.addedLineDecorations.getRanges(); - await deleteRangeLines(greenRanges); + await deleteRangeLines(this.addedLineDecorations.ranges); } this.clearDecorations(); diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index 6c94ab343..45ce0736c 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -344,11 +344,11 @@ export class VerticalDiffManager { // startLine += effectiveLineDelta; // endLine += effectiveLineDelta; - existingHandler.clear(false); + await existingHandler.clear(false); } await new Promise((resolve) => { - setTimeout(resolve, 200); + setTimeout(resolve, 150); }); // Create new handler with determined start/end From 71685897a87ceff868a375ff60841a63dce9535a Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 1 May 2025 21:37:52 -0700 Subject: [PATCH 4/5] myers cleanup --- .../vscode/src/diff/vertical/decorations.ts | 13 ++++++------- extensions/vscode/src/diff/vertical/handler.ts | 17 +++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/decorations.ts b/extensions/vscode/src/diff/vertical/decorations.ts index 02f5daca4..2b3b15cca 100644 --- a/extensions/vscode/src/diff/vertical/decorations.ts +++ b/extensions/vscode/src/diff/vertical/decorations.ts @@ -176,16 +176,15 @@ export class RemovedLineDecorationManager { deleteRangesStartingAt(line: number) { for (let i = 0; i < this.ranges.length; i++) { if (this.ranges[i].range.start.line === line) { - this.ranges[i].decoration.dispose(); - let count = 1; + let sequential = 0; while ( - i + count < this.ranges.length && - this.ranges[i + count].range.start.line === line + count + i + sequential < this.ranges.length && + this.ranges[i + sequential].range.start.line === line + sequential ) { - count++; - this.ranges[i + count].decoration.dispose(); + this.ranges[i + sequential].decoration.dispose(); + sequential++; } - return this.ranges.splice(i, count); + return this.ranges.splice(i, sequential); } } } diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 4f1635926..5ba06f014 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -454,11 +454,11 @@ export class VerticalDiffHandler implements vscode.Disposable { return; } - //update decorations + // Update decorations this.removedLineDecorations.shiftDownAfterLine(startLine, lineDelta); this.addedLineDecorations.shiftDownAfterLine(startLine, lineDelta); - //update code lens + // Update code lens this.shiftCodeLensObjects(startLine, lineDelta); } @@ -504,7 +504,6 @@ export class VerticalDiffHandler implements vscode.Disposable { // newline separated, because myersDiff() would consider // ["A"] => "A" and ["A", ""] => "A\n" to be the same single line. // "A\n" and "A\n\n" are unambiguous. - // const oldFileContent = diffLines .filter((line) => line.type === "same" || line.type === "old") @@ -517,13 +516,15 @@ export class VerticalDiffHandler implements vscode.Disposable { .map((line) => line.line) .join("\n") + "\n"; - const diffs = myersDiff(oldFileContent, newFileContent); + const myersDiffs = myersDiff(oldFileContent, newFileContent); - const myersDiffLines = diffs.map((diff) => diff.line).join("\n"); + const replaceContent = myersDiffs + .map((diff) => (diff.type === "old" ? "" : diff.line)) + .join("\n"); // Then, we insert our diff lines await this.editor.edit((editBuilder) => { - editBuilder.replace(this.range, myersDiffLines), + editBuilder.replace(this.range, replaceContent), { undoStopAfter: false, undoStopBefore: false, @@ -536,7 +537,7 @@ export class VerticalDiffHandler implements vscode.Disposable { const codeLensBlocks: VerticalDiffCodeLens[] = []; - diffs.forEach((diff, index) => { + myersDiffs.forEach((diff, index) => { if (diff.type === "old") { this.removedLineDecorations.addLine(this.startLine + index, diff.line); numRed++; @@ -558,7 +559,7 @@ export class VerticalDiffHandler implements vscode.Disposable { codeLensBlocks.push({ numGreen, numRed, - start: this.startLine + diffs.length - numRed - numGreen, + start: this.startLine + myersDiffs.length - numRed - numGreen, }); } From b6ec53d76927201a769c68ff205bd5209caa9742 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 2 May 2025 18:07:15 -0700 Subject: [PATCH 5/5] patrick requested changes - remove in-block function etc --- .../vscode/src/diff/vertical/handler.ts | 60 +++++++------------ 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 5ba06f014..6a4e36241 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -174,6 +174,25 @@ export class VerticalDiffHandler implements vscode.Disposable { ); } + private async deleteRangeLines(ranges: vscode.Range[]) { + await this.editor.edit( + (editBuilder) => { + for (const range of ranges) { + editBuilder.delete( + new vscode.Range( + range.start, + new vscode.Position(range.end.line + 1, 0), + ), + ); + } + }, + { + undoStopAfter: false, + undoStopBefore: false, + }, + ); + } + private updateIndexLineDecorations() { if (this.options.instant) { // We don't show progress on instant apply @@ -205,22 +224,6 @@ export class VerticalDiffHandler implements vscode.Disposable { this.editor.setDecorations(indexDecorationType, []); } - // public getLineDeltaBeforeLine(line: number) { - // // Returns the number of lines removed from a file when the diff currently active is closed - // let totalLineDelta = 0; - // for (const range of this.addedLineDecorations - // .getRanges() - // .sort((a, b) => a.start.line - b.start.line)) { - // if (range.start.line > line) { - // break; - // } - - // totalLineDelta -= range.end.line - range.start.line + 1; - // } - - // return totalLineDelta; - // } - async clear(accept: boolean) { vscode.commands.executeCommand( "setContext", @@ -228,36 +231,17 @@ export class VerticalDiffHandler implements vscode.Disposable { false, ); - const deleteRangeLines = async (ranges: vscode.Range[]) => { - await this.editor.edit( - (editBuilder) => { - for (const range of ranges) { - editBuilder.delete( - new vscode.Range( - range.start, - new vscode.Position(range.end.line + 1, 0), - ), - ); - } - }, - { - undoStopAfter: false, - undoStopBefore: false, - }, - ); - }; - const removedRanges = this.removedLineDecorations.ranges; if (accept) { // Accept all: delete all the red ranges and clear green decorations - await deleteRangeLines(removedRanges.map((r) => r.range)); + await this.deleteRangeLines(removedRanges.map((r) => r.range)); } else { // Reject all: Re-insert red lines, delete green ones for (const r of removedRanges) { - await deleteRangeLines([r.range]); + await this.deleteRangeLines([r.range]); await this.insertTextAboveLine(r.range.start.line, r.line); } - await deleteRangeLines(this.addedLineDecorations.ranges); + await this.deleteRangeLines(this.addedLineDecorations.ranges); } this.clearDecorations();