From 92a38ccf5f0639d815684bd786f75ebede9e950c Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 2 Jan 2025 23:24:32 -0600 Subject: [PATCH 1/2] bugfix: handle inline edit w/ myers diff --- .../vscode/src/diff/vertical/handler.ts | 73 ++++++++++--------- .../vscode/src/diff/vertical/manager.ts | 2 +- .../VerticalPerLineCodeLensProvider.ts | 9 --- 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index a99c9c1c33..80331f8059 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -343,6 +343,7 @@ export class VerticalDiffHandler implements vscode.Disposable { async run(diffLineGenerator: AsyncGenerator) { let diffLines = []; + try { // As an indicator of loading this.updateIndexLineDecorations(); @@ -481,9 +482,17 @@ export class VerticalDiffHandler implements vscode.Disposable { * we have received all of the diff lines. */ async reapplyWithMeyersDiff(diffLines: DiffLine[]) { + // First, we reset the original diff by deleting any new lines and clearing decorations + for (const range of this.greenDecorationManager.getRanges()) { + await this.deleteLinesAt( + range.start.line, + range.end.line - range.start.line + 1, + ); + } + this.clearDecorations(); - // First, get our old/new file content based on the diff lines + // Then, get our old/new file content based on the original lines const oldFileContent = diffLines .filter((line) => line.type === "same" || line.type === "old") .map((line) => line.line) @@ -497,55 +506,49 @@ export class VerticalDiffHandler implements vscode.Disposable { const diffs = myersDiff(oldFileContent, newFileContent); const meyersDiffLines = diffs.map((diff) => diff.line).join("\n"); - console.log({ newFileContent }); - - // Then, we overwrite the potentially miscalcualted diff blocks with our Meyers Diff - const fullRange = new vscode.Range( - this.editor.document.positionAt(0), - this.editor.document.positionAt(this.editor.document.getText().length), - ); + // Then, we insert our diff lines await this.editor.edit((editBuilder) => { - editBuilder.replace(fullRange, meyersDiffLines); + editBuilder.replace(this.range, meyersDiffLines), + { + undoStopAfter: false, + undoStopBefore: false, + }; }); - let currentBlock = { - start: 0, - numRed: 0, - numGreen: 0, - }; + // Lastly, we apply decorations + let numRed = 0; + let numGreen = 0; - const blocks: VerticalDiffCodeLens[] = []; + const codeLensBlocks: VerticalDiffCodeLens[] = []; - // Lastly, we apply decorations diffs.forEach((diff, index) => { if (diff.type === "old") { - this.redDecorationManager.addLine(index); - currentBlock.numRed++; + this.redDecorationManager.addLine(this.startLine + index); + numRed++; } else if (diff.type === "new") { - this.greenDecorationManager.addLine(index); - currentBlock.numGreen++; - } else if ( - diff.type === "same" && - (currentBlock.numRed > 0 || currentBlock.numGreen > 0) - ) { - blocks.push({ - ...currentBlock, - start: index - currentBlock.numRed - currentBlock.numGreen, + this.greenDecorationManager.addLine(this.startLine + index); + numGreen++; + } else if (diff.type === "same" && (numRed > 0 || numGreen > 0)) { + codeLensBlocks.push({ + numRed, + numGreen, + start: this.startLine + index - numRed - numGreen, }); - currentBlock = { - start: index + 1, - numRed: 0, - numGreen: 0, - }; + numRed = 0; + numGreen = 0; } }); - if (currentBlock.numRed > 0 || currentBlock.numGreen > 0) { - blocks.push(currentBlock); + if (numRed > 0 || numGreen > 0) { + codeLensBlocks.push({ + numGreen, + numRed, + start: this.startLine + diffs.length - 1 - numRed - numGreen, + }); } - this.editorToVerticalDiffCodeLens.set(this.fileUri, blocks); + this.editorToVerticalDiffCodeLens.set(this.fileUri, codeLensBlocks); this.refreshCodeLens(); } } diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index ee47fd1732..1e1b41a173 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -3,13 +3,13 @@ import { ConfigHandler } from "core/config/ConfigHandler"; import { streamDiffLines } from "core/edit/streamDiffLines"; import { pruneLinesFromBottom, pruneLinesFromTop } from "core/llm/countTokens"; import { getMarkdownLanguageTagForFile } from "core/util"; +import * as URI from "uri-js"; import * as vscode from "vscode"; import EditDecorationManager from "../../quickEdit/EditDecorationManager"; import { VsCodeWebviewProtocol } from "../../webviewProtocol"; import { VerticalDiffHandler, VerticalDiffHandlerOptions } from "./handler"; -import * as URI from "uri-js"; export interface VerticalDiffCodeLens { start: number; diff --git a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts index 52fd07f793..43c9015f19 100644 --- a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts +++ b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts @@ -53,15 +53,6 @@ export class VerticalDiffCodeLensProvider implements vscode.CodeLensProvider { arguments: [uri, i], }), ); - - if (codeLenses.length === 2) { - codeLenses.push( - new vscode.CodeLens(range, { - title: `${getMetaKeyLabel()}I to add instructions`, - command: "", - }), - ); - } } return codeLenses; From cb77d90a896918f64a62a8e5ad7d95ad7ff56b78 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 2 Jan 2025 23:26:33 -0600 Subject: [PATCH 2/2] Update handler.ts --- extensions/vscode/src/diff/vertical/handler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 80331f8059..a3a7f651fd 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -544,7 +544,7 @@ export class VerticalDiffHandler implements vscode.Disposable { codeLensBlocks.push({ numGreen, numRed, - start: this.startLine + diffs.length - 1 - numRed - numGreen, + start: this.startLine + diffs.length - numRed - numGreen, }); }