From 678bc7ae4c67440032ef2e045600e910f1196968 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 1 May 2025 21:06:40 -0700 Subject: [PATCH] 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({