From 7c91cc34cc18485465cf318b084f5d65f549ce39 Mon Sep 17 00:00:00 2001 From: Justin Milner Date: Fri, 26 Apr 2024 16:13:53 -0400 Subject: [PATCH 1/3] CON-991-a accept/reject all working here --- extensions/vscode/src/commands.ts | 8 ++--- .../src/diff/verticalPerLine/handler.ts | 34 +++++++++++++++++++ .../src/diff/verticalPerLine/manager.ts | 32 ++++++++++++----- extensions/vscode/src/lang-server/codeLens.ts | 5 +-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 57e01d9e7..bbeb9793e 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -162,11 +162,11 @@ const commandsMap: ( verticalDiffManager.clearForFilepath(newFilepath, false); await diffManager.rejectDiff(newFilepath); }, - "continue.acceptVerticalDiffBlock": (filepath?: string, index?: number) => { - verticalDiffManager.acceptRejectVerticalDiffBlock(true, filepath, index); + "continue.acceptVerticalDiffBlock": (filepath?: string, index?: number, all?:boolean) => { + verticalDiffManager.acceptRejectVerticalDiffBlock(true, filepath, index, all); }, - "continue.rejectVerticalDiffBlock": (filepath?: string, index?: number) => { - verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index); + "continue.rejectVerticalDiffBlock": (filepath?: string, index?: number, all?: boolean) => { + verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index, all); }, "continue.quickFix": async (message: string, code: string, edit: boolean) => { sidebar.webviewProtocol?.request("newSessionWithPrompt", { diff --git a/extensions/vscode/src/diff/verticalPerLine/handler.ts b/extensions/vscode/src/diff/verticalPerLine/handler.ts index f6973e054..14552461c 100644 --- a/extensions/vscode/src/diff/verticalPerLine/handler.ts +++ b/extensions/vscode/src/diff/verticalPerLine/handler.ts @@ -376,4 +376,38 @@ export class VerticalPerLineDiffHandler { this.refreshCodeLens(); } + + public async rejectAllBlocks(startLineList: number[], numGreenList: number[]) { + // Delete all green lines + // Gather start and end indexes for all green lines to be deleted + const deletionRanges: vscode.Range[] = []; + for (let i = 0; i < startLineList.length; i++) { + const start = startLineList[i]; + const numGreen = numGreenList[i]; + // Assuming numGreen lines immediately follow the start line + if (numGreen > 0) { + const startLine = new vscode.Position(start, 0); + const endLine = startLine.translate(numGreen); + deletionRanges.push(new vscode.Range(startLine, endLine)); + } + } + + // Execute all deletion in one edit operation + await this.editor.edit((editBuilder) => { + for (const range of deletionRanges) { + editBuilder.delete(range); + } + }, { + undoStopAfter: false, + undoStopBefore: false, + }); + + //clear all decorations + this.redDecorationManager.clear() + this.greenDecorationManager.clear() + + //remove code lenses + this.editorToVerticalDiffCodeLens.set(this.filepath, []); + this.refreshCodeLens(); + } } diff --git a/extensions/vscode/src/diff/verticalPerLine/manager.ts b/extensions/vscode/src/diff/verticalPerLine/manager.ts index 45bb82c10..a54b9ed6a 100644 --- a/extensions/vscode/src/diff/verticalPerLine/manager.ts +++ b/extensions/vscode/src/diff/verticalPerLine/manager.ts @@ -97,10 +97,11 @@ export class VerticalPerLineDiffManager { vscode.commands.executeCommand("setContext", "continue.diffVisible", false); } - acceptRejectVerticalDiffBlock( + async acceptRejectVerticalDiffBlock( accept: boolean, filepath?: string, index?: number, + all?: boolean, ) { if (!filepath) { const activeEditor = vscode.window.activeTextEditor; @@ -125,13 +126,28 @@ export class VerticalPerLineDiffManager { return; } - // CodeLens object removed from editorToVerticalDiffCodeLens here - handler.acceptRejectBlock( - accept, - block.start, - block.numGreen, - block.numRed, - ); + if (!all && blocks.length > 1) { // Only accept/reject the block at the specified index + if (!blocks || blocks.length <= index) { + return; + } + handler.acceptRejectBlock( + accept, + blocks[index].start, + blocks[index].numGreen, + blocks[index].numRed, + ); + } else if (all && accept) { // Accept all blocks + for (const block of blocks) { + await handler.acceptRejectBlock( + accept, + block.start, + block.numGreen, + block.numRed, + ); + } + } else { // Reject all blocks + handler.rejectAllBlocks(blocks?.map(block => block.start), blocks?.map(block => block.numGreen)) + } if (blocks.length === 1) { this.clearForFilepath(filepath, true); diff --git a/extensions/vscode/src/lang-server/codeLens.ts b/extensions/vscode/src/lang-server/codeLens.ts index bdc7de4fd..e268be2d7 100644 --- a/extensions/vscode/src/lang-server/codeLens.ts +++ b/extensions/vscode/src/lang-server/codeLens.ts @@ -32,6 +32,7 @@ class VerticalPerLineCodeLensProvider implements vscode.CodeLensProvider { if (!blocks) { return []; } + console.log("blocks in provideCodeLenses: ", blocks.length) const codeLenses: vscode.CodeLens[] = []; for (let i = 0; i < blocks.length; i++) { @@ -46,12 +47,12 @@ class VerticalPerLineCodeLensProvider implements vscode.CodeLensProvider { new vscode.CodeLens(range, { title: `Accept All (${getMetaKeyLabel()}⇧↩)`, command: "continue.acceptVerticalDiffBlock", - arguments: [filepath, i], + arguments: [filepath, i, true], }), new vscode.CodeLens(range, { title: `Reject All (${getMetaKeyLabel()}⇧⌫)`, command: "continue.rejectVerticalDiffBlock", - arguments: [filepath, i], + arguments: [filepath, i, true], }), ); } From 957dbcc265b1c8a91eeaa093b7d3d8dfb2fa5b65 Mon Sep 17 00:00:00 2001 From: Justin Milner Date: Sat, 27 Apr 2024 22:21:04 -0400 Subject: [PATCH 2/3] CON-991-a: Everything working --- extensions/vscode/src/commands.ts | 2 +- .../src/diff/verticalPerLine/handler.ts | 42 ++++- .../src/diff/verticalPerLine/manager.ts | 152 ++++++++++++------ 3 files changed, 144 insertions(+), 52 deletions(-) diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index bbeb9793e..c6b790635 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -257,7 +257,7 @@ const commandsMap: ( "getDefaultModelTitle", undefined, ); - await verticalDiffManager.streamEdit(text, modelName); + await verticalDiffManager.streamEdit(text, modelName, previousInput); } else { // Pick context first const selectedProviders = await vscode.window.showQuickPick( diff --git a/extensions/vscode/src/diff/verticalPerLine/handler.ts b/extensions/vscode/src/diff/verticalPerLine/handler.ts index 14552461c..49d4b91b7 100644 --- a/extensions/vscode/src/diff/verticalPerLine/handler.ts +++ b/extensions/vscode/src/diff/verticalPerLine/handler.ts @@ -8,6 +8,7 @@ import { redDecorationType, } from "./decorations"; import { VerticalDiffCodeLens } from "./manager"; +import { start } from "repl"; export class VerticalPerLineDiffHandler { private editor: vscode.TextEditor; @@ -361,6 +362,13 @@ export class VerticalPerLineDiffHandler { this.redDecorationManager.shiftDownAfterLine(startLine, offset); this.greenDecorationManager.shiftDownAfterLine(startLine, offset); + // Shift the codelens objects + this.shiftCodeLensObjects(startLine, offset) + + this.refreshCodeLens(); + } + + private shiftCodeLensObjects(startLine: number, offset: number){ // Shift the codelens objects const blocks = this.editorToVerticalDiffCodeLens @@ -375,7 +383,7 @@ export class VerticalPerLineDiffHandler { this.editorToVerticalDiffCodeLens.set(this.filepath, blocks); this.refreshCodeLens(); - } +} public async rejectAllBlocks(startLineList: number[], numGreenList: number[]) { // Delete all green lines @@ -410,4 +418,36 @@ export class VerticalPerLineDiffHandler { this.editorToVerticalDiffCodeLens.set(this.filepath, []); this.refreshCodeLens(); } + + public updateLineDelta(filepath: string, startLine: number, lineDelta: number) { + // Retrieve the diff blocks for the given file + const blocks = this.editorToVerticalDiffCodeLens.get(filepath); + if (!blocks) { + return; + } + + this.redDecorationManager.shiftDownAfterLine(startLine, lineDelta); + this.greenDecorationManager.shiftDownAfterLine(startLine, lineDelta); + + // Update the diff blocks based on the line delta + const updatedBlocks = blocks.map(block => { + // If the change occurs before the block, adjust the block's start line + if (startLine <= block.start) { + block.start += lineDelta; + } + // If the change occurs within the block, adjust the number of green lines (red lines can't be edited) + else if (startLine < block.start + block.numGreen) { + block.numGreen += lineDelta + } + // If file changes occur after the block, that doesn't change anything for the block + return block; + }).filter(block => block.numRed > 0 || block.numGreen > 0); // Remove blocks with no red or green lines + + // Update the map with the filtered blocks + this.editorToVerticalDiffCodeLens.set(filepath, updatedBlocks); + + // Adjust the code lens's accordingly + // this.shiftCodeLensObjects(startLine, lineDelta) + // this.refreshCodeLens(); + } } diff --git a/extensions/vscode/src/diff/verticalPerLine/manager.ts b/extensions/vscode/src/diff/verticalPerLine/manager.ts index a54b9ed6a..4b2afd9f5 100644 --- a/extensions/vscode/src/diff/verticalPerLine/manager.ts +++ b/extensions/vscode/src/diff/verticalPerLine/manager.ts @@ -19,7 +19,12 @@ export class VerticalPerLineDiffManager { filepathToCodeLens: Map = new Map(); - constructor(private readonly configHandler: ConfigHandler) {} + private userChangeListener: vscode.Disposable | undefined; + + constructor(private readonly configHandler: ConfigHandler) { + console.log("Constructor being run") + this.userChangeListener = undefined + } createVerticalPerLineDiffHandler( filepath: string, @@ -49,36 +54,55 @@ export class VerticalPerLineDiffManager { } } - getOrCreateVerticalPerLineDiffHandler( - filepath: string, - startLine: number, - endLine: number, - ) { - if (this.filepathToHandler.has(filepath)) { - return this.filepathToHandler.get(filepath)!; - } else { - const editor = vscode.window.activeTextEditor; // TODO - if (editor && editor.document.uri.fsPath === filepath) { - const handler = new VerticalPerLineDiffHandler( - startLine, - endLine, - editor, - this.filepathToCodeLens, - this.clearForFilepath.bind(this), - this.refreshCodeLens, - ); - this.filepathToHandler.set(filepath, handler); - return handler; - } else { - return undefined; - } - } - } - getHandlerForFile(filepath: string) { return this.filepathToHandler.get(filepath); } + //Creates a listener for document changes by user. + private enableDocumentChangeListener(): vscode.Disposable | undefined { + if (this.userChangeListener) { + console.log("Document change listener already enabled."); + return; + } + + console.log("enabled") + this.userChangeListener = vscode.workspace.onDidChangeTextDocument((event) => { + // Check if there is an active handler for the affected file + const filepath = event.document.uri.fsPath; + const handler = this.getHandlerForFile(filepath); + if (handler) { + // If there is an active diff for that file, handle the document change + this.handleDocumentChange(event, handler); + } + }); + } + + //Listener for user doc changes is disabled during updates to the text document by continue. + public disableDocumentChangeListener() { + if (this.userChangeListener) { + this.userChangeListener.dispose(); + this.userChangeListener = undefined; + } + } + + private handleDocumentChange( + event: vscode.TextDocumentChangeEvent, + handler: VerticalPerLineDiffHandler + ) { + // Loop through each change in the event + event.contentChanges.forEach((change) => { + // Calculate the number of lines added or removed + const linesAdded = change.text.split('\n').length - 1; + const linesDeleted = change.range.end.line - change.range.start.line; + const lineDelta = linesAdded - linesDeleted; + + + console.log("handling doc change: ", linesAdded, " ", linesDeleted) + // Update the diff handler with the new line delta + handler.updateLineDelta(event.document.uri.fsPath, change.range.start.line, lineDelta); + }); + } + clearForFilepath(filepath: string | undefined, accept: boolean) { if (!filepath) { const activeEditor = vscode.window.activeTextEditor; @@ -126,27 +150,38 @@ export class VerticalPerLineDiffManager { return; } - if (!all && blocks.length > 1) { // Only accept/reject the block at the specified index - if (!blocks || blocks.length <= index) { - return; - } - handler.acceptRejectBlock( - accept, - blocks[index].start, - blocks[index].numGreen, - blocks[index].numRed, - ); - } else if (all && accept) { // Accept all blocks - for (const block of blocks) { + try { + //Disable user file change listener + console.log("disabling") + this.disableDocumentChangeListener() + + if (!all && blocks.length > 1) { // Only accept/reject the block at the specified index + if (!blocks || blocks.length <= index) { + return; + } await handler.acceptRejectBlock( accept, - block.start, - block.numGreen, - block.numRed, + blocks[index].start, + blocks[index].numGreen, + blocks[index].numRed, ); - } - } else { // Reject all blocks - handler.rejectAllBlocks(blocks?.map(block => block.start), blocks?.map(block => block.numGreen)) + } else if (all && accept) { // Accept all blocks + for (const block of blocks) { + await handler.acceptRejectBlock( + accept, + block.start, + block.numGreen, + block.numRed, + ); + } + } else { // Reject all blocks + await handler.rejectAllBlocks(blocks?.map(block => block.start), blocks?.map(block => block.numGreen)) + } + } finally { + if (blocks.length > 1){ + console.log(blocks.length - 1, " blocks still remaining. Enabling") + this.enableDocumentChangeListener() + } } if (blocks.length === 1) { @@ -154,29 +189,43 @@ export class VerticalPerLineDiffManager { } } - async streamEdit(input: string, modelTitle: string | undefined) { + async streamEdit(input: string, modelTitle: string | undefined, quickEdit?: string) { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); const editor = vscode.window.activeTextEditor; + if (!editor) { return; } const filepath = editor.document.uri.fsPath; - const startLine = editor.selection.start.line; - const endLine = editor.selection.end.line; + // Check for existing diffs in the same file the new one will be created in const existingHandler = this.getHandlerForFile(filepath); + + // initialize start/end line to existing handler if it exists and should reuse previousInput, + // otherwise, use the editor selection + const startLine = (quickEdit && existingHandler) ? existingHandler.range.start.line : editor.selection.start.line + const endLine = (quickEdit && existingHandler) ? existingHandler.range.end.line : editor.selection.end.line + + if (existingHandler && !quickEdit) { //If there is an existing handler and should not reuse block + //reject the existing diff + console.log("New diff being created - rejecting previous diff in: ", filepath) + this.acceptRejectVerticalDiffBlock(false, filepath, undefined, true) + } existingHandler?.clear(false); + await new Promise((resolve) => { setTimeout(resolve, 200); }); + const diffHandler = this.createVerticalPerLineDiffHandler( filepath, - existingHandler?.range.start.line ?? startLine, - existingHandler?.range.end.line ?? endLine, + startLine, + endLine, input, ); + if (!diffHandler) { return; } @@ -238,6 +287,9 @@ export class VerticalPerLineDiffManager { console.error("Error streaming diff:", e); vscode.window.showErrorMessage(`Error streaming diff: ${e}`); } finally { + console.log("enable in streamEdit") + this.enableDocumentChangeListener() + vscode.commands.executeCommand( "setContext", "continue.streamingDiff", From a5d981f5475fed4ee4aa33cde4644a6402c1e236 Mon Sep 17 00:00:00 2001 From: Justin Milner Date: Mon, 29 Apr 2024 17:04:29 -0400 Subject: [PATCH 3/3] Testing completed --- extensions/vscode/src/commands.ts | 10 +- .../src/diff/verticalPerLine/handler.ts | 80 +++-------- .../src/diff/verticalPerLine/manager.ts | 128 +++++++++--------- extensions/vscode/src/lang-server/codeLens.ts | 9 +- 4 files changed, 90 insertions(+), 137 deletions(-) diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index c6b790635..8c90d0417 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -162,11 +162,11 @@ const commandsMap: ( verticalDiffManager.clearForFilepath(newFilepath, false); await diffManager.rejectDiff(newFilepath); }, - "continue.acceptVerticalDiffBlock": (filepath?: string, index?: number, all?:boolean) => { - verticalDiffManager.acceptRejectVerticalDiffBlock(true, filepath, index, all); + "continue.acceptVerticalDiffBlock": (filepath?: string, index?: number) => { + verticalDiffManager.acceptRejectVerticalDiffBlock(true, filepath, index); }, - "continue.rejectVerticalDiffBlock": (filepath?: string, index?: number, all?: boolean) => { - verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index, all); + "continue.rejectVerticalDiffBlock": (filepath?: string, index?: number) => { + verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index); }, "continue.quickFix": async (message: string, code: string, edit: boolean) => { sidebar.webviewProtocol?.request("newSessionWithPrompt", { @@ -302,7 +302,7 @@ const commandsMap: ( "\n\n---\n\n" + text; - await verticalDiffManager.streamEdit(text, defaultModelTitle); + await verticalDiffManager.streamEdit(text, defaultModelTitle, previousInput); } } }, diff --git a/extensions/vscode/src/diff/verticalPerLine/handler.ts b/extensions/vscode/src/diff/verticalPerLine/handler.ts index 49d4b91b7..23ff3f9ec 100644 --- a/extensions/vscode/src/diff/verticalPerLine/handler.ts +++ b/extensions/vscode/src/diff/verticalPerLine/handler.ts @@ -8,7 +8,6 @@ import { redDecorationType, } from "./decorations"; import { VerticalDiffCodeLens } from "./manager"; -import { start } from "repl"; export class VerticalPerLineDiffHandler { private editor: vscode.TextEditor; @@ -209,7 +208,21 @@ export class VerticalPerLineDiffHandler { this.editor.setDecorations(indexDecorationType, []); } - clear(accept: boolean) { + 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", @@ -225,7 +238,7 @@ export class VerticalPerLineDiffHandler { this.editorToVerticalDiffCodeLens.delete(this.filepath); - this.editor.edit( + await this.editor.edit( (editBuilder) => { for (const range of rangesToDelete) { editBuilder.delete( @@ -364,8 +377,6 @@ export class VerticalPerLineDiffHandler { // Shift the codelens objects this.shiftCodeLensObjects(startLine, offset) - - this.refreshCodeLens(); } private shiftCodeLensObjects(startLine: number, offset: number){ @@ -383,41 +394,7 @@ export class VerticalPerLineDiffHandler { this.editorToVerticalDiffCodeLens.set(this.filepath, blocks); this.refreshCodeLens(); -} - - public async rejectAllBlocks(startLineList: number[], numGreenList: number[]) { - // Delete all green lines - // Gather start and end indexes for all green lines to be deleted - const deletionRanges: vscode.Range[] = []; - for (let i = 0; i < startLineList.length; i++) { - const start = startLineList[i]; - const numGreen = numGreenList[i]; - // Assuming numGreen lines immediately follow the start line - if (numGreen > 0) { - const startLine = new vscode.Position(start, 0); - const endLine = startLine.translate(numGreen); - deletionRanges.push(new vscode.Range(startLine, endLine)); - } - } - - // Execute all deletion in one edit operation - await this.editor.edit((editBuilder) => { - for (const range of deletionRanges) { - editBuilder.delete(range); - } - }, { - undoStopAfter: false, - undoStopBefore: false, - }); - - //clear all decorations - this.redDecorationManager.clear() - this.greenDecorationManager.clear() - - //remove code lenses - this.editorToVerticalDiffCodeLens.set(this.filepath, []); - this.refreshCodeLens(); - } + } public updateLineDelta(filepath: string, startLine: number, lineDelta: number) { // Retrieve the diff blocks for the given file @@ -426,28 +403,11 @@ export class VerticalPerLineDiffHandler { return; } + //update decorations this.redDecorationManager.shiftDownAfterLine(startLine, lineDelta); this.greenDecorationManager.shiftDownAfterLine(startLine, lineDelta); - // Update the diff blocks based on the line delta - const updatedBlocks = blocks.map(block => { - // If the change occurs before the block, adjust the block's start line - if (startLine <= block.start) { - block.start += lineDelta; - } - // If the change occurs within the block, adjust the number of green lines (red lines can't be edited) - else if (startLine < block.start + block.numGreen) { - block.numGreen += lineDelta - } - // If file changes occur after the block, that doesn't change anything for the block - return block; - }).filter(block => block.numRed > 0 || block.numGreen > 0); // Remove blocks with no red or green lines - - // Update the map with the filtered blocks - this.editorToVerticalDiffCodeLens.set(filepath, updatedBlocks); - - // Adjust the code lens's accordingly - // this.shiftCodeLensObjects(startLine, lineDelta) - // this.refreshCodeLens(); + //update code lens + this.shiftCodeLensObjects(startLine, lineDelta) } } diff --git a/extensions/vscode/src/diff/verticalPerLine/manager.ts b/extensions/vscode/src/diff/verticalPerLine/manager.ts index 4b2afd9f5..d66804e44 100644 --- a/extensions/vscode/src/diff/verticalPerLine/manager.ts +++ b/extensions/vscode/src/diff/verticalPerLine/manager.ts @@ -22,7 +22,6 @@ export class VerticalPerLineDiffManager { private userChangeListener: vscode.Disposable | undefined; constructor(private readonly configHandler: ConfigHandler) { - console.log("Constructor being run") this.userChangeListener = undefined } @@ -58,32 +57,30 @@ export class VerticalPerLineDiffManager { return this.filepathToHandler.get(filepath); } - //Creates a listener for document changes by user. + // Creates a listener for document changes by user. private enableDocumentChangeListener(): vscode.Disposable | undefined { - if (this.userChangeListener) { - console.log("Document change listener already enabled."); + if (this.userChangeListener) { //Only create one listener per file return; } - console.log("enabled") this.userChangeListener = vscode.workspace.onDidChangeTextDocument((event) => { // Check if there is an active handler for the affected file const filepath = event.document.uri.fsPath; const handler = this.getHandlerForFile(filepath); - if (handler) { + if (handler ) { // If there is an active diff for that file, handle the document change this.handleDocumentChange(event, handler); } }); } - //Listener for user doc changes is disabled during updates to the text document by continue. + // Listener for user doc changes is disabled during updates to the text document by continue public disableDocumentChangeListener() { - if (this.userChangeListener) { - this.userChangeListener.dispose(); - this.userChangeListener = undefined; - } + if (this.userChangeListener) { + this.userChangeListener.dispose(); + this.userChangeListener = undefined; } + } private handleDocumentChange( event: vscode.TextDocumentChangeEvent, @@ -96,8 +93,6 @@ export class VerticalPerLineDiffManager { const linesDeleted = change.range.end.line - change.range.start.line; const lineDelta = linesAdded - linesDeleted; - - console.log("handling doc change: ", linesAdded, " ", linesDeleted) // Update the diff handler with the new line delta handler.updateLineDelta(event.document.uri.fsPath, change.range.start.line, lineDelta); }); @@ -118,14 +113,15 @@ export class VerticalPerLineDiffManager { this.filepathToHandler.delete(filepath); } + this.disableDocumentChangeListener() + vscode.commands.executeCommand("setContext", "continue.diffVisible", false); } async acceptRejectVerticalDiffBlock( accept: boolean, filepath?: string, - index?: number, - all?: boolean, + index?: number ) { if (!filepath) { const activeEditor = vscode.window.activeTextEditor; @@ -150,75 +146,72 @@ export class VerticalPerLineDiffManager { return; } - try { - //Disable user file change listener - console.log("disabling") - this.disableDocumentChangeListener() + // Disable listening to file changes while continue makes changes + this.disableDocumentChangeListener() - if (!all && blocks.length > 1) { // Only accept/reject the block at the specified index - if (!blocks || blocks.length <= index) { - return; - } - await handler.acceptRejectBlock( - accept, - blocks[index].start, - blocks[index].numGreen, - blocks[index].numRed, - ); - } else if (all && accept) { // Accept all blocks - for (const block of blocks) { - await handler.acceptRejectBlock( - accept, - block.start, - block.numGreen, - block.numRed, - ); - } - } else { // Reject all blocks - await handler.rejectAllBlocks(blocks?.map(block => block.start), blocks?.map(block => block.numGreen)) - } - } finally { - if (blocks.length > 1){ - console.log(blocks.length - 1, " blocks still remaining. Enabling") - this.enableDocumentChangeListener() - } - } + // CodeLens object removed from editorToVerticalDiffCodeLens here + await handler.acceptRejectBlock( + accept, + block.start, + block.numGreen, + block.numRed, + ); if (blocks.length === 1) { this.clearForFilepath(filepath, true); + } else { + // Re-enable listener for user changes to file + this.enableDocumentChangeListener() } } async streamEdit(input: string, modelTitle: string | undefined, quickEdit?: string) { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); - const editor = vscode.window.activeTextEditor; + let editor = vscode.window.activeTextEditor; if (!editor) { return; } const filepath = editor.document.uri.fsPath; - - // Check for existing diffs in the same file the new one will be created in - const existingHandler = this.getHandlerForFile(filepath); - // initialize start/end line to existing handler if it exists and should reuse previousInput, - // otherwise, use the editor selection - const startLine = (quickEdit && existingHandler) ? existingHandler.range.start.line : editor.selection.start.line - const endLine = (quickEdit && existingHandler) ? existingHandler.range.end.line : editor.selection.end.line + // Initialize start/end at editor selection + let startLine = editor.selection.start.line + let endLine = editor.selection.end.line - if (existingHandler && !quickEdit) { //If there is an existing handler and should not reuse block - //reject the existing diff - console.log("New diff being created - rejecting previous diff in: ", filepath) - this.acceptRejectVerticalDiffBlock(false, filepath, undefined, true) - } - existingHandler?.clear(false); + // Check for existing handlers in the same file the new one will be created in + const existingHandler = this.getHandlerForFile(filepath); + + if (existingHandler) { + let existingStartLine = existingHandler?.range.start.line + if (quickEdit) { // Previous diff was a quickEdit + // Check if user has highlighted a range + let rangeBool = (startLine != endLine) ||(editor.selection.start.character != editor.selection.end.character) + + // Check if the range is different from the previous range + let newRangeBool = (startLine != existingHandler.range.start.line) || (endLine != existingHandler.range.end.line) + + if (!rangeBool || !newRangeBool) { + // User did not highlight a new range -> use start/end from the previous quickEdit + startLine = existingHandler.range.start.line + endLine = existingHandler.range.end.line + } + } + + // Clear the previous handler + let effectiveLineDelta = existingHandler.getLineDeltaBeforeLine(startLine) + existingHandler.clear(false) + + startLine += effectiveLineDelta + endLine += effectiveLineDelta + } await new Promise((resolve) => { setTimeout(resolve, 200); }); + // Create new handler with determined start/end const diffHandler = this.createVerticalPerLineDiffHandler( filepath, startLine, @@ -227,13 +220,14 @@ export class VerticalPerLineDiffManager { ); if (!diffHandler) { + console.warn("Issue occured while creating new vertical diff handler") return; } - let selectedRange = existingHandler?.range ?? editor.selection; + let selectedRange = diffHandler.range // Only if the selection is empty, use exact prefix/suffix instead of by line - if (!selectedRange.isEmpty) { + if (selectedRange.isEmpty) { selectedRange = new vscode.Range( editor.selection.start.with(undefined, 0), editor.selection.end.with(undefined, Number.MAX_SAFE_INTEGER), @@ -283,13 +277,13 @@ export class VerticalPerLineDiffManager { getMarkdownLanguageTagForFile(filepath), ), ); + + // enable a listener for user edits to file while diff is open + this.enableDocumentChangeListener() } catch (e) { - console.error("Error streaming diff:", e); + this.disableDocumentChangeListener() vscode.window.showErrorMessage(`Error streaming diff: ${e}`); } finally { - console.log("enable in streamEdit") - this.enableDocumentChangeListener() - vscode.commands.executeCommand( "setContext", "continue.streamingDiff", diff --git a/extensions/vscode/src/lang-server/codeLens.ts b/extensions/vscode/src/lang-server/codeLens.ts index e268be2d7..168d802d3 100644 --- a/extensions/vscode/src/lang-server/codeLens.ts +++ b/extensions/vscode/src/lang-server/codeLens.ts @@ -32,7 +32,6 @@ class VerticalPerLineCodeLensProvider implements vscode.CodeLensProvider { if (!blocks) { return []; } - console.log("blocks in provideCodeLenses: ", blocks.length) const codeLenses: vscode.CodeLens[] = []; for (let i = 0; i < blocks.length; i++) { @@ -46,13 +45,13 @@ class VerticalPerLineCodeLensProvider implements vscode.CodeLensProvider { codeLenses.push( new vscode.CodeLens(range, { title: `Accept All (${getMetaKeyLabel()}⇧↩)`, - command: "continue.acceptVerticalDiffBlock", - arguments: [filepath, i, true], + command: "continue.acceptDiff", + arguments: [filepath, i], }), new vscode.CodeLens(range, { title: `Reject All (${getMetaKeyLabel()}⇧⌫)`, - command: "continue.rejectVerticalDiffBlock", - arguments: [filepath, i, true], + command: "continue.rejectDiff", + arguments: [filepath, i], }), ); }