Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(patch apply): it should apply changes through the ide. #145

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
152 changes: 89 additions & 63 deletions src/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import {
ideOpenSettingsAction,
ideDiffPasteBackAction,
ideDiffPreviewAction,
ChatThread,
DiffPreviewResponse,
setOpenFiles,
type ChatThread,
type DiffPreviewResponse,
resetDiffApi,
ideAnimateFileStart,
ideAnimateFileStop,
ideWriteResultsToFile,
type PatchResult,
} from "refact-chat-js/dist/events";
import { basename, join } from "path";
import { diff_paste_back } from "./chatTab";
Expand Down Expand Up @@ -114,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");
Expand All @@ -145,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; }
Expand Down Expand Up @@ -563,6 +553,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);
// }
Expand Down Expand Up @@ -594,6 +588,82 @@ 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);
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) {
this.deleteFile(result.file_name_delete);
}
}
}

async startFileAnimation(fileName: string) {

Expand Down Expand Up @@ -694,63 +764,19 @@ 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);
} else if(change.file_name_delete) {
this.deleteFile(change.file_name_delete);
}

// TODO: delete
}
}

Expand Down
Loading