From 4d93311a2bf83ba838eb80cdc5b80cae6fa9f267 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 9 Feb 2024 21:35:44 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20follow-up=20quick=20edits=20with=20?= =?UTF-8?q?cmd/ctrl+shift+L?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/diff/verticalPerLine/handler.ts | 16 +++- .../src/diff/verticalPerLine/manager.ts | 78 +++++++++++-------- extensions/vscode/src/lang-server/codeLens.ts | 2 +- 3 files changed, 63 insertions(+), 33 deletions(-) diff --git a/extensions/vscode/src/diff/verticalPerLine/handler.ts b/extensions/vscode/src/diff/verticalPerLine/handler.ts index 9ed45928c..d6b6e9704 100644 --- a/extensions/vscode/src/diff/verticalPerLine/handler.ts +++ b/extensions/vscode/src/diff/verticalPerLine/handler.ts @@ -19,13 +19,27 @@ export class VerticalPerLineDiffHandler { private currentLineIndex: number; private cancelled: boolean = false; + public get range(): vscode.Range { + const startLine = Math.min(this.startLine, this.endLine); + const endLine = Math.max(this.startLine, this.endLine); + return new vscode.Range(startLine, 0, endLine, Number.MAX_SAFE_INTEGER); + } + private newLinesAdded: number = 0; - constructor(startLine: number, endLine: number, editor: vscode.TextEditor) { + public input?: string; + + constructor( + startLine: number, + endLine: number, + editor: vscode.TextEditor, + input?: string + ) { this.currentLineIndex = startLine; this.startLine = startLine; this.endLine = endLine; this.editor = editor; + this.input = input; this.redDecorationManager = new DecorationTypeRangeManager( redDecorationType, diff --git a/extensions/vscode/src/diff/verticalPerLine/manager.ts b/extensions/vscode/src/diff/verticalPerLine/manager.ts index a85b58100..ed32376b0 100644 --- a/extensions/vscode/src/diff/verticalPerLine/manager.ts +++ b/extensions/vscode/src/diff/verticalPerLine/manager.ts @@ -11,7 +11,8 @@ class VerticalPerLineDiffManager { createVerticalPerLineDiffHandler( filepath: string, startLine: number, - endLine: number + endLine: number, + input: string ) { if (this.filepathToEditorMap.has(filepath)) { this.filepathToEditorMap.get(filepath)?.clear(false); @@ -22,7 +23,8 @@ class VerticalPerLineDiffManager { const handler = new VerticalPerLineDiffHandler( startLine, endLine, - editor + editor, + input ); this.filepathToEditorMap.set(filepath, handler); return handler; @@ -87,49 +89,63 @@ export async function streamEdit(input: string) { const startLine = editor.selection.start.line; const endLine = editor.selection.end.line; + const existingHandler = + verticalPerLineDiffManager.getHandlerForFile(filepath); + existingHandler?.clear(false); + await new Promise((resolve) => { + setTimeout(resolve, 200); + }); const diffHandler = verticalPerLineDiffManager.createVerticalPerLineDiffHandler( filepath, - startLine, - endLine + existingHandler?.range.start.line ?? startLine, + existingHandler?.range.end.line ?? endLine, + input ); + if (!diffHandler) { + return; + } - if (diffHandler) { - const selectedRange = new vscode.Range( + const selectedRange = + existingHandler?.range ?? + new vscode.Range( editor.selection.start.with(undefined, 0), editor.selection.end.with(undefined, Number.MAX_SAFE_INTEGER) ); - const rangeContent = editor.document.getText(selectedRange); - const llm = await llmFromTitle(); + const rangeContent = editor.document.getText(selectedRange); + const llm = await llmFromTitle(); - // Unselect the range - editor.selection = new vscode.Selection( - editor.selection.active, - editor.selection.active + // Unselect the range + editor.selection = new vscode.Selection( + editor.selection.active, + editor.selection.active + ); + + vscode.commands.executeCommand("setContext", "continue.streamingDiff", true); + + if (existingHandler?.input) { + if (existingHandler.input.startsWith("Original request: ")) { + existingHandler.input = existingHandler.input.substring( + "Original request: ".length + ); + } + input = `Original request: ${existingHandler.input}\nUpdated request: ${input}`; + } + try { + await diffHandler.run( + streamDiffLines( + rangeContent, + llm, + input, + getMarkdownLanguageTagForFile(filepath) + ) ); - + } finally { vscode.commands.executeCommand( "setContext", "continue.streamingDiff", - true + false ); - - try { - await diffHandler.run( - streamDiffLines( - rangeContent, - llm, - input, - getMarkdownLanguageTagForFile(filepath) - ) - ); - } finally { - vscode.commands.executeCommand( - "setContext", - "continue.streamingDiff", - false - ); - } } } diff --git a/extensions/vscode/src/lang-server/codeLens.ts b/extensions/vscode/src/lang-server/codeLens.ts index 46331654f..04915b777 100644 --- a/extensions/vscode/src/lang-server/codeLens.ts +++ b/extensions/vscode/src/lang-server/codeLens.ts @@ -41,7 +41,7 @@ class VerticalPerLineCodeLensProvider implements vscode.CodeLensProvider { if (codeLenses.length === 2) { codeLenses.push( new vscode.CodeLens(range, { - title: `(${getMetaKeyLabel()}⇧↩/${getMetaKeyLabel()}⇧⌫ to accept/reject all)`, + title: `(${getMetaKeyLabel()}⇧↩/${getMetaKeyLabel()}⇧⌫ to accept/reject all, ${getMetaKeyLabel()}⇧L to retry)`, command: "", }) );