Skip to content

Feat: reset button #40

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

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 41 additions & 0 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On resets, the prepend is lost because prepend is always an empty 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`);
}
}
7 changes: 6 additions & 1 deletion src/utils/messageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ const Messages = createMessages({
EvalEditor: (workspaceLocation: VscWorkspaceLocation) => ({
workspaceLocation: workspaceLocation,
}),
ResetEditor: (
workspaceLocation: VscWorkspaceLocation,
initialCode: string,
) => ({
workspaceLocation,
initialCode,
}),
NotifyAssessmentsOverview: (
assessmentOverviews: VscAssessmentOverview[],
courseId: number,
Expand Down