diff --git a/extensions/vscode/src/diff/vertical/decorations.ts b/extensions/vscode/src/diff/vertical/decorations.ts index 5d3ae7525..2b3b15cca 100644 --- a/extensions/vscode/src/diff/vertical/decorations.ts +++ b/extensions/vscode/src/diff/vertical/decorations.ts @@ -1,25 +1,32 @@ 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 greenDecorationType = vscode.window.createTextEditorDecorationType( - { +const removedLineDecorationType = (line: string) => + vscode.window.createTextEditorDecorationType({ isWholeLine: true, - backgroundColor: { id: "diffEditor.insertedLineBackground" }, + backgroundColor: { id: "diffEditor.removedLineBackground" }, outlineWidth: "1px", outlineStyle: "solid", - outlineColor: { id: "diffEditor.insertedTextBorder" }, + outlineColor: { id: "diffEditor.removedTextBorder" }, rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, - }, -); + after: { + contentText: line, + 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", + }); + +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( { @@ -35,13 +42,19 @@ export const belowIndexDecorationType = rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, }); -export class DecorationTypeRangeManager { - constructor( - private decorationType: vscode.TextEditorDecorationType, - private editor: vscode.TextEditor, - ) {} +function translateRange(range: vscode.Range, lineOffset: number): vscode.Range { + return new vscode.Range( + range.start.translate(lineOffset), + range.end.translate(lineOffset), + ); +} - private ranges: vscode.Range[] = []; +// Class for managing highlight decorations for added lines (e.g. GREEN) +export class AddedLineDecorationManager { + constructor(private editor: vscode.TextEditor) {} + + ranges: vscode.Range[] = []; + decorationType = addedLineDecorationType; applyToNewEditor(newEditor: vscode.TextEditor) { this.editor = newEditor; @@ -78,24 +91,10 @@ export class DecorationTypeRangeManager { this.editor.setDecorations(this.decorationType, this.ranges); } - getRanges() { - 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); @@ -107,5 +106,86 @@ export class DecorationTypeRangeManager { return this.ranges.splice(i, 1)[0]; } } + this.editor.setDecorations(this.decorationType, this.ranges); + } +} + +// 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) {} + + ranges: { + line: string; + 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({ + line, + range: new vscode.Range( + startIndex + i, + 0, + startIndex + i, + Number.MAX_SAFE_INTEGER, + ), + decoration: removedLineDecorationType(line), + }); + i++; + } + this.applyDecorations(); + } + + addLine(index: number, line: string) { + this.addLines(index, [line]); + } + + applyDecorations() { + this.ranges.forEach((r) => { + this.editor.setDecorations(r.decoration, [r.range]); + }); + } + + // Removed decorations are always unique, so we'll always dispose + clear() { + this.ranges.forEach((r) => { + r.decoration.dispose(); + }); + this.ranges = []; + } + + 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(); + } + + // 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) { + let sequential = 0; + while ( + i + sequential < this.ranges.length && + this.ranges[i + sequential].range.start.line === line + sequential + ) { + this.ranges[i + sequential].decoration.dispose(); + sequential++; + } + 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 2e90de6e6..6a4e36241 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -3,11 +3,10 @@ import * as URI from "uri-js"; import * as vscode from "vscode"; import { - DecorationTypeRangeManager, + AddedLineDecorationManager, + RemovedLineDecorationManager, belowIndexDecorationType, - greenDecorationType, indexDecorationType, - redDecorationType, } from "./decorations"; import type { ApplyState, DiffLine } from "core"; @@ -28,7 +27,9 @@ export class VerticalDiffHandler implements vscode.Disposable { private cancelled = false; private newLinesAdded = 0; private deletionBuffer: string[] = []; - private redDecorationManager: DecorationTypeRangeManager; + private removedLineDecorations: RemovedLineDecorationManager; + private addedLineDecorations: AddedLineDecorationManager; + private get fileUri() { return this.editor.document.uri.toString(); } @@ -56,15 +57,8 @@ export class VerticalDiffHandler implements vscode.Disposable { ) { this.currentLineIndex = startLine; - this.redDecorationManager = new DecorationTypeRangeManager( - redDecorationType, - 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) { @@ -73,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(); @@ -86,8 +80,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,19 +97,19 @@ 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 - 1), ); - this.redDecorationManager.addLines( + this.removedLineDecorations.addLines( this.currentLineIndex - this.insertedInCurrentBlock, - this.deletionBuffer.length, + this.deletionBuffer, ); // Shift green decorations downward - this.greenDecorationManager.shiftDownAfterLine( + this.addedLineDecorations.shiftDownAfterLine( this.currentLineIndex - this.insertedInCurrentBlock, this.deletionBuffer.length, ); @@ -137,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) => { @@ -165,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++; } @@ -184,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 @@ -215,51 +224,28 @@ 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; - } - - totalLineDelta -= range.end.line - range.start.line + 1; - } - - return totalLineDelta; - } - async clear(accept: boolean) { vscode.commands.executeCommand( "setContext", "continue.streamingDiff", false, ); - const rangesToDelete = accept - ? this.redDecorationManager.getRanges() - : this.greenDecorationManager.getRanges(); + + const removedRanges = this.removedLineDecorations.ranges; + if (accept) { + // 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); + } 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, @@ -381,7 +367,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); @@ -389,20 +375,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); @@ -449,11 +438,11 @@ export class VerticalDiffHandler implements vscode.Disposable { return; } - //update decorations - this.redDecorationManager.shiftDownAfterLine(startLine, lineDelta); - this.greenDecorationManager.shiftDownAfterLine(startLine, lineDelta); + // Update decorations + this.removedLineDecorations.shiftDownAfterLine(startLine, lineDelta); + this.addedLineDecorations.shiftDownAfterLine(startLine, lineDelta); - //update code lens + // Update code lens this.shiftCodeLensObjects(startLine, lineDelta); } @@ -463,8 +452,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(); @@ -499,24 +488,27 @@ 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") - .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); + 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, @@ -529,12 +521,12 @@ export class VerticalDiffHandler implements vscode.Disposable { const codeLensBlocks: VerticalDiffCodeLens[] = []; - diffs.forEach((diff, index) => { + myersDiffs.forEach((diff, index) => { if (diff.type === "old") { - this.redDecorationManager.addLine(this.startLine + index); + 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({ @@ -551,7 +543,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, }); } diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index ca653205d..bb09ac588 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -349,11 +349,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