From e95fe8849a7b213bdf92d8409735fc0f58451310 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Tue, 8 Oct 2024 13:14:33 +0200 Subject: [PATCH 1/4] fix(patch apply): it should apply changes through the ide. --- src/sidebar.ts | 125 ++++++++++++++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 49 deletions(-) diff --git a/src/sidebar.ts b/src/sidebar.ts index 7bd9296d..70bf8b74 100644 --- a/src/sidebar.ts +++ b/src/sidebar.ts @@ -23,12 +23,14 @@ import { ideOpenSettingsAction, ideDiffPasteBackAction, ideDiffPreviewAction, - ChatThread, - DiffPreviewResponse, + type ChatThread, + type DiffPreviewResponse, setOpenFiles, resetDiffApi, ideAnimateFileStart, ideAnimateFileStop, + ideWriteResultsToFile, + type PatchResult, } from "refact-chat-js/dist/events"; import { basename, join } from "path"; import { diff_paste_back } from "./chatTab"; @@ -563,6 +565,10 @@ export class PanelWebview implements vscode.WebviewViewProvider { return this.stopFileAnimation(e.payload); } + if(ideWriteResultsToFile.match(e)) { + return this.writeResultsToFile(e.payload); + } + // if(ideOpenChatInNewTab.match(e)) { // return this.handleOpenInTab(e.payload); // } @@ -595,6 +601,71 @@ export class PanelWebview implements vscode.WebviewViewProvider { // } + createNewFileWithContent(fileName: string, content: string) { + const newFile = vscode.Uri.parse('untitled:' + fileName); + vscode.workspace.openTextDocument(newFile).then(document => { + const edit = new vscode.WorkspaceEdit(); + edit.insert(newFile, new vscode.Position(0, 0), content); + return vscode.workspace.applyEdit(edit).then(success => { + if (success) { + vscode.window.showTextDocument(document); + this.refetchDiffsOnSave(document); + } else { + vscode.window.showInformationMessage('Error: creating file ' + fileName); + } + }); + }); + } + + async addDiffToFile(fileName: string, content: string) { + const document = await vscode.workspace.openTextDocument(vscode.Uri.file(fileName)); + await vscode.window.showTextDocument(document); + + const start = new vscode.Position(0, 0); + const end = new vscode.Position(document.lineCount, 0); + const range = new vscode.Range(start, end); + + + diff_paste_back( + document, + range, + content + ); + // TODO: can remove this when diff api is removed. + this.refetchDiffsOnSave(document); + } + + async editFileWithContent(fileName: string, content: string) { + const document = await vscode.workspace.openTextDocument(vscode.Uri.file(fileName)); + const start = new vscode.Position(0, 0); + const end = new vscode.Position(document.lineCount, 0); + const range = new vscode.Range(start, end); + + const edit = new vscode.WorkspaceEdit(); + edit.delete(document.uri, range); + edit.insert(document.uri, start, content); + vscode.workspace.applyEdit(edit).then(success => { + if(success) { + vscode.window.showTextDocument(document); + } else { + vscode.window.showInformationMessage('Error: editing file ' + fileName); + } + }); + } + + + async writeResultsToFile(results: PatchResult[]) { + for(const result of results) { + if(result.file_name_add) { + this.createNewFileWithContent(result.file_name_add, result.file_text); + } else if(result.file_name_edit) { + this.editFileWithContent(result.file_name_edit, result.file_text); + } else if (result.file_name_delete) { + // TODO: delete the file + } + } + } + async startFileAnimation(fileName: string) { const openFiles = this.getOpenFiles(); @@ -694,60 +765,16 @@ export class PanelWebview implements vscode.WebviewViewProvider { } private async handleDiffPreview(response: DiffPreviewResponse) { - - // TODO: Won't work if no file is open :/ - // const editor = vscode.window.activeTextEditor; - // if(!editor) { return; } const openFiles = this.getOpenFiles(); for (const change of response.results) { if (change.file_name_edit !== null && change.file_text !== null) { - const document = await vscode.workspace.openTextDocument(vscode.Uri.file(change.file_name_edit)); - await vscode.window.showTextDocument(document); - - const start = new vscode.Position(0, 0); - const end = new vscode.Position(document.lineCount, 0); - const range = new vscode.Range(start, end); - - - diff_paste_back( - document, - range, - change.file_text - ); - this.refetchDiffsOnSave(document); - + this.addDiffToFile(change.file_name_edit, change.file_text); } else if(change.file_name_add !== null && change.file_text!== null && openFiles.includes(change.file_name_add) === false) { - const newFile = vscode.Uri.parse('untitled:' + change.file_name_add); - vscode.workspace.openTextDocument(newFile).then(document => { - const edit = new vscode.WorkspaceEdit(); - edit.insert(newFile, new vscode.Position(0, 0), change.file_text); - return vscode.workspace.applyEdit(edit).then(success => { - if (success) { - vscode.window.showTextDocument(document); - this.refetchDiffsOnSave(document); - } else { - vscode.window.showInformationMessage('Error: creating file ' + change.file_name_add); - } - }); - }); + this.createNewFileWithContent(change.file_name_add, change.file_text); } else if(change.file_name_add !== null && change.file_text!== null && openFiles.includes(change.file_name_add)) { - // almost duplicate of edit - const document = await vscode.workspace.openTextDocument(vscode.Uri.file(change.file_name_add)); - await vscode.window.showTextDocument(document); - const start = new vscode.Position(0, 0); - const end = new vscode.Position(document.lineCount, 0); - const range = new vscode.Range(start, end); - - diff_paste_back( - document, - range, - change.file_text - ); - - this.refetchDiffsOnSave(document); - + this.addDiffToFile(change.file_name_add, change.file_text); } // TODO: delete From ef33909dcc12b888f9354c1ba9d539cfc40c9f84 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Tue, 8 Oct 2024 13:34:19 +0200 Subject: [PATCH 2/4] feat(patch): add ability to delete file. --- src/sidebar.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/sidebar.ts b/src/sidebar.ts index 70bf8b74..90ca7267 100644 --- a/src/sidebar.ts +++ b/src/sidebar.ts @@ -600,6 +600,17 @@ export class PanelWebview implements vscode.WebviewViewProvider { // panel.webview.html = html; // } + + async deleteFile(fileName: string) { + const pathToFile = vscode.Uri.file(fileName); + const edit = new vscode.WorkspaceEdit(); + edit.deleteFile(pathToFile); + return vscode.workspace.applyEdit(edit).then(success => { + if(!success) { + vscode.window.showInformationMessage("Error: could not delete: " + pathToFile); + } + }) + } createNewFileWithContent(fileName: string, content: string) { const newFile = vscode.Uri.parse('untitled:' + fileName); @@ -661,7 +672,7 @@ export class PanelWebview implements vscode.WebviewViewProvider { } else if(result.file_name_edit) { this.editFileWithContent(result.file_name_edit, result.file_text); } else if (result.file_name_delete) { - // TODO: delete the file + this.deleteFile(result.file_name_delete); } } } @@ -775,9 +786,9 @@ export class PanelWebview implements vscode.WebviewViewProvider { this.createNewFileWithContent(change.file_name_add, change.file_text); } else if(change.file_name_add !== null && change.file_text!== null && openFiles.includes(change.file_name_add)) { this.addDiffToFile(change.file_name_add, change.file_text); + } else if(change.file_name_delete) { + this.deleteFile(change.file_name_delete); } - - // TODO: delete } } From 65d77cec39b6f88160b488b2869b2c4c42235979 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Tue, 8 Oct 2024 15:20:36 +0200 Subject: [PATCH 3/4] refactor(chat): remove `setOpenFiles` action --- src/sidebar.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/sidebar.ts b/src/sidebar.ts index 90ca7267..0231c145 100644 --- a/src/sidebar.ts +++ b/src/sidebar.ts @@ -25,7 +25,6 @@ import { ideDiffPreviewAction, type ChatThread, type DiffPreviewResponse, - setOpenFiles, resetDiffApi, ideAnimateFileStart, ideAnimateFileStop, @@ -116,11 +115,6 @@ export class PanelWebview implements vscode.WebviewViewProvider { } })); - this._disposables.push(vscode.window.onDidChangeVisibleTextEditors((editors) => { - this.sendOpenFiles(editors); - })); - - // this._disposables.push(vscode.workspace.onDidOpenTextDocument((event) => { // console.log("onDidOpenTextDocument"); @@ -147,12 +141,6 @@ export class PanelWebview implements vscode.WebviewViewProvider { return openFiles; } - sendOpenFiles(editors: readonly vscode.TextEditor[]): void { - const files = editors.map(editor => editor.document.uri.fsPath); - const message = setOpenFiles(files); - this._view?.webview.postMessage(message); - } - sendSnippetToChat() { const snippet = this.getSnippetFromEditor(); if(!snippet) { return; } From 345e9043206f5f60303544c920275f056808a487 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Tue, 8 Oct 2024 16:47:11 +0200 Subject: [PATCH 4/4] chore: bump chat to `v2.0.1-alpha.5`, patch results handlers. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 023a5210..cbb2e8c6 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "fetch-h2": "^3.0.2", "json5": "^2.2.3", "marked": "^4.0.8", - "refact-chat-js": "v2.0.1-alpha.3", + "refact-chat-js": "v2.0.1-alpha.5", "uuid": "^9.0.1", "vscode-languageclient": "^7.0.0" },