From a340f00ca1ada0a0c18d66c0a8a70a6741609114 Mon Sep 17 00:00:00 2001 From: Shirshajit Sen Gupta Date: Fri, 27 Jun 2025 00:01:23 +0800 Subject: [PATCH 1/2] Fix: reset button functionality --- src/utils/editor.ts | 15 +++++++++++++++ src/utils/messageHandler.tsx | 7 ++++++- src/utils/messages.ts | 7 +++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/utils/editor.ts b/src/utils/editor.ts index 62a05d0..a69ff46 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -196,4 +196,19 @@ export class Editor { ) { this.onChangeCallback = callback; } + + reset(prepend: string, initialCode: string) { + this.log(`EXTENSION: Editor's reset called`); + this.replace( + prepend !== "" + ? [ + "// PREPEND -- DO NOT EDIT", + prepend, + "// END PREPEND", + initialCode, + ].join("\n") + : initialCode, + "reset", + ); + } } diff --git a/src/utils/messageHandler.tsx b/src/utils/messageHandler.tsx index c17415d..d588a84 100644 --- a/src/utils/messageHandler.tsx +++ b/src/utils/messageHandler.tsx @@ -111,7 +111,12 @@ export class MessageHandler { sendToFrontend(this.panel, msg); break; } - + case MessageTypeNames.ResetEditor: + if (this.activeEditor) { + this.activeEditor.reset("", message.initialCode); + this.panel?.reveal(vscode.ViewColumn.Two); + } + break; case MessageTypeNames.NewEditor: this.activeEditor = await Editor.create( message.workspaceLocation, diff --git a/src/utils/messages.ts b/src/utils/messages.ts index b35c498..0ce9367 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -48,6 +48,13 @@ const Messages = createMessages({ EvalEditor: (workspaceLocation: VscWorkspaceLocation) => ({ workspaceLocation: workspaceLocation, }), + ResetEditor: ( + workspaceLocation: VscWorkspaceLocation, + initialCode: string, + ) => ({ + workspaceLocation, + initialCode, + }), NotifyAssessmentsOverview: ( assessmentOverviews: VscAssessmentOverview[], courseId: number, From b4864cc7f69b4d10d22a7d8706a12a2c3bec1a31 Mon Sep 17 00:00:00 2001 From: Shirshajit Sen Gupta Date: Fri, 27 Jun 2025 00:14:54 +0800 Subject: [PATCH 2/2] Save editor on reset --- src/utils/editor.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/utils/editor.ts b/src/utils/editor.ts index a69ff46..dfa3c0e 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -197,7 +197,31 @@ export class Editor { this.onChangeCallback = callback; } - reset(prepend: string, initialCode: string) { + async save() { + this.log(`EXTENSION: Editor's save called`); + if (!this.editor) { + this.log(`EXTENSION: Editor is not defined, cannot save`); + return; + } + const text = this.editor.document.getText(); + if (text === "") { + this.log(`EXTENSION: Editor's code is empty, not saving`); + return; + } + await vscode.workspace.fs.writeFile( + vscode.Uri.file(this.editor.document.fileName), + new TextEncoder().encode(text), + ); + this.log(`EXTENSION: Editor's code saved successfully`); + //?: Send the code to the frontend - if needed + // const message = Messages.Text( + // this.workspaceLocation, + // text, + // ); + // sendToFrontendWrapped(message); + } + + async reset(prepend: string, initialCode: string) { this.log(`EXTENSION: Editor's reset called`); this.replace( prepend !== "" @@ -210,5 +234,7 @@ export class Editor { : initialCode, "reset", ); + await this.save(); + this.log(`EXTENSION: Editor's code reset successfully`); } }