diff --git a/src/utils/editor.ts b/src/utils/editor.ts index 62a05d0..dfa3c0e 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -196,4 +196,45 @@ export class Editor { ) { this.onChangeCallback = callback; } + + 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 !== "" + ? [ + "// PREPEND -- DO NOT EDIT", + prepend, + "// END PREPEND", + initialCode, + ].join("\n") + : initialCode, + "reset", + ); + await this.save(); + this.log(`EXTENSION: Editor's code reset successfully`); + } } 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,