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], }), ); }